Hi i am trying to fetch single value from below Array example -
嗨,我想从下面的数组示例中获取单个值 -
i want to fetch order_id and name -
我想获取order_id和名称 -
<?php
$url = 'http://example.com/index.php?route=feed/rest_api/orders&key=test219';
$json = file_get_contents($url);
echo '<pre>'; echo $json; echo '</pre>';
?>
Array showing
数组显示
Array
(
[0] => Array
(
[order_id] => 6
[name] => zoraya panansar
[status] => Complete
[date_added] => 2014-01-24 23:06:09
[products] => 4
[total] => 14.8000
[address_1] => 345 goldhawk road
[address_2] => hammersmith
[city] => london
[Postcode] => w6 0wz
[Country] => United Kingdom
[Email] => zpanansar@yahoo.com
[Tele.] => 0786870150
)
I already tried
我已经试过了
echo $json->order_id;
and
和
echo $json[0]['order_id'];
but its not working... Please help
但它不起作用......请帮助
3 个解决方案
#1
0
You can not access the data that the server is returning. Instead of returning you a json object, the server is returning you the print_r()
output over an array.
您无法访问服务器返回的数据。服务器不是返回一个json对象,而是通过数组返回print_r()输出。
If its your server, then stop the print, and use json_encode()
over the array before printing.
如果是您的服务器,则停止打印,并在打印前在阵列上使用json_encode()。
If it's not your server, then you have to parse the values from the text. You can use preg_match_all()
for this.
如果它不是您的服务器,那么您必须解析文本中的值。您可以使用preg_match_all()。
preg_match_all("/\[order_id\] => ([^\n\r]+)/", $json, $m);
print_r($m[1]);
There may be built-in php function that can take such print_r()
output and revert back into a php array, but I don't know about it!
可能有内置的PHP函数,可以采取这样的print_r()输出并恢复到PHP数组,但我不知道它!
#2
1
index.php is returning wrong data, if you look closer then you will see it is trying to send success = false result as json data but probably for testing purposes, it is also returning array content directly before the json data.
index.php返回错误的数据,如果你仔细观察,那么你会发现它正在尝试发送success = false结果作为json数据,但可能是出于测试目的,它也是在json数据之前直接返回数组内容。
Besides, if this is supposed to be a json result then you need to use json_decode before using the returned json object as a class object like $json->order_id.
此外,如果这应该是一个json结果,那么在使用返回的json对象作为类对象(如$ json-> order_id)之前,需要使用json_decode。
This is what I get when I try your current URL With the parameters you provided: Look at the last line:
这是我在使用您提供的参数尝试当前URL时获得的内容:查看最后一行:
Array
(
[0] => Array
(
[order_id] => 6
[name] => zoraya panansar
[status] => Complete
[date_added] => 2014-01-24 23:06:09
[products] => 4
[total] => 14.8000
[address_1] => 345 goldhawk road
[address_2] => hammersmith
[city] => london
[Postcode] => w6 0wz
[Country] => United Kingdom
[Email] => zpanansar@yahoo.com
[Tele.] => 0786870150
)
[1] => Array
(
[order_id] => 5
[name] => ciria ann nuqui
[status] => Complete
[date_added] => 2014-01-24 17:58:36
[products] => 7
[total] => 36.4000
[address_1] => 345 goldhawk road
[address_2] => hammersmith
[city] => london
[Postcode] => w6 0wz
[Country] => United Kingdom
[Email] => joycernuqui@yahoo.com
[Tele.] => 07515732291
)
)
{"success":false}
#3
0
Change the response from the url to json (Use json_encode function)
The last Row in url script should be
将响应从url更改为json(使用json_encode函数)url脚本中的最后一行应该是
echo json_encode($some_array);
And than if you want the content:
而且如果你想要内容:
$array = json_decode(file_get_contents($url),TRUE);
if(!is_array($array)){
echo 'bad response';
exit();
}
var_dump($array);
echo $array[0]['order_id'];
#1
0
You can not access the data that the server is returning. Instead of returning you a json object, the server is returning you the print_r()
output over an array.
您无法访问服务器返回的数据。服务器不是返回一个json对象,而是通过数组返回print_r()输出。
If its your server, then stop the print, and use json_encode()
over the array before printing.
如果是您的服务器,则停止打印,并在打印前在阵列上使用json_encode()。
If it's not your server, then you have to parse the values from the text. You can use preg_match_all()
for this.
如果它不是您的服务器,那么您必须解析文本中的值。您可以使用preg_match_all()。
preg_match_all("/\[order_id\] => ([^\n\r]+)/", $json, $m);
print_r($m[1]);
There may be built-in php function that can take such print_r()
output and revert back into a php array, but I don't know about it!
可能有内置的PHP函数,可以采取这样的print_r()输出并恢复到PHP数组,但我不知道它!
#2
1
index.php is returning wrong data, if you look closer then you will see it is trying to send success = false result as json data but probably for testing purposes, it is also returning array content directly before the json data.
index.php返回错误的数据,如果你仔细观察,那么你会发现它正在尝试发送success = false结果作为json数据,但可能是出于测试目的,它也是在json数据之前直接返回数组内容。
Besides, if this is supposed to be a json result then you need to use json_decode before using the returned json object as a class object like $json->order_id.
此外,如果这应该是一个json结果,那么在使用返回的json对象作为类对象(如$ json-> order_id)之前,需要使用json_decode。
This is what I get when I try your current URL With the parameters you provided: Look at the last line:
这是我在使用您提供的参数尝试当前URL时获得的内容:查看最后一行:
Array
(
[0] => Array
(
[order_id] => 6
[name] => zoraya panansar
[status] => Complete
[date_added] => 2014-01-24 23:06:09
[products] => 4
[total] => 14.8000
[address_1] => 345 goldhawk road
[address_2] => hammersmith
[city] => london
[Postcode] => w6 0wz
[Country] => United Kingdom
[Email] => zpanansar@yahoo.com
[Tele.] => 0786870150
)
[1] => Array
(
[order_id] => 5
[name] => ciria ann nuqui
[status] => Complete
[date_added] => 2014-01-24 17:58:36
[products] => 7
[total] => 36.4000
[address_1] => 345 goldhawk road
[address_2] => hammersmith
[city] => london
[Postcode] => w6 0wz
[Country] => United Kingdom
[Email] => joycernuqui@yahoo.com
[Tele.] => 07515732291
)
)
{"success":false}
#3
0
Change the response from the url to json (Use json_encode function)
The last Row in url script should be
将响应从url更改为json(使用json_encode函数)url脚本中的最后一行应该是
echo json_encode($some_array);
And than if you want the content:
而且如果你想要内容:
$array = json_decode(file_get_contents($url),TRUE);
if(!is_array($array)){
echo 'bad response';
exit();
}
var_dump($array);
echo $array[0]['order_id'];