Php 使用curl的多维数组获取在foreach中显示无效参数
我有一个这样的数组,我使用file_get_内容获取它,这里是另一个url(从这里获取数组)Php 使用curl的多维数组获取在foreach中显示无效参数,php,Php,我有一个这样的数组,我使用file_get_内容获取它,这里是另一个url(从这里获取数组) $a=array(); $a=Db::getInstance()->ExecuteS("SELECT * FROM tablename LIMIT 0 , 2 ;"); $a=(array)$a; print_r($a); 然后我用 $result = file_get_contents('http://sample.com/change.php'); 这是$result的输出: Array
$a=array();
$a=Db::getInstance()->ExecuteS("SELECT *
FROM tablename
LIMIT 0 , 2
;");
$a=(array)$a;
print_r($a);
然后我用
$result = file_get_contents('http://sample.com/change.php');
这是$result的输出:
Array
(
[0] => Array
(
[id_stock_available] => 1
[id_product] => 1
[id_product_attribute] => 0
[id_shop] => 1
[id_shop_group] => 0
[quantity] => 3
[depends_on_stock] => 0
[out_of_stock] => 2
)
[1] => Array
(
[id_stock_available] => 2
[id_product] => 2
[id_product_attribute] => 0
[id_shop] => 1
[id_shop_group] => 0
[quantity] => 1
[depends_on_stock] => 0
[out_of_stock] => 2
)
)
当我为$result申请foreach时:
foreach ($result as $value) {
var_dump($value);
//var_dump($value['installed'];
}
它向我显示了为foreach()提供的
无效参数文件获取内容返回响应内容(失败时返回false),这是一个字符串,但不是数组
您必须将print\r
的结果解析回数组,这不是一个好主意
因此,请使用一些编码/解码策略来执行此操作:
echo json_encode($a);
及
不是序列化Web服务数据的正确方法,因为序列化数据意味着应该在接收端对其进行反序列化。正如您所知,没有一个函数可以对print\u r
输出的数据进行反序列化,即获取由print\u r
创建的字符串并返回传入的array
只读存储器:
打印\u r-打印有关变量的人类可读信息
人类可读并不意味着计算机可读
您有两种选择:
php的/:
/:
我建议使用json
,因为几乎每个平台都可以是web服务的客户端。使用本机序列化时,WS的使用者也将是用php编写的客户端。在php文件中,您必须将其更改为:
// your query here
$a = Db::getInstance()->ExecuteS("SELECT * FROM tablename LIMIT 0 , 2;");
// then output it as JSON
header('Content-Type: application/json');
echo json_encode($a);
然后,要在另一个php上获得它,请执行以下操作:
$result = file_get_contents('http://sample.com/change.php');
$values = json_decode($result, true);
这些值应该作为一个数组出现在$values
上尝试检查var\u dump($result)
首先显示输出字符串(614)”数组([0]=>array([id\u stock\u available]=>1[id\u product]=>1[id\u product\u属性]=>0[id\u shop]=>1[id\u shop\u group]=>0[数量]=>3[取决于库存]=>0[缺货]=>2][1]=>数组([id\U库存可用]=>2[id\U产品]=>2[id\U产品属性]=>0[id\U店铺]=>1[id\U店铺群]=>0[数量]=>1[取决于库存]=>0[库存不足]=>2))“您尝试过这个吗?$result=(数组)$result;然后再做一个var_dump如果你仔细看,$result
实际上是一个字符串。它在你的var_dump
@GianCarlo上显示数组(1){[0]=>string(614)”数组([0]=>array([id_库存可用]=>1[id_产品]=>1[id_产品属性]=>0[id_店铺]=>1[id_店铺团队]=>0[quantity]>3[取决于库存]=>0[缺货]=>2][1]=>1[取决于库存]=>2[id\U产品]=>2[id\U产品属性]=>0[id\U店铺]=>1[id\U店铺组]=>0[数量]=>1[取决于库存]=>0[缺货]=>2]。]现在foreach invaid参数没有显示,但它也在foreach循环中显示正确的数组,就像am使用foreach($result as$csv){}@user3450650不客气。你需要在答案的左边打勾来标记对你有帮助的答案。欢迎你,没有问题,所以有一个提示:下次尝试添加并发布相关代码,因为这可能对解决问题非常有帮助。@user3450650还需要阅读为什么singleton是不好的做法:
// web service.
$a=(array)$a;
json_encode($a);
// receiving end.
$result = file_get_contents('http://sample.com/change.php');
var_dump(json_decode($result);
// your query here
$a = Db::getInstance()->ExecuteS("SELECT * FROM tablename LIMIT 0 , 2;");
// then output it as JSON
header('Content-Type: application/json');
echo json_encode($a);
$result = file_get_contents('http://sample.com/change.php');
$values = json_decode($result, true);