I used foreach to print all keys and values. "$devicename_key" is printing the key which is fine but "$devicetype_key" is not printing. I see the value of 129 exist in array.
I have array with id in one row and value in next, its coming from database and I was able to clean it up in something like this. If there is another way to do this, please feel free to tell me.
I am getting the key for 128 adding 1 to it and then printing the value of 128. 129 is not printing.
Key=0, Value=:128
Key=1, Value=:TCM-7811
Key=2, Value=:129
Key=3, Value=:2
Key=4, Value=:130
Key=5, Value=:3
foreach($extrafield_info as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
// device name 128
$deviceid_key = array_search('128', $extrafield_info);
//echo 'device id key is ' . $deviceid_key;
$devicename_key = $deviceid_key +1 ;
//echo 'device key is ' . $devicename_key;
// device type 129
if (array_key_exists('129', $extrafield_info)) {
$devicetype_key = array_search('129', $extrafield_info);
echo 'key is ' . $devicetype_key;
}
You are doing array_key_exists
in your final if
check, but 129 is not the key, it is the value.
I think I understand what you are trying to do:
$extrafield_info[] = 128;
$extrafield_info[] = 'TCM-7811';
$extrafield_info[] = 129;
$extrafield_info[] = 2;
$extrafield_info[] = 130;
$extrafield_info[] = 3;
// Fetch key value for 128 (ends up being 0)
$deviceid_key = array_search(128, $extrafield_info);
// Get next key (should be now 1)
$devicename_key = ($deviceid_key +1) ;
// Insert next key for search for new $devicename_key (1)
if(array_key_exists($devicename_key, $extrafield_info)) {
// Should return key 2
$devicetype_key = array_search(129, $extrafield_info);
echo 'key is ' . $devicetype_key;
}