0

I have the following array

    $fieldValues = [
        'brand' => 'brand_id',
        'mobile' => 'mobile',
        'customer' => 'customer_id',
        'points_value' => 'points'
    ];

Here is my field array which should get values from keys;

$fields = ['brand','points_value'];

So, here the output should be.

$fields = ['brand_id','points'];

I can create a function to convert them into values but just wanted to check if there is any function which can help me with it? I tried array_flip code but bad luck.

1 Answer 1

1

I don't think there is a single function that does that. However, a combination of array_flip and array_intersect_key does it:

$result = array_values(array_intersect_key($fieldValues, array_flip($fields)));
print_r($result);

Sidenote: If you want the keys preserved, then just remove the array_values. This just turns the resultant array into numeric indexed.

Or just plain ol' foreach should also suffice:

$result = [];
foreach($fields as $key) {
    if(isset($fieldValues[$key])) {
        $result[] = $fieldValues[$key];
    }
}
print_r($result);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.