0

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";
    }
}
2
  • 1
    what you have tried so far? Commented Jul 6, 2017 at 21:29
  • @Ebrahim Poursadeqi, I've tried this: foreach ($values as $B) { $B = preg_replace('~[\r\n]+~', '', $B); if ($B['DeviceName'] == $Device) { print "Device ID: ".$B['DeviceId']."\n"; } } But it returns deviceID from another array =( Commented Jul 6, 2017 at 21:34

5 Answers 5

1
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!");
}
Sign up to request clarification or add additional context in comments.

7 Comments

Parse error: syntax error, unexpected '[' in check_stardwind.php on line 46 return [$sample['DeviceName'],$sample['DeviceID']];
I use PHP 5.3.3 =) May be it's reason)
@whi1test I don't have this line in my codes [$sample['DeviceName'],$sample['DeviceID'] . I had [$sample['DeviceName'],$sample['state']]; you changed $sample['state'] to $sample['DeviceID'] ?
@whi1test yes it is,I changed [] to array.you can test it
@ Ebrahim Poursadeqi, Yes, now it's working! I just added $sample = preg_replace('~[\r\n]+~', '', $sample); below foreach in function.
|
1

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

3 Comments

OMG, sorry guys! I looked into old output of my script (in txt file). That is why I was confused when DeviceID doesn't match. Thank you very much!
Glad to help. :)
Is there way to insert condition to check wether I found devices called "Device2" or not. I tried so: foreach ($values as $row) { $row = preg_replace('~[\r\n]+~', '', $row); if (isset($row['DeviceName']) AND ($row['DeviceName'] == $Device)) { print "DeviceID: ".$row['DeviceId']."\n"; }else{ print "UNKNOWN: Device ".$Device." not found!\n"; } } I get result: Device: HAImage1 not found. DeviceID: 0x0000000BF239A900 Device: HAImage1 not found. Device: HAImage1 not found. Device: HAImage1 not found. I want to receive only one reply if "HAImage1" is not found. =|
0

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'];

Comments

0

Since you're assigning $B, you should pass it by reference:

 foreach($values as &$B)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.