Posted on Leave a comment

Convert JSON to Array in PHP with Online Demo

by Vincy. Last modified on October 27th, 2022.

This tutorial covers the basic details of the PHP json_encode function. It gives examples of decoding JSON string input to a PHP array.

It also describes this PHP JSON function‘s conventions, rules and limitations. First, let’s see a quick example of converting JSON to an array.

Convert JSON to PHP Array

This example has a JSON string that maps the animal with its count. The output of converting this JSON will return an associative array.

It uses PHP json_decode() with boolean true as its second parameter. With these decoding params, the JSON will be converted into a PHP array.

Quick example

<?php
// JSON string in PHP Array
$jsonString = '{"Lion":101,"Tiger":102,"Crocodile":103,"Elephant":104}';
$phpArray = json_decode($jsonString, true); // display the converted PHP array
var_dump($phpArray);
?>

Output

array(4) { ["Lion"]=> int(101) ["Tiger"]=> int(102) ["Crocodile"]=> int(103) ["Elephant"]=> int(104)
}

See this online demo to get the converted array result from a JSON input.
View demo

See the diagram that shows the input JSON string and the output stdClass object of the JSON decoding. In the previous article, we have seen examples of the reverse operation that is converting a PHP array to a JSON string.
php json to array

PHP json_decode()

This native PHP function decodes the JSON string into a parsable object tree or an array. This is the syntax of this function.

json_decode( string $json, ?bool $associative = null, int $depth = 512, int $flags = 0
): mixed
  1. $json – Input JSON string.
  2. $associative – a boolean based on which the output format varies between an associative array and a stdClass object.
  3. $depth – the allowed nesting limit.
  4. $flag – Predefine constants to enable features like exception handling during the JSON to array convert.

You can find more about this function in the official documentation online.

Convert JSON to PHP Object

This program has a minute change of not setting the boolean flag to the PHP json_decode function. This will return a PHP stdClass object tree instead of an array.

<?php
// JSON string in PHP Array
$jsonString = '{"name":"Lion"}'; $phpObject = json_decode($jsonString);
print $phpObject->name;
?>

Output

Lion

Common mistakes during conversion from JSON to Array

The following JSON string is a valid JSON object in JavaScript, but not here in PHP. The issue is the single quote. It should be changed to a double quote.

If you want to see the JavaScript example to read and display JSON data the linked article has the code.

<?php
// 1. key and value should be within double quotes
$notValidJson = "{ 'lion': 'animal' }";
json_decode($notValidJson); // will return null // 2. without a quote is also not allowed
$notValidJson = '{ lion: "animal" }';
json_decode($notValidJson); // will return null // 3. should not have a comma at the end
$notValidJson = '{ "lion": "animal", }';
json_decode($notValidJson); // will return null
?>

How to convert JSON with large integers

This can be achieved by setting the bitmask parameter of the predefined JSON constants.

The JSON_BIGINT_AS_STRING constant is used to convert JSON with data having large integers.

<?php
$jsonString = '{"largeNumber": 12345678901234567890123}'; var_dump(json_decode($jsonString, false, 512, JSON_BIGINT_AS_STRING));
?>

Output

object(stdClass)#1 (1) { ["number"]=> string(20) "12345678901234567890123"
}

How to get errors when using json_decode

The function json_last_error() is used to return details about the last error occurrence. The following example handles the possible error cases of this PHP JSON function.

<?php
$jsonString = '{"Lion":101,"Tiger":102,"Crocodile":103,"Elephant":104}';
json_decode($jsonString); switch (json_last_error()) { case JSON_ERROR_DEPTH: echo 'Error: Nesting limit exceeded.'; break; case JSON_ERROR_STATE_MISMATCH: echo 'Error: Modes mismatch.'; break; case JSON_ERROR_CTRL_CHAR: echo 'Error: Unexpected character found.'; break; case JSON_ERROR_SYNTAX: echo 'Error: Syntax error, invalid JSON.'; break; case JSON_ERROR_UTF8: echo 'Error: UTF-8 characters incorrect encoding.'; break; default: echo 'Unexpected error.'; break;
}
?>

SURPRISE! JSON to Array and Array to JSON conversion is not symmetrical

<?php $jsonString = '{"0": "No", "1": "Yes"}'; // convert json to an associative array $array = json_decode($jsonString, true); print json_encode($array) . PHP_EOL;
?>

Output

["No","Yes"]

The PHP object is now changed to a PHP array. You may not expect it.

Encode -> Decode -> Encode

The above will not return the data to its original form.

The output of decoding to PHP arrays and encoding from PHP arrays are not always symmetrical. But, the output of decoding from stdClass objects and encoding to stdClass objects are always symmetrical.

So if you have plans to do cyclical conversion between the PHP array and a JSON string, then first convert the PHP array to an object. The convert the JSON.

View demo

↑ Back to Top

Posted on Leave a comment

6 Ways to Remove Python List Elements

5/5 – (2 votes)

Problem Formulation and Solution Overview

This article will show you how 6 ways to remove List elements in Python.

To make it more interesting, we have the following running scenario:

Suppose you have a Christmas List containing everyone to buy a gift for. Once a gift is purchased, remove this person from the List. Once all gifts have been purchased, remove the entire List.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']

💬 Question: How would we write code to remove items from a Python List?

We can accomplish this task by one of the following options:


Method 1: Use the del Keyword

This method uses Python’s del Keyword and highlights its ability to remove one List element and all List elements.

Remove One List Element

In this scenario, Asa's gift has been purchased and will be removed from the List.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
del xmas_list[3]
print(xmas_list)

As shown on the highlighted line, Asa is removed from the List by using del, referencing xmas_list and specifying Asa’s location ([3]).

When xmas_list is output to the terminal, the following displays.

['Anna', 'Elin', 'Inger', 'Sofie', 'Gunnel', 'Linn']

