我有一个数组作为查询结果,并且该数组内部有一个关联数组。我需要选择有价值的产品,即我需要从该结果中获得["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"}
)
)
)
答案 0 :(得分:0)
结果不会得到数组。您得到stdClass这是一个对象。 您必须访问其属性。而且该属性是一个数组,其中包含一个元素,该元素是json编码的字符串,因此您必须先对其进行解码,然后再访问您感兴趣的数组键。 另外,您也没有指定感兴趣的产品数据(从row或rows属性?可以有更多行吗?)。
https://www.php.net/manual/en/language.types.object.php
https://www.php.net/manual/en/function.json-decode.php
<?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);
所有3个转储均导致:
array(3) {
[0]=>
string(3) "176"
[1]=>
string(3) "143"
[2]=>
string(2) "60"
}