BottyZ
BottyZ

Reputation: 145

how to check specific keys and values in a multi-dimensional php array, exist and have values?

I have stored an array based on post values, with multiple arrays stored in it, example below (the key numbers can vary depend on user input):

Array (
[1] => Array
  ([system] => A [serial]=> 12345L)
[3] => Array
  ([system] => B [serial] =>)
[4] => Array
  ([system] => D [serial] => 56789L [licence] => ABCD1-EFGH2-IJKL3-MNOP4-QRST5)
[5] => Array
  ([serial] => 98765L [licence] => 1234567890)
)

Note: I'm not sure if it worth mentioning but for the above example the array keys are 1345, but it could be 123456 or 56, etc...

I want to check that each of the arrays has a system key and a serial key, and that these keys both have a value. The licence key is optional.

so in theory my example would mean that 1 & 4 are valid, but 3 isn't as its missing a value for serial and neither is 5 as it doesn't have a key for system.

I'm a bit lost in how I would go about this, owing to it being inside of an array to begin with. However, I've made the below which is very rudimental and I can imagine quite inefficient.

foreach($sys_arr as $k => $v) {
  if (is_array($v) == true) {
    foreach($v as $key => $value) {
      if ($key = "system" && $value == null) {
        echo "Error: System has no value<br>";
      } elseif ($key = "serial" && $value == null) {
        echo "Error: Serial has no value<br>";
      } else {
        echo "Both keys have values<br>";
      }
    }
    if (!array_key_exists("system", $v)) {
      echo "Error: No System Key<br>";
    } elseif (!array_key_exists("serial", $v)) {
      echo "Error: No Serial Key<br>";
    } else {
      echo "Both Keys exist!<br>";
    }
  }
}

Is anyone able to offer any suggestions on how I can improve this?

Upvotes: 1

Views: 98

Answers (2)

Sofiene Djebali
Sofiene Djebali

Reputation: 4508

This should work :

$array = [
    ['system' => 'A', 'serial' => '1232'],
    ['system' => 'B', 'serial' => ''],
    ['system' => 'D', 'serial' => '1232', 'licence' => '123123'],
    ['serial' => '&2312321', 'licence' => '123123']
];

$valid = [];

foreach ($array as $key => $value) {
    if (!isset($value['system'])) {
        echo 'Error: No System Key<br>';
        continue;
    }
    if (!isset($value['serial'])) {
        echo 'Error: No Serial Key<br>';
        continue;
    }

    if (empty($value['system'])) {
        echo 'Error: System has no value<br>';
        continue;
    }
    if (empty($value['serial'])) {
        echo 'Error: Serial has no value<br>';
        continue;
    }
    $valid[] = $value;
}

var_dump($valid);

Upvotes: 2

JYoThI
JYoThI

Reputation: 12085

no need foreach again just do something like this

foreach($sys_arr as $k => $v) 
{


  if (key($v['system']) = "system" && $v['system'] == null)
  {
    echo "Error: System has no value<br>";
  } 
  elseif (key($v['serial'])= "serial" && $v['serial'] == null) 
  {
    echo "Error: Serial has no value<br>";
  } 
  else
  {
    echo "Both keys have values<br>";
  }

if (!array_key_exists("system", $v)) 
{
  echo "Error: No System Key<br>";
} 
elseif (!array_key_exists("serial", $v))
{
  echo "Error: No Serial Key<br>";
}
else 
{
  echo "Both Keys exist!<br>";
}
}

Upvotes: 1

Related Questions