Remove All List Elements

In this scenario, all gifts have been purchased, and all List elements will be removed.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
del xmas_list
print(xmas_list)

As shown on the highlighted line, all elements of xmas_list are removed by using del and referencing xmas_list.

When xmas_list is output to the terminal, the following error is generated.

NameError: name 'xmas_list' is not defined

💡Note: This error is generated because the variable xmas_list no longer exists in memory.

YouTube Video

Method 2: Use remove() and a For Loop

This example uses the remove() function in conjunction with a for loop to remove one List element and all List elements.

Remove One List Element

In this scenario, Elin's gift has been purchased and will be removed from the List.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
xmas_list.remove('Elin')
print(xmas_list)

As shown on the highlighted line, Elin is removed from the List using the remove() function and passing Elin’s name as an argument.

When xmas_list is output to the terminal, the following displays.

['Anna', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']

Remove All List Elements

In this scenario, all gifts have been purchased, and all List elements will be removed.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
for item in xmas_list.copy(): xmas_list.remove(item)
print(xmas_list)

As shown on the first highlighted line, a for loop is instantiated. This loop declares a shallow copy of the List to iterate.

On each iteration, the remove() function is called and passed the current name in xmas_list as an argument (see below) and removed.

For example:

Anna
Elin
Inger
Asa
Sofie
Gunnel
Linn

When xmas_list is output to the terminal, an empty List displays.

[]

💡Note: A shallow copy creates a reference to the original List. For further details, view the video below.

YouTube Video

Method 3: Use slicing

This method uses slicing to remove one List element and all List elements.

Remove One List Element

In this scenario, Anna's gift has been purchased and will be removed from the List.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
xmas_list = xmas_list[1:]
print(xmas_list)

As shown on the highlighted line, Anna is removed from the List using slicing.

When xmas_list is output to the terminal, the following displays.

['Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']

Remove All List Elements

In this scenario, all gifts have been purchased, and all List elements will be removed.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
xmas_list = []
print(xmas_list)

As shown on the highlighted line, all List elements are removed by declaring an empty List.

When xmas_list is output to the terminal, an empty List displays.

[]
YouTube Video

Method 4: Use pop()

This method uses the pop() function to remove one List element and all List elements.

Remove One List Element

In this scenario, Linn's gift has been purchased and will be removed from the List.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
xmas_list.pop()
xmas_list.pop(2)
print(xmas_list)

As shown on the first highlighted line, the pop() method is appended to the xmas_list. This lets Python know to remove a List element from said List. Since no element is specified, the last element is removed (Linn).

On the second highlighted line, the pop() method is appended to the xmas_list and passed one (1) argument: the element to remove (2). This action removes Inger.

When xmas_list is output to the terminal, the following displays.

['Anna', 'Elin', 'Asa', 'Sofie', 'Gunnel']

💡Note: Both Linn and Inger are no longer in xmas_list.

Remove All List Elements

In this scenario, all gifts have been purchased, and all List elements will be removed.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn'] for i in xmas_list.copy(): xmas_list.pop()
print(xmas_list)

As shown on the first highlighted line, a for loop is instantiated. This loop declares a shallow copy of the List to iterate.

On each iteration, the pop() method is called. Since no argument is passed, the last element is removed.

When xmas_list is output to the terminal, an empty List displays.

[]
YouTube Video

Method 5: Use List Comprehension

This method uses List Comprehension to remove all List elements that do not meet the specified criteria.

Remove One List Element

In this scenario, Gunnel's gift has been purchased and will be removed from the List.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
xmas_list = [value for value in xmas_list if value != 'Gunnel']
print(xmas_list)

When xmas_list is output to the terminal, the following displays.

['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Linn']

💡Note: To remove all List elements, pass it empty brackets as shown follows: (xmas_list = []).

YouTube Video

Method 6: Use clear()

This method uses clear() to remove all List elements.

In this scenario, all gifts have been purchased, and all List elements will be removed.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
xmas_list.clear()
print(xmas_list)

As shown on the highlighted line, all List elements are removed by appending the clear() function to xmas_list.

When xmas_list is output to the terminal, an empty List displays.

[]
YouTube Video

Summary

This article has provided six (6) ways to remove List elements to select the best fit for your coding requirements.

Good Luck & Happy Coding!


Programming Humor

💡 Programming is 10% science, 20% ingenuity, and 70% getting the ingenuity to work with the science.

~~~

  • Question: Why do Java programmers wear glasses?
  • Answer: Because they cannot C# …!

Feel free to check out our blog article with more coding jokes. 😉

Posted on Leave a comment

Convert JavaScript Object to JSON String

by Vincy. Last modified on October 26th, 2022.

JSON string conversion on the client and server side is an important requirement in data handling. Most programming languages contain native functions for handling JSON objects and string data.

The JSON format is a convenient way of structuring, transmitting, or logging hierarchical data. The JSON string is a bundled unit to transmit object properties over the API terminals.

In this tutorial, we will see how to convert a JavaScript object to a JSON string. The JSON.stringify() of the JS script is used to do this. This is a quick solution for converting the given JS object to a JSON.

Quick example

var jsObject = { "name": "Lion", "type": "wild"
};
var jsonString = JSON.stringify(jsObject)
console.log(jsonString);

Output

{"name":"Lion","type":"wild"}

javascript object to string

About JavaScript JSON.stringify()

The JSON.stringify() method accepts 3 parameters to convert JavaScript objects into JSON string. See the syntax and the possible parameters of this JavaScript method.

Syntax

JSON.stringify(value)
JSON.stringify(value, replacer)
JSON.stringify(value, replacer, space)

The replacer and space parameters are optional.

  • value – The JS object to be converted to a JSON string.
  • replacer – a  function or an array of specifications to convert JavaScript objects.
  • space – It is a specification used to format or prettify the output JSON.

The JSON.stringify() method can also accept JavaScript arrays to convert into JSON strings.

How to get a formatted JSON string from a JavaScript object

This example supplies the “space” parameter to the JSON.stringify method. This parameter helped to format the JSON string as shown in the output below the program.

When we see the PHP array to JSON conversion example, it used the PHP bitmask parameter to achieve prettyprinting of JSON output.

var jsObject = { "name": "Lion", "type": "wild"
}; // this is to convert a JS object to a formatted JSON string
var formattedJSON = JSON.stringify(jsObject, null, 2);
console.log(formattedJSON);

Output

{ "name": "Lion", "type": "wild"
}

How to store JSON string to a JavaScript localStorage

The localStorage is a mechanism to have persistent data or state on the client side. It accepts string data to be stored with a reference of a user-defined key.

In this example, we used this storage tool to keep the JSON string of cart session data.

This code pushes two records to the cart array. Then, it converts the array into the JSON string to put it into the localStorage.

Note: JSON.stringify() can also accepts array to convert into a JSON string.

We have already used this storage mechanism to create a JavaScript persistent shopping cart.

const cart = { cartItem: []
};
cart.cartItem.push({ product: "Watch", quantity: 3, unitPrice: 100 });
cart.cartItem.push({ product: "Smart Phone", quantity: 5, unitPrice: 600 }); // use case for converting JS object to a JSON string
// convert object to JSON string before storing in local storage
const cartJSONString = JSON.stringify(cart); localStorage.setItem("cartSession", JSON.stringify(cartJSONString)); // retrieving from local storage
let cartFromStorage = localStorage.getItem("cartSession");
const getCartItemFromSession = JSON.parse(cartFromStorage); console.log(getCartItemFromSession);

Output

{ "cartItem": [ {"product":"Watch","quantity":3,"unitPrice":100}, {"product":"Smart Phone","quantity":5,"unitPrice":600} ]
}

How dates in the JavaScript object behave during JSON stringify

The JSON.stringify() function converts the JavaScript Date object into an equivalent date string as shown in the output.

The code instantiates the JavaScript Date() class to set the current date to a JS object property.

// when you convert a JS object to JSON string, date gets automatically converted
// to equivalent string form
var jsObject = { "name": "Lion", "type": "wild", today: new Date()
};
const jsonString = JSON.stringify(jsObject);
console.log(jsonString);

Output

{"name":"Lion","type":"wild","today":"2022-10-23T10:58:55.791Z"}

How JSON stringify converts the JavaScript objects with functions

If the JS object contains functions as a value of a property, the JSON.stringify will omit the function. Then, it will return nothing for that particular property.

The resultant JSON string will have the rest of the properties that have valid mapping.

// when you convert a JS object to JSON string, // functions in JS object is removed by JSON.stringify var jsObject = { "name": "Lion", "type": "wild", age: function() { return 10; }
};
const jsonString = JSON.stringify(jsObject);
console.log(jsonString);

