Convert PHP Arrays to Strings

Let us talk about scripts, HTML, Perl, PHP, apache, etc.
User avatar
Stevyn
SysOp
Posts:1786
Joined:Mon Nov 09, 2009 10:03 am
Location:Japan
Contact:
Convert PHP Arrays to Strings

Post by Stevyn » Tue Feb 12, 2019 4:06 pm

Convert PHP Arrays to Strings

PHP's implode function returns a string consisting of array element values joined using a string that you specify:

Code: Select all

$ar = ['apple', 'orange', 'pear', 'grape'];
echo implode(', ', $ar);
// apple, orange, pear, grape
The string used to separate the array values is the first argument. If you leave out that argument and just pass the array you wish to join, the string between array elements defaults to an empty string:

Code: Select all

$ar = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
echo implode($ar); // abcdefg
If your arrays consist of values that are not strings, those values will be converted to strings for inclusion in the string returned by the implode function:

Code: Select all

$ar = [true, false, 0, 1, NULL, 1.42];
echo implode(', ', $ar); // 1, , 0, 1, , 1.42
Notice that numbers are accurately represented in the string returned by the implode function while true is converted to 1, and false and NULL display as empty strings.

Convert Array of Arrays to String
If the array you pass to the implode function contains elements that are arrays, Array will be the output for each sub-array:

Code: Select all

$food = [
    'fruits' => ['apple', 'raspberry', 'pear', 'banana'],
    'vegetables' => ['peas', 'carrots', 'cabbage'],
    'grains' => ['wheat', 'rice', 'oats']
];

echo implode(', ', $food);
// Array, Array, Array 
Also, an error will be logged or displayed: Notice: Array to string conversion.

An array consisting of a single level of sub-arrays, like the example above, could be converted to a string using the following function:

Code: Select all

function subArraysToString($ar, $sep = ', ') {
    $str = '';
    foreach ($ar as $val) {
        $str .= implode($sep, $val);
        $str .= $sep; // add separator between sub-arrays
    }
    $str = rtrim($str, $sep); // remove last separator
    return $str;
}

// $food array from example above
echo subArraysToString($food);
// apple, raspberry, pear, banana, peas, carrots, cabbage, wheat, rice, oats

Other Ways to Convert to String
PHP offers more ways to convert arrays and other values to strings: json_encode and serialize. First we demonstrate applying json_encode to the following array:

Code: Select all

$person = [
    'name' => 'Jon',
    'age' => 26,
    'status' => null,
    'friends' => ['Matt', 'Kaci', 'Jess']
];

echo json_encode($person);
// {"name":"Jon","age":26,"status":null,"friends":["Matt","Kaci","Jess"]}
Find out more about json_encode, JavaScript Object Notation, and the json_decode function that is used to restore the original PHP value.

Next we pass the same array to the serialize function:

Code: Select all

echo serialize($person);
// a:4:{s:4:"name";s:3:"Jon";s:3:"age";i:26;s:6:"status";N;s:7:"friends";a:3:{i:0;s:4:"Matt";i:1;s:4:"Kaci";i:2;s:4:"Jess";}} 
Find out more about PHP serialize and unserialize functions in the PHP Manual.

implode and explode
PHP's explode function does the opposite of implode: it converts a string to an array.

https://www.dyn-web.com/php/arrays/convert.php
Contact me directly: Ironfeatherbooks (@) gmail.com

Image

Post Reply