简体   繁体   中英

What kind of php array is this and how do I edit it?

I am working with a project that is coded in php OOP. I have created a form that post back to itself and gets those values and puts them into a predefined array format. I say predefined because this is the way the previous coder has done it:

$plane->add(array('{"name":"Chris","age":"22"}','{"name":"Joyce","age":"45"}'));

So when I get my values from the $_POST array, I thought it would be simple, so I tried

$plane->add(array("{'name':$customerName,'age':$customerAge}"));

This is triggering an error though, it seems it's passing the actual name of the variable in as a string instead of it's value. So how do I pass those values in to that function. While we are on it, can someone explain what kind of array that is, I thought arrays were always $key=>value set, not $key:$value .

As other comments and answers have pointed out, the data is being serialized in a format known as JSON. I suggest reading up on json_encode() and json_decode()

To make your example work, you would have to do:

$data = array("name" => $customerName, "age" => $customerAge);
$plane->add(array(json_encode($data));

That looks like json: http://sandbox.onlinephpfunctions.com/code/e1f358d408a53a8133d3d2e5876ef46876dff8c6

Code:

$array = json_decode('{"name":"Chris","age":"22"}');
print_r( $array );

And you can convert an array to json with:

 $array = array("Customer" => "John");
 $arrayJson = json_encode( $array);

So to put it in your context:

$array = array("Name"=>"Chris", "age" => 22);
$array2 = array("Name"=>"John", "age" => 26);
$plane->add(array( json_encode( $array),json_encode( $array2) );

It looks like it could be JSON, but might not be.

Be careful to quote everything like they have done, you didn't have any quotes around the name or age. I've added the same sort of quotes, and used the backslashes so that PHP doesn't use them to end the string:

$plane->add(array("{\"name\":\"$customerName\",\"age\":\"$customerAge\"}"));

Be wary of user data, if $customerName and $customerAge come from POST data, you need to properly escape them using a well tested escaping function, not something you just hack together ;)

It looks like your array is an array of JSON encoded arrays. Try using:

$plane->add(array('{"name":"' . $nameVar . '","age":"' . $ageVar . '"}', ...));

If you use the following:

echo json_encode(array('name' => 'Me', 'age' => '75'), array('name' => 'You', 'age' => '30'));

You will get the following string:

[{"name":"Me","age":"75"},{"name":"You","age":"30"}]

I believe you are getting an error because what you are trying to pass to the function is not JSON while (It looks like ) the function expects an array json strings, Manually trying to write the string might not be a good idea as certain characters and type encode differently. Best to use json_encode to get you json string.

$plane->add(array(json_encode(array('name'=>$customerName,'age'=>$customerAge))));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM