0

Looking at the code above...

$Array = array(array("name"=>"Mickey","type"=>"mouse"),array("name"=>"Donald","type"=>"duck"),array("name"=>"Little Helper","type"=>"eniac"));
$search = "Donald";
foreach($Array as $Item){
    if($Item["name"]==$search) $MyItem = $Item;
}
echo('The item named "'.$search.'" is '.$MyItem["type"]);

... I have the feeling that there is an array function or a better way to find an item inside a bidimensional array. These arrays are like a table. Maybe setting the keys as the index unique values (in this case, the name), but I don't know how to do either.

Gustavo
  • 1,673
  • 4
  • 24
  • 39
  • 1
    there is no official way. php has single dimensional arrays and offers a large number of functions to deal with 1 dimensional arrays. the fact that you can embed arrays in arrays doesn't change the fact that php's functions are all geared to single dimensional arrays. – Marc B Mar 05 '14 at 20:51
  • 1
    http://stackoverflow.com/questions/10811908/find-values-in-multidimensional-arrays – djot Mar 05 '14 at 20:54
  • I'm only aware of one function specifically for dealing with multidimensional arrays: [array_multisort](http://us1.php.net/manual/en/function.array-multisort.php). – larsAnders Mar 05 '14 at 20:54
  • @MarcB - while most individual functions are targeted at single dimensional arrays, there are a couple of exceptions like the new array_column() – Mark Baker Mar 05 '14 at 20:55
  • @djot: I did a lot o searches and did't find it, maybe stack needs a better searcher... – Gustavo Mar 05 '14 at 22:43
  • @MarcB: not sure about this, conceptually speaking. Any space is generated by a certain number of dimensions, one by one. Php is only following mathematics. – Gustavo Mar 05 '14 at 22:46
  • @GustavoPinent: truu, but php arrays can't be done with `$arr[1,2]` or whatever. You get one key in an array, and that's your 1 dimension. The fact that `$arr[1][2]` works is irrelevant. that's two separate 1 dimensional arrays, orthogonal to each other. – Marc B Mar 06 '14 at 00:58

3 Answers3

2

Using the new array_column() function in PHP 5.5

$Array = array(array("name"=>"Mickey","type"=>"mouse"),array("name"=>"Donald","type"=>"duck"),array("name"=>"Little Helper","type"=>"eniac"));
$search = "Donald";

$key = array_search(
    $search,
    array_column($Array,'name')
);
if($key !== false) {
    $MyItem = $Array[$key];
    echo('The item named "'.$search.'" is '.$MyItem["type"]);
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • I did a proposal. I think will be the "official" solution because came from php producers. Also I suggest you to put a previous version temp code, I don't have any server with this version running, maybe is not very popular so far. – Gustavo Mar 05 '14 at 23:14
  • PHP 5.5 is still only running on about 1.3% of web sites according to w3techs.com, but growing slowly in popularity – Mark Baker Mar 05 '14 at 23:28
  • @MarkBaker: Anyway, spread the wor(l)d. I try to popularise `array_column` wherever I can ;) – djot Mar 06 '14 at 08:15
1

If you can recompose the array as:

array("Mickey"=>"mouse","Donald"=>"duck","Little Helper"=>"eniac");

or

array("Mickey"=>array("name"=>"mouse"),"Donald"=>array("name"=>"duck"),"Little Helper"=>array("name"=>"eniac"));

and just return by key

ka_lin
  • 9,329
  • 6
  • 35
  • 56
0

Works for this case:

echo array_column($Array, 'type', 'name')[$search];

Or with check:

$names = array_column($Array, 'type', 'name');
echo isset($names[$search]) ? $names[$search] : 'not found';

To convert to name => type use:

$Array = array_column($Array, 'type', 'name');

Then after you can just use $Array[$search].

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • @GustavoPinent: The first example with the `[$search]` nomenclature works as of PHP 5.4.0. `array_column()` is only available in PHP > 5.5.0. So your way is it for your version. – AbraCadaver Mar 06 '14 at 01:29
  • Also, in 5.3.x (version I work) array_function(args)[key] doesn't works, I got to doit in two steps Array = array_function(args) and than Array[key]. I'm thinking about an upgrade... – Gustavo Mar 06 '14 at 02:26