1

I working now for a couple of hours on my project, i have some key's from a early array that i want to loop to my new array and pick the value of the key's that i have from the early array.

Early array:

$old_keys = ['key1','key2','key3'];

New array:

$result = ['key1' => 'foo', 'key2' => 'bar', 'key6' => 'ipsum'];

Output that i want is:

$output = ['foo', 'bar'];

This is what i made:

foreach ($old_keys as $old_key) {

    $output[] = array_column($result, $old_key);

}
return $output;

What did i wrong because everything what i get is a empty array

Thanks a lot!

Update:

$keys = array_flip($old_keys);

$output = array_values(array_intersect_key($result, $keys));

echo '<pre>';
var_dump($output);

The $output is now filled with the multidimensional array of $result, but the values 'foo' and 'bar' are in the second level of this multidimensional array. The problem is, i get only the first level of this array and not the second level.

example of the output:

[
 [0] => string
 [1] => string
 [
  [key1] => foo
  [key2] => bar
 ]
]

What i want is:

[
 [0] => string
 [1] => string
 [3] => foo
 [4] => bar
]

Solution:

$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($result)), true);
3
  • While you are still here, you may want to re-title your question, because it is not an accurate way to describe your process. Commented Mar 17, 2017 at 2:28
  • Do you have a better title ? Commented Mar 17, 2017 at 3:03
  • perhaps something like: How to filter an array by keys using another array Commented Mar 17, 2017 at 3:22

1 Answer 1

1

Filter the $result array using $old_keys, but use the $old_keys values as keys:

$old_keys = array_flip(['key1','key2','key3']);
$result = ['key1' => 'foo', 'key2' => 'bar', 'key6' => 'ipsum'];
$output=array_intersect_key($result,$old_keys);
print_r($output);

If you want to re-index the keys in the result, replace the $output declaration with:

$output=array_values(array_intersect_key($result,$old_keys));
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, but i get so as i expect an empty array as output, because result is a multidimensional array.
@PieterDijkstra In your question, $result is not a multidimensional array. Please update your question to present $result as a multidimensional array. You must offer me a correct sample to manipulate so I can replicate the issue and solve the problem.
I don't know if I need to explain this bit, but my code replaces yours. You don't need to do any looping. And you can just return the print_r value. If there are no old keys in the result array, output will be an empty array.
see my update, i've already update my code with yours but it doesn't work on the way i want.
Something else must be wrong with your code, because this works: sandbox.onlinephpfunctions.com/code/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.