1

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?

2
  • can you please provide what should be the output you needed? Commented May 19, 2016 at 12:15
  • Is your problem solved ? :) Commented May 19, 2016 at 12:33

2 Answers 2

2

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

2 Comments

this code is great as it works to remove the incorrect arrays from the array, however in this instance I want to be able to show an error to the user when they've not submitted both a system and serial number. I may use your awesome code later in my project though!
thank you for your help with the much simplier version of what I was trying to make. I can already think of a few improvements to other parts of my code using a similar approach to the above.
1

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>";
}
}

1 Comment

thanks for your input, Id didn't realise I could use a key() function. However, this results in an error for me at the moment. Which is: PHP Fatal error: Can't use function return value in write context which refers to the first if line.

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.