简体   繁体   English

从关联数组中获取特定键/值并将它们存储为变量

[英]Get Specific Key/Value From Associative Array and Store them as Variable

I need to access the specific key/value of associative array as variables using foreach loop. 我需要使用foreach循环访问关联数组的特定key/value作为变量。 Getting key/value pairs of all data in the array is not a problem. 获取array中所有数据的key/value对不是问题。 The problem is with getting a specific key/value . 问题在于获取特定的 key/value Take the code below for example 以下面的代码为例

<?php

$data = [

    'person1' => 
        [
            'id' => 101,
            'firstName' => 'John',
            'LastName' => 'Smith'

        ],

        'person2' => 
        [
            'id' => 102,
            'firstName' => 'Mary',
            'LastName' => 'Smart'

        ]

];

I can get key/value pairs of all data in the array by the code below: 我可以通过以下代码获取array中所有数据的key/value对:

foreach($data as $firstKey => $firstValue){
    foreach ($firstValue as $secondKey => $secondValue) {
        echo $secondKey. ": ". $secondValue . "<br />";


    }
}

The above code is not exactly what I want. 上面的代码并不是我想要的。 I want to get specific key/value for example, I want to get only the key/value of only the id s. 我想得到特定的key/value ,例如,我想只得到id的键/值。

So I tried something like this: 所以我尝试过这样的事情:

$specificId = null;
foreach($data as $firstKey => $firstValue){
    foreach ($firstValue as $secondKey => $secondValue) {
        if($secondKey == 'id'){ // using if statement to get key/value pair of person's id
            $specificId = $secondValue; //storing the specific ids as variable
            echo $secondKey . ": ". $specificId . "<br>"; 

        }

    }
}


?>

The above code seems to work but if I also want to get key/value for only firstName , then, I have to write another if statement. 上面的代码似乎工作,但如果我也想获得firstName key/value ,那么,我必须编写另一个if语句。 I will end up writing so many if statements in the foreach loop. 我将最终在foreach循环中编写如此多的if语句。 Do you know another way I can write lesser code to get specific key/value pairs? 您是否知道另一种方法可以编写较少的代码来获取特定的key/value对?

There are many similar questions like mine but the answer I am looking for is not the objective of any of those questions. 有许多类似的问题,比如我的,但我正在寻找的答案不是任何这些问题的目标。

foreach($data as $firstKey => $firstValue) {
       echo array_keys($firstValue, $firstValue['id'])[0].': '.$firstValue['id'].'</br>';
       echo array_keys($firstValue, $firstValue['firstName'])[0].': '.$firstValue['firstName'].'</br>';
}

you can just call the associative array key from the array 你可以从数组中调用关联数组键

This outputs key and values 这输出键和值

array_intersect_key should do the job just fine: array_intersect_key应该可以正常工作:

$results=[];
foreach($data as $name => $attriubtesPerson)
{
    $results[$name] = array_intersect_key($attributesPerson, ['id' => null, 'firstName' => null]);
}

documentation can be found here 文档可以在这里找到

To access specific key values you need to convert the array to Json and access it just like a normal object. 要访问特定的键值,您需要将数组转换为Json并像普通对象一样访问它。

  $data = [ 'person1' => [ 'id' => 101, 'firstName' => 'John', 'LastName' => 'Smith' ], 'person2' => [ 'id' => 102, 'firstName' => 'Mary', 'LastName' => 'Smart' ] ]; $obj = json_decode(json_encode($data)); //to access key=values directly echo $obj->person1->firstname; //to access all firstname only foreach($obj as $o){ echo $o->firstName; } 

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

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