Combine 2 arrays of different length

后端 未结 4 1848
情深已故
情深已故 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 16:55

    something like this one:

    function combine($array1, $array2) {
        $array3 = array();
        foreach ($array2 as $key => $value) { //loop through all entries of array2
    
            //get the entry of array1 that corresponds to the value of array2's entry
            if (isset($array1[$value]) {
                $array3[$key] = $array1[$value] 
            }
        }
    
        return $array3;
    }
    

    I haven't tested it but it should give you something for thought.

提交回复
热议问题