Svedr Svedr - 4 months ago 34
JSON Question

Output Json Array inside a PHP array

I am having a slight issue in optimizing my code to output the values in the Json Array stored inside a PHP array.

This is the sample array

Array
(
[0] => Array
(
[0] => ["string 1", "string 2", "string 3"]
)

[1] => Array
(
[0] => ["string 4", "string 5", "string 6"]
)
.....
)


This is the code I am using and it works

$q = EstateTypes::query()->lists('column_value');

$array1 = json_decode($q[0], true);
$array2 = json_decode($q[1], true);
$array3 = json_decode($q[2], true);

foreach ($array1 as $key => $value) {
echo $value . "<br>";
}
foreach ($array2 as $key => $value) {
echo $value . "<br>";
}
foreach ($array3 as $key => $value) {
echo $value . "<br>";
}


I hope you see that the problem is this code is duplicating itself a lot. I've been trying to solve it properly but have been unable to. Would be glad for any help or further pointers on what I should do.

Answer
$q = EstateTypes::query()->lists('column_value');

foreach($q as $item){
    $array = json_decode($item, true);
    foreach ($array as $value) {
        echo $value . "<br>";
    }
}
Comments