Output

{"name":"Lion","type":"wild"}

JavaScript toString() limitations over JSON.stringify: 

If the input JavaScript object contains a single or with a predictable structure, toString() can achieve this conversion.

It is done by iterating the JS object array and applying stringification on each iteration. Example,

let jsonString = { 'name': 'Lion', type: 'wild', toString() { return '{name: "${this.name}", age: ${this.type}}'; }
};
console.log(jsonString);

But, it is not an efficient way that has the probability of missing some properties during the iteration.

Why and how to convert the JSON string into a JSON object

The JSON string is a comfortable format during data transmission and data logging. Other than that it must be in a format of an object tree to parse, read from, and write to the JSON.

The JSON.parse() method is used to convert JSON String to a JSON Object. A JSON object will look like a JS object only. See the following comparison between a JS object and a JSON object.

//JavaScript object
const jsObject = { 'animal-name': 'Lion', animalType: 'wild', endangered: false
} //JSON object
{ "animal-name": "Lion", "animalType": "wild", "endangered": false
}

Download

↑ Back to Top

Posted on Leave a comment

Python | Split String by Whitespace

Rate this post

Summary: Use "given string".split() to split the given string by whitespace and store each word as an individual item in a list.
Minimal Example:
print("Welcome Finxter".split())
# OUTPUT: [‘Welcome’, ‘Finxter’]

Problem Formulation

Problem: Given a string, How will you split the string into a list of words using whitespace as a separator/delimiter?

Let’s understand the problem with the help of a few examples:

Example 1:
Input: text = “Welcome to the world of Python”
Explanation: Split the string into a list of words using a space ” ” as the delimiter to separate the words from the given string.
Output:
[‘Welcome’, ‘to’, ‘the’, ‘world’, ‘of’, ‘Python’]

Example 2:
Input:
text = “””Item_1
Item_2
Item_3″””
print(text.split(‘\n’))
Explanation: Split the string into a list of words using a newline “\n” as the delimiter to separate the words from the given string.
Output: [‘Item_1’, ‘Item_2’, ‘Item_3’]

Example 3:
text = “This is just a random text:\n New Line”
Explanation: The given string contains a combination of whitespaces between the words, such as space, multiple-spaces, a tab and a new line character. All of these whitespace characters have to be considered as delimiters while separating the words from the given string and storing them as items in a list. Here’s how the output looks:
Output:
[‘This’, ‘is’, ‘just’, ‘a’, ‘random’, ‘text:’, ‘New’, ‘Line’]

So, we have two situations at hand. One, that has a single whitespace used as a delimiter and another that has multiple whitespace characters as delimiters in the same string. Let’s dive into the numerous ways of solving this problem.

Method 1: Using split()

split() is a built-in method in Python which splits the string at a given separator and returns a split list of substrings. Here’s a minimal example that demonstrates how the split function works – finxterx42'.split('x') will split the string with the character ‘x’ as the delimiter and return the following list as an output: ['fin', 'ter', '42']. The default separator, i.e., when no value is passed to the split function is considered as any whitespace character, i.e., it will take into account any whitespace such as ‘\n’, ” “, ‘\t’, etc.

