1
(
    [1] => Array
        (
            [rules_properties_id] => 1
            [operator] => >=
            [value] => 2
            [function] => NumOrdersPlaced
            [rules_properties_params] => Array
                (
                    [num_days] => 30
                    [customer_id] => 5
                )

        )

    [2] => Array
        (
            [rules_properties_id] => 1
            [operator] => >=
            [value] => 5
            [function] => NumOrdersPlaced
            [rules_properties_params] => Array
                (
                    [num_days] => 90
                    [customer_id] => 5
                )

        )

    [3] => Array
        (
            [rules_properties_id] => 2
            [operator] => >
            [value] => 365
            [function] => CustAcctAge
            [rules_properties_params] => Array
                (
                    [customer_id] => 5
                )

        )

)

That's the print_r of an array that I'm getting back from my database. I need to find the index number of the sub-array that contains the function called NumOrdersPlaced (the expected result would be 2.) Is the only way to do this by looping through the array and subarrays and comparing (as in this answer)? Or is there a more efficient, elegant (i.e. one-liner) function available that I don't know about?

Community
  • 1
  • 1
EmmyS
  • 11,892
  • 48
  • 101
  • 156
  • did you used [array_search](http://www.php.net/manual/en/function.array-search.php#91365) ? –  Jan 30 '13 at 17:19
  • @raina77ow - sorry, this was cut-and-paste when we populated the database. There actually won't ever be two sub-arrays that contain the same function. But that brings up an interesting point. If there are two that contain the same, I'd want it to return an array of the index numbers, in this example 1 and 2. – EmmyS Jan 30 '13 at 17:20
  • @AkamOmer - no, I haven't tried that, because as mentioned, this is a multidimensional array. I don't want to have to loop through the sub-arrays and call array_search on each one. – EmmyS Jan 30 '13 at 17:22
  • I'd probably use `array_filter` here to grep all the items with `isset($el['function']) && $el['function'] === 'NumOrdersPlaced'`, then collect the keys of the resulting array. – raina77ow Jan 30 '13 at 17:27
  • php doesn't directly support searching multidimensional arrays. you'll have to loop over the sub-arrays and search each one manually. – Marc B Jan 30 '13 at 17:28
  • see [this](http://phpfiddle.org/main/code/747-6qe) if it help –  Jan 30 '13 at 17:32

1 Answers1

0

No, there is no one liner for searching multidimensional arrays in PHP, expect you'll write a function for it yourself and use it as one liner :)

Approaches would be:

  1. Change the data structure to somewhat that is good for search operations. eg xml with xpath
  2. When creating the array from your question, create another array which indexes are the function names, and which values are pointers to the sub arrays of the original array
  3. Use the database for that operations. It's optimized for it
  4. ...

When searching for the 'right' way you'll have to find a compromise between performance of search, insert, update, remove operations, memory consumption and ease of usage.


As you asked for a PHP solution, here comes an example how I would do it in PHP using and additional index array: (approach 2. from the list above)

// we need two arrays now:
$data = array(); 
$index = array();

// imagine you loop through database query results
foreach($db_result as $record) {
    // create a copy of $record as the address
    // of record will contain the last(!) element agter foreach
    $item = $record;

    // store pointers to that array in data and index
    $data []= &$item;
    $index[$item->function] = &$item;
}


// here is your one liner
$found = isset($index['NumOrdersPlaced']) ? $index['NumOrdersPlaced'] : NULL;

// another index seach:
$found = isset($index['CustAcctAge']) ? $index['CustAcctAge'] : NULL;

// note there is no additonal loop. The cost is the 
// additional memory for $index
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    you can put everything in one line. it just wont be very readable – Gordon Jan 30 '13 at 17:40
  • Can you explain what the `$data` array is for? I see that you're putting stuff in it, but it's never being accessed anywhere that I can see. – EmmyS Feb 01 '13 at 18:05
  • It its not directly accessed but data containts the 'real' data. index contains just references to entries in data. So you can access the elements of $data directly by 'function' – hek2mgl Feb 01 '13 at 19:20
  • @EmmyS Its the same like indexes in databases. The table contains the data and indexes containing just references to records in the table. This makes search operations much faster while having additional memory costs (the index) – hek2mgl Feb 01 '13 at 19:24