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?