Read more about the split() method in this blog tutorial: Python String split().

Approach: Thus to split a string based on a given whitespace delimiter, you can simply pass the specific whitespace character as a separator/delimiter to the split('whitespace_character') function.

Code:

# Example 1:
text = "Welcome to the world of Python"
print(text.split(' '))
# OUTPUT: ['Welcome', 'to', 'the', 'world', 'of', 'Python'] # Example 2:
text = """Item 1
Item 2
Item 3"""
print(text.split('\n'))
# OUTPUT: ['Item_1', 'Item_2', 'Item_3'] # Example 3: text = "This is just a\trandom text:\nNew Line"
print(text.split()) # OUTPUT: ['This', 'is', 'just', 'a', 'random', 'text:', 'New', 'Line']

Note that to separate the words in the third example we did specify any separator within the split() function. This is because when you don’t specify the separator, then Python will automatically consider that any whitespace character that occurs within the given string is a separator.

Method 2: Using regex

Another extremely handy way of separating a string with whitespace characters as separators is to use the regex library.

Approach 1: Import the regex library and use its split method as re.split('\s+', text) where ‘\s+’ returns a match whenever the string contains one or more whitespace characters. Therefore, whenever any whitespace character is encountered, the string will be separated at that point.

Code:

import re
# Example 1:
text = "Welcome to the world of Python"
print(re.split('\s+', text))
# OUTPUT: ['Welcome', 'to', 'the', 'world', 'of', 'Python'] # Example 2:
text = """Item_1
Item_2
Item_3"""
print(re.split('\s+', text))
# OUTPUT: ['Item_1', 'Item_2', 'Item_3'] # Example 3:
text = "This is just a\trandom text:\nNew Line"
print(re.split('\s+', text))
# OUTPUT: ['This', 'is', 'just', 'a', 'random', 'text:', 'New', 'Line']

Related Tutorial: Python Regex Split

Approach 2: Another way of using the regex library to solve this question is to use the findall() method of the regex library. Import the regex library and use re.findall(r'\S+', text) where the expression returns all the characters/words in a list that do not contain any whitespace character. This essentially means that whenever Python finds and segregates a string that has no whitespace in it. As soon as a whitespace character is found it considers that as a breakpoint, therefore the next word that has a continuous sequence of characters without the presence of any whitespace character is taken into account.

Here’s a graphical representation of the above explanaton:

Code:

import re
# Example 1:
text = "Welcome to the world of Python"
print(re.findall(r'\S+', text))
# OUTPUT: ['Welcome', 'to', 'the', 'world', 'of', 'Python'] # Example 2:
text = """Item_1
Item_2
Item_3"""
print(re.findall(r'\S+', text))
# OUTPUT: ['Item_1', 'Item_2', 'Item_3'] # Example 3:
text = "This is just a random text:\n New Line"
print(re.findall(r'\S+', text))
# OUTPUT: ['This', 'is', 'just', 'a', 'random', 'text:', 'New', 'Line']

Related Tutorial: Python re.findall() – Everything You Need to Know

Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Conclusion

We have successfully solved the given problem using different approaches. I hope you enjoyed this article and it helps you in your Python coding journey. Please subscribe and stay tuned for more interesting articles!

Related Reads:
⦿ How To Split A String And Keep The Separators?
⦿
 How To Cut A String In Python?
⦿ Python | Split String into Characters


Python Regex Course

Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages.  

Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages

Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions.  Regular expressions ​rule the game ​when text processing ​meets computer science. 

If you want to become a regular expression master too, check out the most comprehensive Python regex course on the planet:

Posted on Leave a comment

Solidity Bytes and String Arrays, Concat, Allocating Memory, and Array Literals

Rate this post
YouTube Video

💡 With this article, we’ll discover a new and fascinating world of bytes and strings, as well as ways to manipulate them, allocate memory arrays, and use array literals.

It’s part of our long-standing tradition to make this (and other) articles a faithful companion, or a supplement to the official Solidity documentation, starting with these docs for this article’s topics.

Types bytes and string

Besides the arrays we’ve already discussed, there are also some unique arrays, such as bytes and string arrays.

We have to note that the bytes type is very similar to bytes1[], however, the difference is that a bytes array is tightly packed in memory areas calldata and memory.

Furthermore, string is equal to bytes, but does not have a length property or support for index access.

Solidity doesn’t have string manipulation functions compared to other commonly used programming languages, but this can be worked around by including third-party string libraries.

With vanilla Solidity, we can concatenate two strings, e.g. string.concat(s1, s2), and compare two strings by using their keccak-256 hash, e.g.

keccak256(abi.encodePacked(s1)) == keccak256(abi.encodePacked(s2)).

Regarding the preferred use (we could consider this a design pattern), the bytes type is better than bytes1[], because bytes1[] is more expensive due to padding additional 31 bytes between the elements when used in memory.

The padding is absent in storage because of the tight packing used (docs).

👍 Note: A rule of thumb says that bytes should be used for arbitrary-length raw byte data and string for arbitrary-length string data in UTF-8.

💡 Note: If our data can be stored in a variable containing a number of bytes up to 32, it is better to use one of the value types bytes1 ... bytes32, due to their low cost.

To access a byte representation of a string s, we could use the following construct: bytes(s)[7] = 'x'; with regard to the string length, bytes(s).length, e.g.

// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; /** * @title String modification * @dev Demonstrates how to modify a string represented as bytes. */
contract StringModification { string public s = "Some string"; function modifyString() public { bytes(s)[7]='Q'; }
}

💡 Note: By using this approach, we’re accessing bytes of the UTF-8 representation, not the individual characters.

Functions bytes.concat() and string.concat()

