在带有 stdClass 子数组的 JSON 数组中搜索值
Searching for values in JSON array w/ stdClass subarray
此 JSON 片段是 WooCommerce 订单的一部分,从 APIv2 get_order() 函数中提取。 [meta] 包含客户选择的产品变体数据。
[meta] => Array (
[0] => stdClass Object ( [key] => pa_size [label] => Size [value] => Large )
[1] => stdClass Object ( [key] => pa_color [label] => Choose Color [value] => Black )
如何在 stdClass 对象中搜索特定 [key] 和 return 其对应的 [value]?
应该这样做:
foreach ($rows as $object) {
{
$size = $object->pa_size;
$color = $object->pa_color;
// more code
}
<?php
$meta = array(); //Suppose that your array is $meta
$your_key="pa_size"; // if you are trying to search for "pa_size"
$your_value=""; // will retrun value "Large"
foreach( $meta as $obj )
{
if( $obj->key == $your_key )
{
$your_value = $obj->value;
break;
}
}
echo $your_value; // will retrun value "Large"
?>
此 JSON 片段是 WooCommerce 订单的一部分,从 APIv2 get_order() 函数中提取。 [meta] 包含客户选择的产品变体数据。
[meta] => Array (
[0] => stdClass Object ( [key] => pa_size [label] => Size [value] => Large )
[1] => stdClass Object ( [key] => pa_color [label] => Choose Color [value] => Black )
如何在 stdClass 对象中搜索特定 [key] 和 return 其对应的 [value]?
应该这样做:
foreach ($rows as $object) {
{
$size = $object->pa_size;
$color = $object->pa_color;
// more code
}
<?php
$meta = array(); //Suppose that your array is $meta
$your_key="pa_size"; // if you are trying to search for "pa_size"
$your_value=""; // will retrun value "Large"
foreach( $meta as $obj )
{
if( $obj->key == $your_key )
{
$your_value = $obj->value;
break;
}
}
echo $your_value; // will retrun value "Large"
?>