0

i'm having problem on how to iterate in the array in json_encode.

I do ajax POST, in where i have a looping code that do "array push" in jQuery, something like this:

 $(this).closest('tr.header').nextUntil('.header').each(function(){
            i++;
            var forms="<form method='POST'>"+$(this).html()+'</form>';
            data.push($(forms).serializeArray());           
         });

So when i pass this to my controller/ other page, i do this:

  $dataList = json_encode($this->input->post("form"));  

  echo $dataList ;

And the output is:

 [
   [{"name":"category_1", "values":"packages"},
    {"name":"PK_1", "values": "1"}
   ],
   [{"name":"category_2", "value":"products"},
    {"name":"PK_2", "value": "3"}
   ]
 ]

I have tried to do :

  foreach ($dataList as $data) {                            
     echo $data . "\n";                                 
  }

But only give me error on foreach.

Thanks in advance.

3
  • You need to decode that: use json_decode($dataList) Commented Sep 19, 2015 at 8:23
  • In my 2nd statement, i already did the json_encode. Commented Sep 19, 2015 at 8:25
  • 1
    Sorry for that. Decode is what I mean Commented Sep 19, 2015 at 8:25

3 Answers 3

3

Use json_decode()function to get your data as an array then foreach your data.

  $a = '[
    [{"name":"category_1", "values":"packages"},
     {"name":"PK_1", "values": "1"}
    ],
    [{"name":"category_2", "value":"products"},
     {"name":"PK_2", "value": "3"}
    ]
  ]';
    echo '<pre>';
    $res = json_decode($a, true);

    $newArr = [];
    foreach($res as $data => $val)
    {
       foreach($val as $key2 => $val2)
       {
           $newArr[] = $val2;
       }
    }

    foreach($newArr as $key => $val)
    {
        echo 'Name = ' . $val['name'] . ' Values = ' . $val['value'] . '<br/>';
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Oh. My bad. Looks okay now . then how can i go through each items? Like the category_1, PK_1 >?
Can I see the print_r($data)?
Array ( [0]=>Array ( [name]=>category_1 [value]=>packages ) [1]=>Array ( [name]=>PK_1 [value]=>1 ) ) Array ( [0]=>Array ( [name]=>category_2 [value]=>product ) [1]=>Array ( [name]=>PK_2 [value]=>2 ) )
Yup. This solve my problem. Thank you :) But the last line ' Values =' .$val['values'] . Should only $val['value'] ;
1

Just decode the string and loop through the created array.

  <?php 
$a = '[
    [{"name":"category_1", "values":"packages"},
     {"name":"PK_1", "values": "1"}
    ],
    [{"name":"category_2", "value":"products"},
     {"name":"PK_2", "value": "3"}
    ]
  ]';
    echo '<pre>';
    $res = json_decode($a, true);

    $newArr = array();
    foreach($res as $data => $val)
    {

        foreach($val as $k=>$value){
            $value=array_values($value);
          echo 'Name => ' . $value[0] . ' Value => ' . $value[1] . '<br/>';
        }

    }


?>

Comments

1

for array output you need to decode it with json_decode()

here is sample code.

$encode_data = '[[{"name":"category_1", "values":"packages"},{"name":"PK_1", "values": "1"}],[{"name":"category_2", "value":"products"},{"name":"PK_2", "value": "3"}]]';

$dataAr = json_decode($encode_data , true);


foreach($dataAr as $data)
{   
    foreach($data as $value){
        $value=array_values($value);
        echo 'name => ' .$value[0] . ' value => ' .$value[1];
        echo "<br>";
    }

}

4 Comments

How can i get through item, for example just the "category"?
@user3651476 what you want to get name or value ??
Both, cause i need this to be inserted in database. Thanks
Im having the problem of "trying to get property of non-object".

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.