Php 需要在数组中获取数组值
我有一个数组作为查询的结果,数组中有一个关联数组。我需要选择价值产品,即我需要从这个结果中获得Php 需要在数组中获取数组值,php,arrays,getvalue,Php,Arrays,Getvalue,我有一个数组作为查询的结果,数组中有一个关联数组。我需要选择价值产品,即我需要从这个结果中获得[“176”、“143”、“60”]。 请帮我拿这个 stdClass Object ( [num_rows] => 1 [row] => Array ( [setting] => {"name":"featured","product_name":"","p
[“176”、“143”、“60”]
。
请帮我拿这个
stdClass Object (
[num_rows] => 1
[row] => Array (
[setting] => {"name":"featured","product_name":"","product":["176","143","60"],"limit":"10","width":"200","height":"200","status":"1"}
)
[rows] => Array (
[0] => Array (
[setting] => {"name":"featured","product_name":"","product":["176","143","60"],"limit":"10","width":"200",
"height":"200","status":"1"}
)
)
)
您不会因此得到数组。您将获得stdClass,它是一个对象。 您必须访问它的属性。这个属性是一个数组,它包含一个json编码的字符串元素,所以您必须首先对它进行解码,然后访问您感兴趣的数组键。 另外,您没有指定您感兴趣的产品数据(来自row或rows属性?是否可以有更多的行?)
它返回一个stdClass,根据您获取SQL资源的方法,强制它返回一个关联数组,然后您就可以通过索引获取它。顺便说一句,重新格式化您的示例以使其更具可读性。
设置
类似JSON的loos-因此请阅读
<?php
$data = new stdClass();
$data->num_rows = 1;
$data->row = [
'setting' => '{"name":"featured","product_name":"","product":["176","143","60"],"limit":"10","width":"200","height":"200","status":"1"}',
];
$data->rows = [
0 => [
'setting' => '{"name":"featured","product_name":"","product":["176","143","60"],"limit":"10","width":"200","height":"200","status":"1"}'
]
];
// get product array from row
var_dump(json_decode($data->row['setting'])->product);
// get product array from first row of rows
var_dump(json_decode($data->rows[0]['setting'])->product);
// get product array from all rows
array_map(function(array $row) {
var_dump(json_decode($row['setting'])->product);
}, $data->rows);
array(3) {
[0]=>
string(3) "176"
[1]=>
string(3) "143"
[2]=>
string(2) "60"
}