Concatenation is a synonym for joining or gluing together.

🌍 Recommended Tutorial: String Concatenation in Solidity

String Concatenation

The function string.concat() enables us to concatenate any number of string values.

The result of using the string.concat() function is a single-string memory array containing the concatenated strings without any added spacing or padding.

If we’d like to use function parameters of other types that are not implicitly convertible to the string type, we first have to convert them to the string type.

Byte Concatenation

In the same manner, the bytes.concat() function enables us to concatenate any number of bytes or bytes1 ... bytes32 values.

The function result is a single bytes memory array containing the arguments without padding.

If we’d like to use string parameters or other types not implicitly convertible to bytes type, we first convert them to the bytes type.

Example

Let’s use an example to show how a function performs both string and bytes concatenation:

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12; contract C { string s = "Storage"; function f(bytes calldata bc, string memory sm, bytes16 b) public view { string memory concatString = string.concat(s, string(bc), "Literal", sm); assert((bytes(s).length + bc.length + 7 + bytes(sm).length) == bytes(concatString).length); bytes memory concatBytes = bytes.concat(bytes(s), bc, bc[:2], "Literal", bytes(sm), b); assert((bytes(s).length + bc.length + 2 + 7 + bytes(sm).length + b.length) == concatBytes.length); }
}

By calling bytes.concat(...) and string.concat(...) without arguments, a result is an empty array.

Allocating Memory Arrays

We can dynamically resize the storage arrays by adding elements via the .push() member function.

In contrast, memory arrays cannot be dynamically resized and the .push() member function is not available.

However, by using the alternative approach, we can create dynamic-length memory arrays by using the new operator. Just before using the new operator, we have to calculate the required size in advance or create a new, empty array and populate it by copying all elements.

💡 Note: Following the same rule of default values, the elements of freshly allocated arrays are initialized with their default values (docs).

Here we have an example showing arrays a and b, initialized by either a constant size or a parameter-given size.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0; contract C { function f(uint len) public pure { uint[] memory a = new uint[](7); bytes memory b = new bytes(len); assert(a.length == 7); assert(b.length == len); a[6] = 8; }
}

Array Literals

Array literal is represented by a comma-separated list of any number of expressions, which are listed in square brackets, e.g. [1, a, f(3)].

The array literal type is determined in the following way:

  1. The array literal is a statically-sized memory array, and its length is the number of expressions listed in the brackets;
  2. The base type of the array is determined by the type of the first expression T in the list that satisfies the condition: all other expressions must be implicitly convertible to T. If it’s not possible to find such an expression, a type error is thrown;
  3. Besides the convertibility condition (point 2.), one of the expressions must be of the T type.

The following example will clarify what the points above mean; the type of an array literal [1, 2, 3] is uint8[3] memory, because each of the expressions is of type uint8.

