0

Quick question, most likely for a veteran will be easy, or maybe im asking for too much.

i have this code for laravel in php, im not fan to do a foreach, is there a better way? i guess should be an existing function that replace my values of arr to the keys match on arr2, but i dont know

Its really important not to change the order.

$arr=  ['filters', 'repeat', 'via', 'type'];
$arr2= [
        'filters' => 'text1',
        'repeat' => 'text2',
        'via' => 'text3',
        'type' => 'text4',
    ];

foreach($arr as $k)
    $res[]=$arr2[$k];

return $res;
1
  • another way $res = array_intersect_key($arr2, array_flip($arr));, so that you don't lose the matching Commented May 6, 2020 at 8:46

5 Answers 5

3

You can do it using array_map.

$arr=  ['filters', 'repeat', 'via', 'type'];
$arr2 = [
    'filters' => 'text1',
    'repeat' => 'text2',
    'via' => 'text3',
    'type' => 'text4',
];

$res = array_map(function($key) use($arr2) {
    return $arr2[$key];
}, $arr);

print_r($res);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, quite clean and looks profesional, this is what i was looking for, thanks a lot :D
2

You can use Laravel's array_only helper function:

$res = array_only($arr2, $arr);

If you don't want to preserve the keys then you can use the array_values function too:

$res = array_values(array_only($arr2, $arr));

1 Comment

but its not always in same order
2

Yes, two ways:

  1. Use array_values(), assuming the values of $arr are exactly in the same order as the keys in $arr2 and always have the same keys. I'm guessing this is not exactly what you meant, but that's what your example shows. If that's the case, then it is as simple as:
    $res = array_values($arr2)
  1. Use array_map() to map the values of one array (in this case $arr) to a new array ($res):
    $mapper = function($k) use ($arr2) {
        return $arr2[$k];
    };
    $res = array_map($mapper, $arr);

You can then also add handling of cases where $k does not exist in $arr2, e.g. return null and then use array_filter to filter out the null values;

(I did not test this code but generally, this is the approach)

1 Comment

This is good, and yes i can add a null option, thanks
2

You can use Collection in Laravel

$res = collect($arr)->map(function ($key) use ($arr2) {
    return $arr2[$key];
})->toArray()

1 Comment

This would be great to keep it as a collection, ill keep this in mind, thanks :D
1

If you just want the array values you can use the array_values() function.

$arr2= [
        'filters' => 'text1',
        'repeat' => 'text2',
        'via' => 'text3',
        'type' => 'text4',
 ];

$res = array_values($arr2);

This will result in the following array:

['text1', 'text2', 'text3', 'text4']

2 Comments

but you are ignoring the first array
I don't think this deserves a downvote, as the example in your question is unclear. The case you provided will work just fine with array_values(). Next time, provide a more comprehensive example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.