tim.baker
tim.baker

Reputation: 3307

POST Array through JSON foreach

The question:

I have this array coming through a POST field

{"name":"sample","email":"[email protected]","comments":"test"}

I want to split it apart and run it through an array, so the end result would be

name sample
email [email protected]
comments test

What I have tried is this:

$a = $_POST['rawRequest'];
$a = json_encode($a);
foreach ($a as $k => $v) {
    echo "\$a[$k] => $v <br />";
}

But it doesn't do anything, however when I test it with this variable (over using the POST)

$a = array("name" => 1,"email" => 2,"sample" => 3);

It works as expected.

Trying to understand what is going on

It is obviously because what I am dealing with here is two different types of array. However after endless google'ing I can't find anywhere which explains the difference (of the arrays below basically). So +1 to an explination which makes my relatively newbie mind understand what is happening and why it is wrong

{"name"=>"sample","email"=>"[email protected]"=>"comments":"test"}

{"name":"sample","email":"[email protected]","comments":"test"}

Upvotes: 2

Views: 434

Answers (4)

TURTLE
TURTLE

Reputation: 3847

I have Json decoded the encoded array and looped it through a foreach below with comments explaining what each part does.


/* The Json encoded array.*/
$json = '{"name":"sample","email":"[email protected]","comments":"test"}';

/* Decode the Json (back to a PHP array) */
$decode = json_decode($json, true);

/* Loop through the keys and values of the array */
foreach ($decode as $k => $v) {
   $new_string .= $k . ' | ' . $v . '<br/>';
}

/* Show the result on the page */
echo $new_string;

The above code returns the following;

name | sample
email | [email protected]
comments | test

If you want to access the array values one by one you can also use the following code.

/* The Json encoded array.*/
$json = '{"name":"sample","email":"[email protected]","comments":"test"}';

/* Decode the Json (back to a PHP array) */
$decode = json_decode($json, true);

echo $decode['name'];//returns sample
echo $decode['email'];//returns [email protected]
echo $decode['comments'];//returns test

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44864

Try as

$data = '{"name":"sample","email":"[email protected]","comments":"test"}';
$json = json_decode($data,true);
foreach($json as $key=>$val){
 echo $key." - ".$val;
    echo "<br />";
}

Check the output here

http://phpfiddle.org/main/code/ytn-kp0

You have done as

echo "\$a[$k] => $v <br />";

This would output as

$a[name] => sample 

"$a" will be considered as string

You can do the way you are doing but you need to change the echo something as

echo $k ."=>" .$v. "<br />"; 

Since you are looping the array using foreach and $k will contain the key of the array and $v will be the value !!

Upvotes: 1

TheDev
TheDev

Reputation: 82

$aa Isn't a array, is a JSON:

$a = $_POST['rawRequest'];
$aa = json_encode($a);

Thus, you can't use foreach in $aa.

Upvotes: 2

Quixrick
Quixrick

Reputation: 3200

If you want to decode a json string into an array instead of an object, use the 'Array' flag.

$array = json_decode($json_string, true);

Upvotes: 1

Related Questions