Reputation: 231
i have an array that looks like this, I want to search for a saleref and get it to give me the key in PHP, i've tried using array_search but i get nothing back. Alternatively i just want to display the other values in the same array as the salesref searched if there's a better way.
Array
(
[xml] => Array
(
[sale] => Array
(
[0] => Array
(
[saleref] => 305531
[saleline] => 1
[date] =>
[team] => WH
[manifest] => 0
[qty] => 1
[order_status] =>
)
[141] => Array
(
[saleref] => 306062
[saleline] => 1
[date] =>
[team] =>
[manifest] => 0
[qty] => 1
[order_status] => RECEIVED
)
[1] => Array
(
[saleref] => 306062
[saleline] => 2
[date] =>
[team] => WH
[manifest] => 0
[qty] => 1
[order_status] =>
)
Upvotes: 1
Views: 1615
Reputation: 48357
function findkey($val, &$array)
{
$keys=array();
foreach ($array as $key=$try) {
if ($try===$val) {
$keys[]=$key;
} else if (is_array($try)) {
$contained=findkey($val, $try);
if (count($contained)) {
$keys[]=$contained;
}
}
}
return $keys;
}
C.
Upvotes: 0
Reputation: 75599
<?php
function searchSale($needle)
{
foreach ($data['xml']['sale'] as $id => $sale)
{
if ($sale->saleref == $needle)
{
return $id;
}
}
return null;
}
?>
Upvotes: 2