0

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 
) 
6
  • Add [] at $data[]=json_decode($row['category']); Commented Jun 15, 2016 at 6:28
  • Tried that. JSON encoded output of the array if i do so is [["first","second","third","fourth"],["fifth","sixth","seventh","eight","ninth"],["life","death","business" ,"editing","light"]] @Saty Commented Jun 15, 2016 at 6:30
  • Okk use it as $data[] = $row['category']; and outside while loop json_decode($data) Commented Jun 15, 2016 at 6:33
  • Not working...tried it. @Saty Commented Jun 15, 2016 at 6:35
  • $data[]=json_decode($row['category'],true); and use array_merge Commented Jun 15, 2016 at 6:38

4 Answers 4

1

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 );
Sign up to request clarification or add additional context in comments.

Comments

1

You can use array_merge in your loop

http://php.net/manual/fr/function.array-merge.php

Comments

1
Please try this code:

$data= array();
$ds= array();
$f ='';
while($row=mysqli_fetch_array($resultset)){
{
    $data      =   json_decode($row['category']);   
    $f .= implode(',',$data);                       
}
if(!empty($f)) {
  $ds = explode(',',$f);
}
print_r($ds);

Comments

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

  $all_data = array();
  while($row=mysqli_fetch_array($resultset)){
      //Fetch array
      $data       =   json_decode($row['category']); 
      $all_data   =   array_merge($all_data,$data);                      
  }
  var_dump($all_data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.