If we want to change the result to type uint[3] memory, we have to convert the first element to uint.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0; contract C { function f() public pure { g([uint(1), 2, 3]); } function g(uint[3] memory) public pure { // ... }
}

In contrast, the array literal [1, -2] is invalid because it doesn’t comply with point 2., stating that the first expression’s type is a target type T for implicit conversion of other expressions.

Since our first expression is of type uint8, and the second expression is of type int8 (including the negative numbers), the second expression cannot be implicitly converted to uint8.

To avoid a type error, we can declare our array literal as [int8(1), -1], forcing the first expression to be of compatible type int8.

In a more specific case of using, e.g. two-dimensional array literals, we’d step on a problem of fixed-size memory arrays that cannot be converted into each other, regardless of the compatibility of base types.

We can get around this problem by explicitly specifying a common base:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0; contract C { function f() public pure returns (uint24[2][4] memory) { uint24[2][4] memory x = [[uint24(0x1), 1], [0xffffff, 2], [uint24(0xff), 3], [uint24(0xffff), 4]]; // The following does not work, because some of the inner arrays are not of the right type. // uint[2][4] memory x = [[0x1, 1], [0xffffff, 2], [0xff, 3], [0xffff, 4]]; return x; }
}

We cannot assign fixed-size memory arrays to dynamically-sized memory arrays, as shown by the example:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.0 <0.9.0; // This will not compile.
contract C { function f() public { // The next line creates a type error because uint[3] memory // cannot be converted to uint[] memory. uint[] memory x = [uint(1), 3, 4]; }
}

To initialize dynamically-sized arrays, we’d have to resort to assigning the elements individually, as in the example:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0; contract C { function f() public pure { uint[] memory x = new uint[](3); x[0] = 1; x[1] = 3; x[2] = 4; }
}

Conclusion

In this article, we learned even more about reference types, in particular, bytes and string arrays and concatenation, memory array allocation, and array literals.

  1. First, we explained the uniqueness of the arrays based on bytes and string types, and also touched on some of the similarities with the akin types.
  2. Second, we’ve peeked into how to do string concatenation, comparison, and bytes concatenation.
  3. Third, we discovered the specifics of allocating memory arrays and got introduced to the new operator.
  4. Fourth, we got to know array literals with rules for determining the array literal base type. We also became aware of the invalid array literals and what can be done to make them valid.

What’s Next?

This tutorial is part of our extended Solidity documentation with videos and more accessible examples and explanations. You can navigate the series here (all links open in a new tab):

Posted on Leave a comment

How to Convert Pandas DataFrame/Series to NumPy Array?

5/5 – (1 vote)

💬 Programming Challenge: Given a Pandas DataFrame or a Pandas Series object. How to convert them to a NumPy array?

How to Convert Pandas DataFrame/Series to NumPy Array?

In this short tutorial, you’ll learn (1) how to convert a 1D pandas Series to a NumPy array, and (2) how to convert a 2D pandas DataFrame to an array. Let’s get started with the first! 👇

Convert Pandas Series to NumPy Array

First, let’s create a Pandas Series.

import pandas as pd # create dataframe df df = pd.Series([22,21,20,14], name= 'GSTitles', index= ['Nadal','Djokovic','Federer','Sampras'])
print(df)

Here’s the resulting Series df:

Nadal 22
Djokovic 21
Federer 20
Sampras 14
Name: GSTitles, dtype: int64

Now that we have our Pandas Series, you can convert this to a NumPy Array using the DataFrame.to_numpy() method.

Like so:

print(df.to_numpy())
# [22 21 20 14]

The resulting object is a NumPy array:

print(type(df.to_numpy()))
# <class 'numpy.ndarray'>

⚡ Attention: There is also the .values() method, but that is being deprecated now – when you look at the Pandas documentation, there is a warning “We recommend using DataFrame.to_numpy instead”.

With this method, only the values in the DataFrame or Series will return. The index labels will be removed.

Here’s how that’ll work:

print(df.values)
# [22 21 20 14]

This was a 1-dimensional array or a Series. Let’s move on to the 2D case next. 👇👇👇

Convert DataFrame to NumPy Array

💬 Question: Let’s try with a two-dimensional DataFrame — how to convert it to a NumPy array?

First, let’s print the dimension of the previous Series to confirm that it was, indeed, a 1D data structure:

print(df.ndim)
# 1

Next, you create a 2D DataFrame object:

import pandas as pd # Create a 2D DataFrame object
df2 = pd.DataFrame(data={'Nadal': [2, 14, 2, 4], 'Djokovic': [9, 2, 7, 3], 'Federer': [6, 1, 8, 5], 'Sampras': [2, 0, 7, 5]}, index=['AO', 'F', 'W', 'US']) print(df2)

Here’s the resulting DataFrame:

Nadal Djokovic Federer Sampras
AO 2 9 6 2
F 14 2 1 0
W 2 7 8 7
US 4 3 5 5

Now, let’s dive into the conversion of this DataFrame to a NumPy array by using the DataFrame.to_numpy() method.

# Convert this DataFrame to a NumPy array
print(df2.to_numpy())

The output shows a NumPy array from the 2D DataFrame — great! 👾

[[ 2 9 6 2] [14 2 1 0] [ 2 7 8 7] [ 4 3 5 5]]

You can see that all indexing metadata has been stripped away from the resulting NumPy array!

Convert Specific Columns from DataFrame to NumPy Array

You can also convert specific columns of a Pandas DataFrame by accessing the columns using pandas indexing and calling the .to_numpy() method on the resulting view object.

Here’s an example:

print(df2[['Djokovic', 'Federer']].to_numpy())

The output:

[[9 6] [2 1] [7 8] [3 5]]

Summary

You can convert a Pandas DataFrame or a Pandas Series object to a NumPy array by means of the df.to_numpy() method. The indexing metadata will be removed.

You can also convert specific columns of a Pandas DataFrame by accessing the columns using pandas indexing and calling the .to_numpy() method on the resulting view object.


Thanks for reading through the whole tutorial! 🙂

Posted on Leave a comment

What’s the Difference Between return and break in Python?

5/5 – (1 vote)

💬 Question: What is the difference between return and break? When to use which?

Let’s first look at a short answer before we dive into a simple example to understand the differences and similarities between return and break.

Comparison

Both return and break are keywords in Python.

  • The keyword return ends a function and passes a value to the caller.
  • The keyword break ends a loop immediately without doing anything else. It can be used within or outside a function.
return break
Used to end a function Used to end a for or while loop
Passes an optional value to the caller of the function (e.g., return 'hello') Doesn’t pass anything to the “outside”

While they serve a different purpose, i.e., ending a function vs ending a loop, there are some cases where they can be used interchangeably.

Similar Use Cases

The following use case shows why you may have confused both keywords return and break. In both cases, you can use them to end a loop inside a function and return to the outside.

Here’s the variant using return:

def f(): for i in range(10): print(i) if i>3: return f()

And here’s the variant using break:

def f(): for i in range(10): print(i) if i>3: break f()

Both code snippets do exactly the same—printing out the first 5 values 0, 1, 2, 3, and 4.

Output:

0
1
2
3
4

However, this is where the similarity between those two keywords ends. Let’s dive into a more common use case where they both perform different tasks in the code.

Different Use Cases

The following example uses both keywords break and return. It uses the keyword break to end the loop as soon as the loop variable i is greater than 3.

So the line print(i) is never executed after variable i reaches the value 4—the loop ends.

But the function doesn’t end because break only ends the loop and not the function. That’s why the statement print('hi') is still executed, and the return value of the function is 42 (which we also print in the final line).

def f(): for i in range(10): if i>3: break print(i) print('hi') return 42 print(f())

Output:

0
1
2
3
hi
42

Summary

The keyword return is different and more powerful than the keyword break because it allows you to specify an optional return value. But it can only be used in a function context and not outside a function.

  • You use the keyword return to give back a value to the caller of the function or terminate the whole function.
  • You use the keyword break to immediately stop a for or while loop.

🐍 Rule: Only if you want to exit a loop inside a function and this would also exit the whole function, you can use both keywords. In that case, I’d recommend using the keyword return instead of break because it gives you more degrees of freedom, i.e., specifying the return value. Plus, it is more explicit which improves the readability of the code.


Thanks for reading over the whole tutorial—if you want to keep learning, feel free to join my email academy. It’s fun! 🙂

Recommended Video

YouTube Video
Posted on Leave a comment

PHP Array to JSON String Convert with Online Demo

by Vincy. Last modified on October 22nd, 2022.

JSON is the best format to transfer data over network. It is an easily parsable format comparatively. That’s why most of the API accepts parameters and returns responses in JSON.

There are online tools to convert an array to a JSON object. This tutorial teaches how to create a program to convert various types of PHP array input into a JSON format.

It has 4 different examples for converting a PHP array to JSON. Those are too tiny in purpose to let beginners understand this concept easily.

Quick example

This quick example is simply coded with a three-line straightforward solution. It takes a single-dimensional PHP array and converts it to JSON.

<?php
$array = array(100, 250, 375, 400);
$jsonString = json_encode($array);
echo $jsonString;
?>

View Demo

The other different array-to-JSON examples handle simple to complex array conversion. It also applies pre-modification (like array mapping) before conversion. The four examples are,

  1. Simple to complex PHP array to JSON.
  2. Remove array keys before converting to JSON.
  3. Convert PHP array with accented characters to JSON
  4. PHP Array to JSON with pretty-printing

If you want the code for the reverse to decode JSON objects to an array, then the linked article has examples.

See this online demo to convert an array of comma-separated values into a JSON object.

php array to json

1) Simple to complex PHP array to JSON

This code handles 3 types of array data into a JSON object. In PHP, it is very easy to convert an array to JSON.

It is a one-line code by using the PHP json_encode() function.

<?php
// PHP Array to JSON string conversion for
// simple, associative and multidimensional arrays
// all works the same way using json_encode
// just present different arrays for example purposes only // simple PHP Array to JSON string
echo '<h1>PHP Array to JSON</h1>';
$array = array( 100, 250, 375, 400
);
$jsonString = json_encode($array);
echo $jsonString; // Associative Array to JSON
echo '<h2>Associative PHP Array to JSON</h2>';
$array = array( 'e1' => 1000, 'e2' => 1500, 'e3' => 2000, 'e4' => 2350, 'e5' => 3000
);
$jsonString = json_encode($array);
echo $jsonString; // multidimensional PHP Array to JSON string
echo '<h2>Multidimensional PHP Array to JSON</h2>';
$multiArray = array( 'a1' => array( 'item_id' => 1, 'name' => 'Lion', 'type' => 'Wild', 'location' => 'Zoo' ), 'a2' => array( 'item_id' => 2, 'name' => 'Cat', 'type' => 'Domestic', 'location' => 'Home' )
);
echo json_encode($multiArray);
?>

Output:

//PHP Array to JSON
[100,250,375,400] //Associative PHP Array to JSON
{"e1":1000,"e2":1500,"e3":2000,"e4":2350,"e5":3000} //Multidimensional PHP Array to JSON
{"a1":{"item_id":1,"name":"Lion","type":"Wild","location":"Zoo"},"a2":{"item_id":2,"name":"Cat","type":"Domestic","location":"Home"}}

2) Remove array keys before converting to JSON

This code handles a different scenario of JSON conversion which must be helpful if needed. For example, if the array associates subject=>marks and the user needs only the marks to plot it in a graph.

It removes the user-defined keys from an associative array and applies json_encode to convert it. It is a two-step process.

  1. It applies PHP array_values() to read the value array.
  2. Then, it applies json_encode on the values array.
<?php
// array_values() to remove assigned keys and convert to the original PHP Array key
echo '<h1>To remove assigned associative keys and PHP Array to JSON</h1>';
$array = array( 'e1' => 1000, 'e2' => 1500, 'e3' => 2000, 'e4' => 2350, 'e5' => 3000
); $jsonString = json_encode(array_values($array));
echo $jsonString;
?>

Output:

[1000,1500,2000,2350,3000]

3) Convert the PHP array with accented characters to JSON

