Amelia Earheart Amelia Earheart - 5 months ago 14
JSON Question

PHP- Convert more than one Json string into a single array

I have a result set which looks something like this:

enter image description here

For each entry, there can be more than one category and I'm trying to decode these JSON strings and save it in a single array. That means, all these categories will be decoded and saved in an array. This data is fetched from database.

This is the code I tried:

//$resultset contains the entire resultset
$resultset_JSON='[["first","second","third","fourth"],["fifth","sixth","seventh","eight","ninth"]‌​,["life","death","business" ,"editing","light"]]';

$data= array();
while($row=mysqli_fetch_array($resultset))
{
$data = json_decode($row['category']);
//$data[] = json_decode($row['category']);
}
print_r($data);


But I'm not getting the desired result. I'm only able to fetch the last row.

Can anyone help me figure this out?

This is the output I'm trying to get:

Array
(
[0] => first
[1] => second
[2] => third
[3] => fourth
[4] => fifth
[5] => sixth
[6] => seventh
[7] => eighth
[8] => ninth
[9] => life
[10] => death
[11] => business
[12] => editing
[13] => light
)

Answer

try this

$final_array = array();

while($row=mysqli_fetch_array($resultset))
{

  $temp_data       =   json_decode($row['category'],true); 
  $final_array     =   array_merge($final_array ,$temp_data);  

 }

 print_r($final_array );
Comments