Get array values by keys

后端 未结 3 1383
春和景丽
春和景丽 2020-12-13 06:01

I am searching for a built in php function that takes array of keys as input and returns me corresponding values.

for e.g. I have a following array



        
3条回答
  •  臣服心动
    2020-12-13 06:11

    I think you are searching for array_intersect_key. Example:

    array_intersect_key(array('a' => 1, 'b' => 3, 'c' => 5), 
                        array_flip(array('a', 'c')));
    

    Would return:

    array('a' => 1, 'c' => 5);
    

    You may use array('a' => '', 'c' => '') instead of array_flip(...) if you want to have a little simpler code.

    Note the array keys are preserved. You should use array_values afterwards if you need a sequential array.

提交回复
热议问题