Sort PHP array_intersect_key () results by second array

I have a method in a class that looks like this:

class SomeClass { private $hidden = array(....); /** * @return array - numeric indexed array in order of $this->hidden. * Suitable for use by list(var1, var2, ...) */ public function getAsList($list = array()) { return array_values(array_intersect_key($this->hidden, array_flip($list) ); } 

But this is not useful, since the calling method does not know the order of the key / element pairs in the associative array in the $ hidden instance variable. Ideally, the returned array will be in the same order as the keys specified in the $ list. For example:

 $foo = new SomeClass(); list($foo, $bar, $baz) = $foo->getAsList(array('foo', 'bar', 'baz'); 

I can easily write some explicit, verbose PHP code in a loop to do this, but is there any smart way to use the various array functions, for example. array_multisort () to spit it out in minimal lines of code (and hopefully with compiled code speed - I will check it if that matters).

In a way, it's a brain teaser that I don't know the answer to yet. This is not critical, I do it without an explicit loop, but I'm curious if this can be done. I spent about 30 minutes on this and have not yet found a solution.

+8
sorting php
source share
2 answers

Perhaps array_replace is the missing part of your puzzle:

 public function getAsList($list = array()) { $klist = array_flip($list); return array_values(array_intersect_key(array_replace($klist, $this->hidden), $klist)); } 

Example ( Demo ):

 $hidden = [ 'apples' => 19, 'eggs' => 7, 'grapes' => 144, 'mushrooms' => 3, 'oranges' => 16 ]; $list = ['grapes', 'apples', 'eggs', 'oranges']; $klist = array_flip($list); print_r(array_values(array_intersect_key(array_replace($klist, $hidden), $klist))); /* Array ( [0] => 144 [1] => 19 [2] => 7 [3] => 16 ) */ 
+9
source share

This is one of those cases when functional programming pales in comparison with the language design in terms of readability, ease of maintenance and efficiency.

I have a penchant for functional programming, but in this case it just doesn't pay off.

Code: ( Demo )

 $hidden = [ 'apples' => 19, 'eggs' => 7, 'grapes' => 144, 'mushrooms' => 3, 'oranges' => 16 ]; $list = ['grapes', 'apples', 'eggs', 'oranges']; foreach ($list as $item) { if (isset($hidden[$item])) { $result[] = $hidden[$item]; } } var_export($result); 

Exit:

 array ( 0 => 144, 1 => 19, 2 => 7, 3 => 16, ) 

If you insist on using functional programming, it would be most efficient to perform the necessary operations in the following order:

  • filter array
  • arrange the keys of the filtered array
  • reindex ordered, filtered array

Here's how:

Code: ( Demo )

 $flippedList = array_flip($list); var_export(array_values(array_replace($flippedList, array_intersect_key($hidden, $flippedList)))); 

(the same conclusion as in the previous fragment)

It is logical that it makes no sense to order an array in which there are extra elements. Lighten the load first.

-one
source share

All Articles