1

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] => 
                            )
2
  • it looks like you are trying to search an xml list correct? Commented Jun 21, 2010 at 13:09
  • it's been converted to an array Commented Jun 21, 2010 at 13:31

2 Answers 2

2
<?php
function searchSale($needle)
{
    foreach ($data['xml']['sale'] as $id => $sale)
    {
        if ($sale->saleref == $needle)
        {
            return $id;
        }
    }
    return null;
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

should be $sale['saleref'] since they're all arrays, not objects.
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.