-1

What would be the most efficient way of looking at an array of associative arrays, to find the node which meets a parameter?

I would like to have a more efficient way of looking through the array to find and return the parent node, that just looping through - looking at each element and returning if matched. (it is also safe to assume, that there are no duplicates of data - so the first found, is the only one found)

Or is a for loop the best thing ive got?

e.g.

array(
[0] => array('name' => 'fred'),
[1] => array('name' => 'dave'),
[2] => array('name' => 'mike)
)

And wanting to get the node of data where the name == 'dave' or to see if there is in fact a node which has a element name set as 'dave'.

e.g. somthing like

isset($data[]['name'] == 'dave')
$info = getdata($data[]['name'] == 'dave')

(Apologies if I'm not using the correct technical terms, please do correct me as I do like to learn!)

Many thanks in advance for any advice! =)

OnIIcE
  • 811
  • 9
  • 27
  • http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php – Khior Aug 22 '13 at 15:37
  • In your second snippet, you're not using isset correctly. isset just checks if a variable exists or not, you cannot evaluate logical expressions inside isset. – ILikeTacos Aug 22 '13 at 16:05

3 Answers3

2

There is no better way than looping. PHP can't perform any magic that does not involve looking at each element in turn.

If you're doing this often, it helps to index your arrays by the search criterion:

$data = array(
    array('name' => 'Dave'),
    array('name' => ...)
);

$indexedData = array();
foreach ($data as $datum) {
    $indexedData[$datum['name']] = $datum;
}

$info = $indexedData['Dave'];

As long as your data structure is sub-optimal, there's only sub-optimal ways to access it.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

Here's a function for array recursion to one level. We use foreach() to loop through each second layer of child arrays, then use the built-in function array_search to see if it exists.

    function as_nested($needle,$haystack){
    $val;
        foreach($haystack as $key=>$arr){
            $arr_key = array_search($needle,$haystack[$key]);
            if(!empty($arr_key)){
             $val = $key;
            }
        }
     return $val;
    }

To execute, you supply the needle, then the haystack.

echo as_nested('dave',$myArray);

Output using your initial array is 1.

    $myArray[0] = array('name'=>'fred');
    $myArray[1] = array('name' => 'dave');
    $myArray[2] = array('name' => 'mike');
AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35
0

There is a function in php called in_array() that looks for a value in an array.

//Code credit to @deceze
$data = array(
    array('name' => 'Dave'),
    array('name' => ...)
);

function getData($data, $searchValue) {
    foreach ($data as $datum) {
        if (in_array($searchValue, $datum)) {
        return $datum;
    }
}
//array returned when $searchValue is found.

You can use the getData function to search for a value in an array (this is not index specific. ie not restricted by only name, etc.)

Mic1780
  • 1,774
  • 9
  • 23