It is also a two-step process to convert the array of data containing accented characters.

It applies UTF8 encoding on the array values before converting them into a JSON object.

For encoding all the elements of the given array, it maps the utf8_encode() as a callback using the PHP array_map() function.

We have seen PHP array functions that are frequently used while working with arrays.

<?php
// Accented characters
// to preserve accented characters during PHP Array to JSON conversion
// you need to utf8 encode the values and then do json_encode
echo '<h1>For accented characters PHP Array to JSON</h1>';
$array = array( 'w1' => 'résumé', 'w2' => 'château', 'w3' => 'façade', 'w4' => 'déjà vu', 'w5' => 'São Paulo'
);
$utfEncodedArray = array_map("utf8_encode", $array);
echo json_encode($utfEncodedArray);
?>

Output:

{"w1":"r\u00c3\u00a9sum\u00c3\u00a9","w2":"ch\u00c3\u00a2teau","w3":"fa\u00c3\u00a7ade","w4":"d\u00c3\u00a9j\u00c3\u00a0 vu","w5":"S\u00c3\u00a3o Paulo"}

4) PHP Array to JSON with pretty-printing

It applies to prettyprint on the converted output JSON properties in a neet spacious format.

The PHP json_encode() function accepts the second parameter to set the bitmask flag. This flag is used to set the JSON_PRETTY_PRINT to align the output JSON properties.

<?php
// to neatly align the output with spaces
// it may be useful when you plan to print the
// JSON output in a raw format
// helpful when debugging complex multidimensional PHP Arrays and JSON objects
// lot more constants are available like this, which might be handy in situations
echo '<h1>Convert PHP Array to JSON and Pretty Print</h1>';
$array = array( 'e1' => 1000, 'e2' => 1500, 'e3' => 2000, 'e4' => 2350, 'e5' => 3000
);
echo json_encode($array, JSON_PRETTY_PRINT);
?>

Output:

{ "e1": 1000, "e2": 1500, "e3": 2000, "e4": 2350, "e5": 3000 }

Download

↑ Back to Top

Posted on Leave a comment

Python | Split String into Characters

Rate this post

Summary: Use the list("given string") to extract each character of the given string and store them as individual items in a list.
Minimal Example:
print(list("abc"))

Problem: Given a string; How will you split the string into a list of characters?

Example: Let’s visualize the problem with the help of an example:

input = “finxter”
output = [‘f’, ‘i’, ‘n’, ‘x’, ‘t’, ‘e’, ‘r’]

Now that we have an overview of our problem let us dive into the solutions without further ado.

