Combine 2 arrays of different length

后端 未结 4 1857
情深已故
情深已故 2021-01-26 16:12

I have an array with 100 values(array1). I have another array with 8 values(array2). I want to take the values from array2 and use them as keys and extract the values in array1

4条回答
  •  被撕碎了的回忆
    2021-01-26 17:02

    you can use below code

    function array_combine2($arr1, $arr2) {
        foreach($arr2 as $val){
         $arr3[] = $arr1[$val];
         }
    
        return $arr3;
    }
    

    For inctanse,

        $arr1['x'] = 2;
    $arr1['y'] = 3;
    $arr1['z'] = 4;
    
    $arr2[0] = 'x';
    $arr2[1] = 'y';
    $arr2[2] = 'z';
    

    the above function will return the below as resbonse

    $arr3[0] = 2;
    $arr3[1] = 3;
    $arr3[2] = 4;
    

提交回复
热议问题