2

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"}
4
  • Wait a minute. What is the raw input you get from $_POST['rawRequest']? You are calling json_encode() which doesn't make sense here. What is the value you started with from $_POST? Commented Feb 12, 2014 at 19:32
  • This is the value of that post field : {"name":"sample","email":"[email protected]","comments":"test"} Commented Feb 12, 2014 at 19:33
  • Ah, then you need to json_decode() it, not json_encode(). Commented Feb 12, 2014 at 19:34
  • @MichaelBerkowski The reason encode is in there is because I thought (possibly incorrectly) this is a string which needs to be converted in to a string Commented Feb 12, 2014 at 19:34

4 Answers 4

2

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

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

1

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 !!

4 Comments

You should explain why this would work instead of saying "try this"
Thanks, I will give this a go. However I would appreciate a explanation as @RUJordan says. I want to learn what is actually happening rather than endlessly copying and pasting :)
@tim.baker I like you.
Yes added the answer with the explanation !!
0

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

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.