Method 1: Using The list Constructor

Approach: One of the simplest ways to solve the given problem is to use the list constructor and pass the given string into it as the input.

list() creates a new list object that contains items obtained by iterating over the input iterable. Since a string is an iterable formed by combining a group of characters, hence, iterating over it using the list constructor yields a single character at each iteration which represents individual items in the newly formed list.

Code:

text = "finxter"
print(list(text)) # ['f', 'i', 'n', 'x', 't', 'e', 'r']

💎Related Tutorial: Python list() — A Simple Guide with Video

Method 2: Using a List Comprehension

Another way to split the given string into characters would be to use a list comprehension such that the list comprehension returns a new list containing each character of the given string as individual items.

Code:

text = "finxter"
print([x for x in text]) # ['f', 'i', 'n', 'x', 't', 'e', 'r']

Prerequisite: To understand what happened in the above code, it is essential to know what a list comprehension does. In simple words, a list comprehension in Python is a compact way of creating lists. The simple formula is [expression + context], where the “expression” determines what to do with each list element. And the “context” determines what elements to select. The context can consist of an arbitrary number of for and if statements. To learn more about list comprehensions, head on to this detailed guide on list comprehensions.

Explanation: Well! Now that you know what list comprehensions are, let’s try to understand what the above code does. In our solution, the context variable x is used to extract each character from the given string by iterating across each character of the string one by one with the help of a for loop. This context variable x also happens to be the expression of our list comprehension as it stores the individual characters of the given string as separate items in the newly formed list.

Multi-line Solution: Another approach to formulating the above solution is to use a for loop. The idea is pretty similar; however, we will not be using a list comprehension in this case. Instead, we will use a for loop to iterate across individual characters of the given string and store them one by one in a new list with the help of the append method.

text = "finxter"
res = []
for i in text: res.append(i)
print(res) # ['f', 'i', 'n', 'x', 't', 'e', 'r']

Method 3: Using map and lambda

Yet another way of solving the given problem is to use a lambda function within the map function. Now, this is complex and certainly not the best fit solution to the given problem. However, it may (or may not ;P) be appropriate when you are handling really complex tasks. So, here’s how to use the two built-in Python functions to solve the given problem:

import re
text = "finxter"
print(list(map(lambda c: c, text))) # ['f', 'i', 'n', 'x', 't', 'e', 'r']

Explanation: The map() function is used to execute a specified function for each item of an iterable. In this case, the iterable is the given string and each character of the string represents an individual item within it. Now, all we need to do is to create a lambda function that simply returns the character passed to it as the input. That’s it! However, the map method will return a map object, so you must convert it to a list using the list() function. Silly! Isn’t it? Nevertheless, it works!

Conclusion

Hurrah! We have successfully solved the given problem using as many as three different ways. I hope you enjoyed this article and it helps you in your Python coding journey. Please subscribe and stay tuned for more interesting articles!

Related Reads:
⦿ How To Split A String And Keep The Separators?
⦿
How To Cut A String In Python?


Posted on Leave a comment

How to Return a File From a Function in Python?

5/5 – (1 vote)

Do you need to create a function that returns a file but you don’t know how? No worries, in sixty seconds, you’ll know! Go! 👇

A Python function can return any object such as a file object. To return a file, first open the file object within the function body, handle all possible errors if the file doesn’t exist, and return it to the caller of the function using the keyword operation return open(filename, mode='r').

Here’s a minimal example that tries to open a filename that was provided by the user via the input() function. If it fails, it prints an error message and asks for a different user input:

def open_file(): while True: filename = input('filename: ') try: return open(filename, mode='r') except: print('Error. Try again') f = open_file()
print(f.read()) 

If I type in the correct file right away, I get the following output when storing the previous code snippet in a file named code.py—the code reads itself (meta 🤯):

filename: code.py
def open_file(): while True: filename = input('filename: ') try: return open(filename, mode='r') except: print('Error. Try again') f = open_file()
print(f.read())

Note that you can open the file in writing mode rather than reading mode by replacing the line with the return statement with the following line:

open(filename, mode='w')

A more Pythonic way, in my opinion, is to follow the single-responsibility pattern whereby a function should do only one thing. In that case, provide the relevant input values into the function like so:

def open_file(filename, mode): try: return open(filename, mode=mode) except: return None def ask_user(): f = open_file(input('filename: '), input('mode: ')) while not f: f = open_file(input('filename: '), input('mode: ')) return f f = ask_user() print(f.read()) 

Notice how the file handling of a single instance and the user input processing are separated into two functions. Each function does one thing only. Unix style.


If you want to improve your programming skills and coding productivity creating massive success with your apps and coding projects, feel free to check out my book on the topic:


The Art of Clean Code

Most software developers waste thousands of hours working with overly complex code. The eight core principles in The Art of Clean Coding will teach you how to write clear, maintainable code without compromising functionality. The book’s guiding principle is simplicity: reduce and simplify, then reinvest energy in the important parts to save you countless hours and ease the often onerous task of code maintenance.

  1. Concentrate on the important stuff with the 80/20 principle — focus on the 20% of your code that matters most
  2. Avoid coding in isolation: create a minimum viable product to get early feedback
  3. Write code cleanly and simply to eliminate clutter 
  4. Avoid premature optimization that risks over-complicating code 
  5. Balance your goals, capacity, and feedback to achieve the productive state of Flow
  6. Apply the Do One Thing Well philosophy to vastly improve functionality
  7. Design efficient user interfaces with the Less is More principle
  8. Tie your new skills together into one unifying principle: Focus

The Python-based The Art of Clean Coding is suitable for programmers at any level, with ideas presented in a language-agnostic manner.


Related Tutorials

Programmer Humor

Q: How do you tell an introverted computer scientist from an extroverted computer scientist? A: An extroverted computer scientist looks at your shoes when he talks to you.