2

in PHP if i have a multidimensional array like this how can i get the only the array that has the key highlight = 1, without using foreach, for, or any kind of loop? is it possible?

array(
array(
    [id] => xxx,
    [name] => blah,
    [highlight] => 0

),
array(
    [id] => yyy,
    [name] => blahblah,
    [highlight] => 1
),
array(
    [id] => zzz,
    [name] => blahblahblah,
    [highlight] => 0
),
)

thanks

Pedro Luz
  • 2,694
  • 4
  • 44
  • 54
  • 4
    `json_encode` and then `preg_match_all` <<== worst idea ever – Joel Harkes May 01 '13 at 11:22
  • Why are you afraid of loops when almost everything uses loops ?? – Baba May 01 '13 at 11:34
  • Try this http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php – Maroun Baydoun May 01 '13 at 11:35
  • @Baba im not affraid of loops, just the way that i have my DB, if that is array of photos, and only one with highlight=1, dont want to loop 1000 photos to get one. if is not possible, i prefer to change the DB and do it in another way. Yes we use alot of loops, but is the worst thing u can do, in terms of memory – Pedro Luz May 01 '13 at 11:48
  • That is micro optimization ... PHP loops are actually very fast – Baba May 01 '13 at 11:53

5 Answers5

5

Unless I'm mistaken, it's not possible without performing some kind of loop. The best solution I can think of would be to use array_filter(), however, this is essentially the same as looping:

$theArray = array_filter($array, function($v) { return $v['highlight'] == 1; });
billyonecan
  • 20,090
  • 8
  • 42
  • 64
  • Can you elaborate why you think that the best thing is `array_filter` and not just a loop? – hakre May 01 '13 at 12:28
  • @hakre I'm lazy and this was the shortest way I could think of doing it. I very much doubt there's any performance benefit to doing this over a regular loop. – billyonecan May 01 '13 at 12:36
2

You should not be afraid of loops because almost all function you can use in this case use loops (arrap_map,array_reduce,array_walk etc) .... etc.

For the fun of it you can try goto :D

START: // your array
O1: $num = - 1; $found = array(); $total = count($array);
O2: $num ++;
O3: if ($num >= $total) goto O6;
O4: if ($array[$num]['highlight'] == 1) $found[] = $array[$num];
O5: goto O2;
O6: print_r($found);
END:

See live Demo it works

Baba
  • 94,024
  • 28
  • 166
  • 217
1

You need to key the array differently if you don't want to use a loop but the hashtable PHP offers out of the box for arrays.

hakre
  • 193,403
  • 52
  • 435
  • 836
1
$json = (json_encode($array));
if (stripos($json,'"highlight":"1"')){
    echo "exists";
}else{ 
echo "doesn't";
}

Fairly fast, no loops, simple...however, it will only tell you whether what you searched for exists. Can be extended to get id & name and converted back to array.

Hope this helps someone out there.

Don
  • 11
  • 1
0

An alternative to billyonecan's correct answer would be to return an index to your array instead of making a copy of the array, as follows:

<?php

$test = array(
  array(
    'id' => xxx,
    'name' => blah,
    'highlight' => 0

  ),
  array(
    'id' => yyy,
    'name' => blahblah,
    'highlight' => 1
  ),
  array(
    'id' => zzz,
    'name' => blahblahblah,
    'highlight' => 0
  ),
);

$myKey = null;

array_walk($test, function(&$item1, $key) {
  global $myKey;
  if ($item1['highlight'] == 1) {
    $myKey = $key;
  }
});

var_dump($test[$myKey]);
// array(3) { ["id"]=> string(3) "yyy" ["name"]=> string(8) "blahblah" ["highlight"]=> int(1) } 
LeonardChallis
  • 7,759
  • 6
  • 45
  • 76