Home > other >  Make new array from different arrays
Make new array from different arrays

Time:01-21

I am trying to make a new array from 3 differents arrays. De keys are always the same in every array.

Example array:

$array1 = array('man'=>'man1', 'desc'=>'desc1', 'stock'=>7, 'price'=>789);
$array2 = array('man'=>'man2', 'desc'=>'desc2', 'stock'=>8, 'price'=>1709);
$array3 = array('man'=>'man3', 'desc'=>'desc3', 'stock'=>9, 'price'=>2759);

I tried this but it is not good:

$length = count($array1);
$result = array();
for ($index = 0; $index < $length; $index  ) {
    $result[]=array($array1[$index], $array2[$index], $array3[$index]);
}

I want the 3 arrays in one array, something like:

Array
(
    [0] => Array
        (
            [man] => man1
            [desc] => desc1
            [stock] => 7
            [price] => 789
        )

    [1] => Array
        (
            [man] => man2
            [desc] => desc2
            [stock] => 8
            [price] => 1709
        )

    [2] => Array
        (
            [man] => man3
            [desc] => desc3
            [stock] => 9
            [price] => 2759
        )

    [3] => Array
        (
            ...
        )

)

But it can be possible that one of the 3 arrays are empty, so don't display this in the new array. It is also possible that one of the 3 arrays gives more results than one like shown above.

Thanks in advance for helping me out here!

UPDATE:

I tried also this and it gives me the right results without:

But it can be possible that one of the 3 arrays are empty, so don't display this in the new array. It is also possible that one of the 3 arrays gives more results than one like in my code above.

$result = array();

foreach($array1 as $k=>$v) {
  $result[0][$k] = $v;
}
foreach($array2 as $k=>$v) {
  $result[1][$k] = $v;
}
foreach($array3 as $k=>$v) {
  $result[2][$k] = $v;
}

And maybe the [0] [1] [2] etc are not good because one of the arrays can have more then one result.

The result array should be displayed in a HTML table where the keys are the column names

CodePudding user response:

You might want to use this:

This is for flat array of result:

$newArray = [];
foreach([$array1, $array2, $array3] as $array){
    if(!empty($array)){
        $newArray[] = $array;
    }
}

And you can use this for multi-dimensional array of result:

function makeArray($arrays, $newArray=[])
{   
    foreach($arrays as $array){
        if(!empty($array)){
            if(isset($array[0]) && is_array($array[0])){
                $newArray = makeArray($array, $newArray);
            }else{
                $newArray[] = $array;
            }
        }
    }
    return $newArray;
}

$newArray = makeArray([$array1, $array2, $array3]);

CodePudding user response:

What you basically want is

$result = [$array1, $array2, $array3];

This however does include empty arrays. You can filter them like this:

$result = array_filter([$array1, $array2, $array3], function($v) { return !empty($v); });

CodePudding user response:

Here is a recursive function that will accept an array containing any number of arguments with multilevel nesting:

function buildResult(array $all):array
{
    $result = [];
    foreach($all as $one){
        if(array_key_exists('man', $one)){
            // one result collect it
            $result[] = $one;
        }else{
            // zero or many results
            // array_merge will ignore empty results
            $result = array_merge($result, buildResult($one));
        }
    }
    
    return $result;
}

Check it out in the sandbox.

If your results will always be no more than one level deep you can skip the recursion:

function buildResult(array $all):array
{
    $result = [];
    foreach($all as $one){
        if(array_key_exists('man', $one)){
            $result[] = $one;
        }else{
            $result = array_merge($result, $one);
        }
    }
    
    return $result;
}
  • Related