tags:

views:

44

answers:

2

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] => 
                            )
+1  A: 
<?php
function searchSale($needle)
{
    foreach ($data['xml']['sale'] as $id => $sale)
    {
        if ($sale->saleref == $needle)
        {
            return $id;
        }
    }
    return null;
}
?>
Sjoerd
should be `$sale['saleref']` since they're all arrays, not objects.
nickf
ah thank you nickf and thanks sjoerd for the solution
jim smith
A: 
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.

symcbean