简体   繁体   中英

PHP Arrays. Get value from array with arrays

I have array in PHP:

Array
(
  [1] => Array
    (
        [DeviceName] => Device1
        [DeviceId] => 0x0000001530A1B380
        [state] => 0
        ...
         )
  [2] => Array
    (
        [DeviceName] => Device2
        [DeviceId] => 0x0000001530A10C80
        [state] => 1
        ...
    )
  [3] => Array
    (
        [DeviceName] => Device3
        [DeviceId] => 0x0000001531471600
        [state] => 0
        ...
    )
  ...
)

How I can get values of state and DeviceId keys only from the array where DeviceName = Device2?

I have tried this, but it returns deviceID from another array:

foreach ($values as $B) {
    $B = preg_replace('~[\r\n]+~', '', $B);
    if ($B['DeviceName'] == $Device) {
        print "Device ID: ".$B['DeviceId']."\n";
    }
}

You can simply access it using for each loop

foreach ($array as $row) {
  if ($row['DeviceName'] == 'Device2') {
    echo $row['DeviceId'];
    echo $row['state'];
  }
}

Here is the live link based on your array Link

function findDeviceAndState($arr,$deviceName){
    foreach($arr as $sample){
        if($sample['DeviceName']==$deviceName){
            return array($sample['DeviceName'],$sample['state']);
         }
    }
    return null;
}

in your code call function and assume your array name is $values and assume you have just one device with name Device2

if(($result=findDeviceAndState($values,'Device2'))!=null){
 echo 'DeviceName: '.$result[0];
 echo 'State : '.$result[1]; 
}else{
 die("Device not found!");
}

Assuming that DeviceName is unique, you can extract the array values and index them by DeviceName :

$result = array_column($values, null, 'DeviceName');
echo $result['Device2']['state'];

由于您要分配$B ,因此应通过引用将其传递:

 foreach($values as &$B)

There are a few ways to do this. I'd probably use a function to return an array of search results. If you expect to find multiple instance of Device2 , you will need to loop through the array and look for matching results.

function findDevice2(array $devices) : array
{
    $results = [];
    foreach ($devices as $device) {
        if ($device['DeviceName'] == 'Device2') {
            $format = [
                'DeviceId' => $device['DeviceId'],
                'state' => $device['state']
            ];
            array_push($results, $format);
        }
    }
    return $results;
}

$foundSet = findDevice2($array);

var_dump($foundSet);

This function accepts the incoming array, loops through each record looking for the DeviceName to be Device2. Once found, it pushes the DeviceId and State into a results array to be returned. This way you end up with an array with only the results you desire.

array(1) {
  [0] =>
  array(2) {
    'DeviceId' =>
    string(18) "0x0000001531471600"
    'state' =>
    int(0)
  }
}

If you aren't using PHP 7.0+, just remove the type declarations from the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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