1

So i got one single dimensional array like:

Array
(
[0] => md5
[1] => name
[2] => description
[3] => url
)

and one multidimensional array:

Array
(
    [0] => Array
        (
            [0] => md5#1
            [1] => name1
            [2] => desc1
            [3] => url1
         )  
    [1] => Array
        (
            [0] => md5#2
            [1] => name2
            [2] => desc2
            [3] => url2
         )  
)

and I want to use values of first array as keys for subarrays of the multidimensional one, so the output should look like:

Array
    (
        [0] => Array
            (
                [md5] => md5#1
                [name] => name1
                [desription] => desc1
                [url] => url1
             )  
        [1] => Array
            (
                [md5] => md5#2
                [name] => name2
                [description] => desc2
                [url] => url2
             )  
    )

Alternatively(as a bit offtopic question), how can I sort the elements of multidimensional array by values of md5 if they keys of subarray are not [md5] but [0]?

Thanks in advance!

3
  • If you're sure in order of elements in both arrays - array_combine. Sorting is done with usort. Commented Jun 2, 2017 at 20:26
  • But how do I do that exactly? If I try to array_combine($keysarray, $multiDarray) I get array_combine(): Both parameters should have an equal number of elements because i think it's trying to set the keys for the [0] => Array, and not for it;s elements. Commented Jun 2, 2017 at 20:29
  • That's why there is the foreach loop see his answer. It goes through each item on the first level and applies the combine on its value. Commented Jun 2, 2017 at 20:36

2 Answers 2

2

For combining values of two arrays where values of one are keys, and values of other are values, use array_combine function:

$keysArray  = [];
$multiArray = [];
$result_array = [];
foreach ($multiArray as $value) {
    $result_array[] = array_combine($keysArray, $value);
}

For sorting - use usort and define your custom function:

usort($result_array, function($a, $b) { return strcmp($a['md5'], $b['md5']); });
Sign up to request clarification or add additional context in comments.

4 Comments

damn, I had 1 extra element in 1st array that i was not aware of and that where this error came from ^^ thanks a lot!
btw how do I save the value of $result_array outside the foreach? probably should put & but where? otherwise $result_array got only 1 element if printed outside the foreach
of course I did, thanks again! i guess it would work the same with array_push() instead of [] right? sry for dumb questions but i'm trying to understand how arrays exactly work
Yes, array_push($array, $value) is the same as $array[] = $value
0

Here is the functional-style equivalent of @u_mulder's answer.

This technique merely applies the array of key names as the keys for every row in the array.

Using arrow function syntax (available since PHP7.4), allows the custom callback to access the globally scoped $keys array without needing to explicitly write use($keys) in the function declaration.

Code: (Demo)

$first = ['md5#1', 'name1', 'description1', 'url1'];
$second = ['md5#2', 'name2', 'description2', 'url2'];
$multidimArray = [$first, $second];

$keys = ['md5', 'name', 'description', 'url'];

var_export(
    array_map(
        fn($row) => array_combine($keys, $row),
        $multidimArray
    )
);

Comments

Your Answer

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