Why does the following code output 0 indices?
I want the indices to be: 0 1 2 3 4 .... How do I fix it?
Code:
foreach ($query->result() as $row){
$data = json_decode($row->residence,true);
foreach($data as $datum){
$newArray = array_chunk($datum['units'], 3);
foreach($newArray as $newA){
$output = array(implode(",",$newA));
echo print_r($output).'<br>'; //this is output
}
}
}
Output:
Array ( [0] => salam,11,11 ) 1
Array ( [0] => khobe,22,22 ) 1
Array ( [0] => salam,55,55 ) 1
Array ( [0] => khobe,66,66 ) 1
I want this output:
Array ( [0] => salam,11,11 ) 1
Array ( [1] => khobe,22,22 ) 1
Array ( [2] => salam,55,55 ) 1
Array ( [3] => khobe,66,66 ) 1
Update:
My JSON encoded in the database:
[{
"units": ["salam", "11", "11", "khobe", "22", "22"],
}, {
"units": ["salam", "55", "55", "khobe", "66", "66"],
}]
In the third foreach, you need to add the $output to a new array, then print_r after the loop.
E.g.
Or similar.
You might need to move the $count = 0;