1

I have a couple of arrays that I want to combine into new arrays but each new array needs to have one value from each value. Just creating a multidimensional array is not enough. So any help would be great.

I have this array

Array ( 
    [0] => Array ( 
        [0] => Supplier 1
        [1] => Supplier 1
        [2] => Supplier 2
    ) 

    [1] => Array ( 
        [0] => Product 1 
        [1] => Product 2
        [2] => Product 3 
    ) 

    [2] => Array ( 
        [0] => 123456 
        [1] => 654321 
        [2] => 111111 
    ) 

    [3] => Array ( 
        [0] => 7 
        [1] => 40 
        [2] => 5 
    ) 
)

But I need it to be like this

Array (
    [0] => Array (
        [supplier] => Supplier 1
        [descr] => Product 1 
        [partid] => 123456
        [quantity] => 7
    )

    [1] => Array (
        [supplier] => Supplier 1
        [descr] => Product 2
        [partid] => 654321
        [quantity] => 40
    )

    [2] => Array (
        [supplier] => Supplier 2
        [descr] => Product 3
        [partid] => 111111
        [quantity] => 5
    )
)
1
  • I have not tried much, I just searched for a similar problem but could not find anything I could use Commented Mar 15, 2016 at 11:34

3 Answers 3

1

You can use the PHP method array_map[1] to perform this (assuming that your original multi-dimensional array is in $arr):

$res = array_map(
    function ($lev, $oms, $num, $aan) {
        return [
            'leverancier' => $lev,
            'artikel_omschrijving' => $oms,
            'artikelnummer' => $num,
            'artikel_aantal' => $aan
        ];
    },
    $arr[3],
    $arr[1],
    $arr[0],
    $arr[2]);

What it does is loop through all n arrays passed at the end of the function call, and passes the entry at the same position in each array to the callback function.

[1] http://php.net/manual/en/function.array-map.php

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

<?php

$input = array ( 
    '3'=>array ( 
        0 => '200046 - Aswo Service B.V.' ,
        1 => '200046 - Aswo Service B.V.' ,
        2 => '200013 - Weststrate verpakkingen' ,
    ),

    '1'=>array ( 
        0 => 'Diode 1N 4007 1000V 1A' ,
        1 => 'Tyrap wit - 100x2,5mm' ,
        2 => 'Kartonnen doos 50x35x35 cm' ,
    ),

    '0'=>array ( 
        0 => '11-00055' ,
        1 => '100538' ,
        2 => '100500' ,
    ),

    '2'=>array ( 
        0 => '7' ,
        1 => '40' ,
        2 => '5' ,
    ) 
);

$result = array();

$loopLength = count($input[0]);

for($i = 0; $i < $loopLength; $i++)
{
    $singleResult = array();
    $singleResult['leverancier'] = $input[3][$i];
    $singleResult['artikel_omschrijving'] = $input[1][$i];
    $singleResult['artikel_nummer'] = $input[0][$i];
    $singleResult['artikel_aantal'] = $input[2][$i];

    $result[] = $singleResult;
}

echo '<pre>';
print_r($result);
echo '</pre>';

Output is:

Array
(
    [0] => Array
        (
            [leverancier] => 200046 - Aswo Service B.V.
            [artikel_omschrijving] => Diode 1N 4007 1000V 1A
            [artikel_nummer] => 11-00055
            [artikel_aantal] => 7
        )

    [1] => Array
        (
            [leverancier] => 200046 - Aswo Service B.V.
            [artikel_omschrijving] => Tyrap wit - 100x2,5mm
            [artikel_nummer] => 100538
            [artikel_aantal] => 40
        )

    [2] => Array
        (
            [leverancier] => 200013 - Weststrate verpakkingen
            [artikel_omschrijving] => Kartonnen doos 50x35x35 cm
            [artikel_nummer] => 100500
            [artikel_aantal] => 5
        )

)

1 Comment

Pleasure is mine :) So if you like my answer (or other user answer), please accept it as a best answer :)
0

Unified solution(for any number of elements) with array_unshift, call_user_func_array, array_map and array_combine functions:

// $arr is your initial array

$keys = ['leverancier', 'artikel_omschrijving', 'artikelnummer', 'artikel_aantal'];
array_unshift($arr, null);

$res = call_user_func_array("array_map", $arr);

$result = array_map(function($v) use (&$keys){
    return array_combine($keys, $v);
}, $res);

var_dump($result); // this will return the expected result
array (size=3)
  0 => 
    array (size=4)
      'leverancier' => string '200046 - Aswo Service B.V.' (length=26)
      'artikel_omschrijving' => string 'Diode 1N 4007 1000V 1A.' (length=23)
      'artikelnummer' => string '11-00055' (length=8)
      'artikel_aantal' => int 7
  1 => 
    array (size=4)
      'leverancier' => string '200046 - Aswo Service B.V.' (length=26)
      'artikel_omschrijving' => string 'Tyrap wit - 100x2,5mm' (length=21)
      'artikelnummer' => int 100538
      'artikel_aantal' => int 40
  2 => 
    array (size=4)
      'leverancier' => string '200013 - Weststrate verpakkingen' (length=32)
      'artikel_omschrijving' => string 'Kartonnen doos 50x35x35 cm' (length=26)
      'artikelnummer' => int 100500
      'artikel_aantal' => int 5

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.