Convert a PHP array to a string or JSON
Use implode() to display a list like pen, paper. Use json_encode() when another program needs to read the original values back.
Join an array with implode()
Pass the separator first and the array second. This example puts a comma and a space between the two labels.
$labels = ["pen", "paper"];
echo implode(", ", $labels);
pen, paper
explode(", ", "pen, paper") gives you back the two labels. A label like pen, blue would also be split. Use JSON or a CSV writer when a value can contain the separator. See the PHP manual for implode() and explode().
When PHP writes a JSON array or object
PHP writes a JSON array when its keys run in order from 0 with no gaps. Named keys or gaps produce a JSON object. If filtering leaves gaps in a list, call array_values() before encoding to reset the keys.
echo json_encode(["001", 2, true], JSON_THROW_ON_ERROR), PHP_EOL;
echo json_encode(["sku" => "001", "quantity" => 2], JSON_THROW_ON_ERROR);
["001",2,true]
{"sku":"001","quantity":2}
Read the JSON back with json_decode($json, true, 512, JSON_THROW_ON_ERROR). Passing true as the second argument tells PHP to read JSON objects as associative arrays. See the json_encode() reference.
To convert a list from a text file, paste it into ArrayCat and choose PHP array or JSON array.