繁体   English   中英

通过php对象数组的键获取值

[英]get value by key of php object array

我有一个 PHP 对象,我试图在不使用 foreach 的情况下通过键获取值。

如果我执行以下操作,我可以获得价值:

$item_data_decode->meta_data[0]->value;

但是这些项目可能有不同的顺序,所以不能指望这种方法,我需要使用密钥,但这不起作用:

$item_data_decode->meta_data['First Name'];

代码:

$item_data_decode = json_decode($item_values);
if (!empty($item_data_decode->meta_data)) {
    $fName = $item_data_decode->meta_data['First Name']->value;
}

对象:

$a = new stdClass();
$a->meta_data = array();
$a->meta_data[0] = new stdClass();
$a->meta_data[0]->id = "2113";
$a->meta_data[0]->key = "First Name";
$a->meta_data[0]->value = "Recipient First Name";
$a->meta_data[1] = new stdClass();
$a->meta_data[1]->id = "2114";
$a->meta_data[1]->key = "Last Name";
$a->meta_data[1]->value = "Recipient Last Name";
$a->meta_data[2] = new stdClass();
$a->meta_data[2]->id = "2115";
$a->meta_data[2]->key = "addressLine 1";
$a->meta_data[2]->value = "Recipient Address Line 1";
$a->meta_data[3] = new stdClass();
$a->meta_data[3]->id = "2116";
$a->meta_data[3]->key = "addressLine2";
$a->meta_data[3]->value = "Recipient Address Line 2";
$a->meta_data[4] = new stdClass();
$a->meta_data[4]->id = "2117";
$a->meta_data[4]->key = "City";
$a->meta_data[4]->value = "Recipient Town/City";
$a->meta_data[5] = new stdClass();
$a->meta_data[5]->id = "2118";
$a->meta_data[5]->key = "Region";
$a->meta_data[5]->value = "Recipient Region/County";
$a->meta_data[6] = new stdClass();
$a->meta_data[6]->id = "2119";
$a->meta_data[6]->key = "Country";
$a->meta_data[6]->value = "N/A";
$a->meta_data[7] = new stdClass();
$a->meta_data[7]->id = "2120";
$a->meta_data[7]->key = "Postcode";
$a->meta_data[7]->value = "Recipient Postcode";


// outputs
[meta_data] => Array ( [0] => stdClass Object ( [id] => 2113 [key] => First Name [value] => Recipient First Name ) [1] => stdClass Object ( [id] => 2114 [key] => Last Name [value] => Recipient Last Name ) [2] => stdClass Object ( [id] => 2115 [key] => addressLine 1 [value] => Recipient Address Line 1 ) [3] => stdClass Object ( [id] => 2116 [key] => addressLine2 [value] => Recipient Address Line 2 ) [4] => stdClass Object ( [id] => 2117 [key] => City [value] => Recipient Town/City ) [5] => stdClass Object ( [id] => 2118 [key] => Region [value] => Recipient Region/County ) [6] => stdClass Object ( [id] => 2119 [key] => Country [value] => N/A ) [7] => stdClass Object ( [id] => 2120 [key] => Postcode [value] => Recipient Postcode ) )

json_decode添加true提供以下内容:

Array ( [id] => 232 [order_id] => 320 [name] => Tb [product_id] => 50 [variation_id] => 0 [quantity] => 1 [tax_class] => [subtotal] => 50 [subtotal_tax] => 0 [total] => 50 [total_tax] => 0 [taxes] => Array ( [total] => Array ( ) [subtotal] => Array ( ) ) [meta_data] => Array ( ) ) Array ( [id] => 233 [order_id] => 320 [name] => Turtle Bay Gift Card [product_id] => 50 [variation_id] => 0 [quantity] => 1 [tax_class] => [subtotal] => 30 [subtotal_tax] => 0 [total] => 30 [total_tax] => 0 [taxes] => Array ( [total] => Array ( ) [subtotal] => Array ( ) ) [meta_data] => Array ( [0] => Array ( [id] => 2113 [key] => First Name [value] => Recipient First Name ) [1] => Array ( [id] => 2114 [key] => Last Name [value] => Recipient Last Name ) [2] => Array ( [id] => 2115 [key] => addressLine 1 [value] => Recipient Address Line 1 ) [3] => Array ( [id] => 2116 [key] => addressLine2 [value] => Recipient Address Line 2 ) [4] => Array ( [id] => 2117 [key] => City [value] => Recipient Town/City ) [5] => Array ( [id] => 2118 [key] => Region [value] => Recipient Region/County ) [6] => Array ( [id] => 2119 [key] => Country [value] => N/A ) [7] => Array ( [id] => 2120 [key] => Postcode [value] => Recipient Postcode ) ) )

我个人会准备这样的数据:

$item_data_decode = json_decode($item_values, true);
$meta_array = array_combine(array_column($item_data_decode['meta_data'], 'key'), $item_data_decode['meta_data']);

if (!empty($meta_array['First Name'])) {
  $fName = $meta_array['First Name']['value'];
}

json_decode的第二个参数确保它只返回数组( Manual. )。 这样你就可以使用像array_column ( Manual ) 和array_combine ( Manual ) 这样的数组函数,并得到一个非常接近你想要的结构的数组。

测试用例,因为没有代码是它的缩写。

访问每个属性是您通常访问对象属性的方式。
请注意,由于"First name"有一个空格,因此不能通过箭头符号访问它,必须用大括号括起来。 对于任何没有空格的属性,都不需要花括号。

您的代码失败的原因是您试图使用用于数组的方括号表示法访问属性。

我知道您无法编辑实际的数组输出,但是如果您可以编辑 JSON,那么这将解决您的问题。

 { "meta_data": { "First name": { "id": 2113, "key": "First name", "value": "Recipient First Name" }, "Last Name": { "id": 2114, "key": "Last Name", "value": "Recipient Last Name" }, "addressLine 1": { "id": 2115, "key": "addressLine 1", "value": "Recipient Address Line 1" }, "addressLine2": { "id": 2116, "key": "addressLine2", "value": "Recipient Address Line 2" }, "City": { "id": 2117, "key": "City", "value": "Recipient Town/City" }, "Region": { "id": 2118, "key": "Region", "value": "Recipient Region/County" }, "Country": { "id": 2119, "key": "Country", "value": "N/A" }, "Postcode": { "id": 2120, "key": "Postcode", "value": "Recipient Postcode" } } }

var_dump($item_data_decode->meta_data->{"First name"}->value); // outputs "Recipient First Name"

如果您可以修改数组结构,则将其结构如下:

array(
    'First Name'=>array(
        'id'=>2113,
        'value'=>'Recipient First Name'
    ),
    'Last Name'=>array(
        'id'=>2114,
        'value'=>'Recipient Last Name'
    ),
    . . .
);

如果需要,您仍然可以像以前一样在foreach循环中使用这个数组,尽管进行了一些更改,同时能够直接访问您想要的值。

如果你不能修改数组结构,那么你就不走运了,如果你想找到你想要的值,就需要一个foreach循环。

如果您担心多次访问数组的性能,请考虑在处理之前将数组转换为上述结构。

编辑

示例数组转换:

$transformed_array = array();
foreach($item_data_decode->meta_data as $data) {
    $transformed_array[$data['key']] = array(
        'id'=>$data['id'],
        'value'=>$data['value']
    );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM