I am trying to get the key index based on the value that I am searching in the array. I have the following array:
[0] => Array
(
[n1:ProductWithTermPricingOptions] => Array
(
[n1:ProductName] => Customer will provide modem
[n1:PricingOptions] => Array
(
[n1:Priority] => 600
[n1:PricingOptionCode] => LCTL_SA_MDM_NONE_LCTL:NONE:MODEM:IN:CON:SA:na:na:NONE:NONE:-1:-1:NONE:LCTL:NONE:0:na:NONE:A:PO:na:0:NULL:NULL:NULL
[n1:ProductMonthlyCharge] => $0.00
[n1:PromoMonthlyCharge] => $0.00
[n1:ProductActivationCharge] => $0.00
[n1:ProductActivationChargeDescription] => Array
(
)
[n1:ContractLength] => Array
(
)
(
)
(
)
[n1:ProductDisclaimer] => Array
(
)
)
)
I need to get the key number where the value "Customer will provide modem" resides in this case it should return number 0 for the key. In other situation it will return a different position. I have tried the following but it returns the key where the value is and not [0]:
public static function searchArrayKeybyValue(array $array, $search) {
foreach ( new RecursiveIteratorIterator ( new RecursiveArrayIterator ( $array ) ) as $key => $value ) {
if ($search === $value)
return $key;
}
return "N/A";
}
My goal here is to put this option in the last position of the array.
function array_finder($array, $search, $parent_key = false)
{
foreach ($array as $local_key => $value) {
$key = ($parent_key === false) ? $local_key : $parent_key;
if (is_array($value) and ($subsearch = array_finder($value, $search, $key)) !== false) {
return $subsearch;
} elseif ($value == $search) {
return $key;
}
}
return false;
}
echo array_finder($array, 'Customer will provide modem');