I am trying to parse this array data that way generated from JQuery and JSON.
Array
(
[d] => Array
(
[0] => Array
(
[order] => 1
)
[1] => Array
(
[order] => 2
)
[2] => Array
(
[order] => 3
)
[3] => Array
(
[order] => 4
)
[4] => Array
(
[order] => 5
)
)
)
I am trying to save the above date into a mysql table, but cannot seem to parse the data properly to b开发者_运维知识库e inserted into the database.
Can anyone help?
I suppose language is PHP? You might wanna take a look json_decode()-function here: http://php.net/manual/en/function.json-decode.php
This looks like the ouput of the function print_r()
-- and this is not intented to be parsed.
Instead of trying to parse that, you should work directly with the data your PHP code is receiving from the Ajax request -- i.e. with the JSON data, decoded with json_decode()
.
You should use json_decode() (as others have already stated) for decoding your JSON into php arrays. But if I got it right your problem is how to work with PHP arrays so i suggest you check out some good basic tutorial. w3c has decent basic array tutorial here: http://www.w3schools.com/php/php_arrays.asp
All you have to do is something like this,
foreach ( $data['d'] as $key => $value ) {
$id = $value['order'];
$order = 'order';
$this->img_model->update_image_order($id , $order);
}
This assumes the number in JSON is the order ID.
精彩评论