Combine subArray values ​​by value from a multidimensional array

I have an array of such products:

$rows = array();
$rows[100] = array(
               array('product_id' => 101, 'name' => ''),
               array('product_id' => 102, 'name' => ''),
               array('product_id' => 103, 'name' => ''),
             );
$rows[200] = array(
               array('product_id' => 201, 'name' => ''),
               array('product_id' => 202, 'name' => ''),
             );
$rows[300] = array(
               array('product_id' => 301, 'name' => ''),
               array('product_id' => 302, 'name' => ''),
               array('product_id' => 303, 'name' => ''),
               array('product_id' => 304, 'name' => ''),
             );

I want to do the following:

$rows[] = array(
            array('product_id' => 101, 'name' => ''),  //First value from first subArray
            array('product_id' => 201, 'name' => ''),  //First value from second subArray
            array('product_id' => 301, 'name' => ''),  //First value from third subArray
            array('product_id' => 102, 'name' => ''),  //Second value from first subArray
            array('product_id' => 202, 'name' => ''),  //...
            array('product_id' => 302, 'name' => ''),
            array('product_id' => 103, 'name' => ''),
            array('product_id' => 303, 'name' => ''),
            array('product_id' => 304, 'name' => ''),
        );

Now I am trying to do this with this code:

$max_store_products = max(array_map('count', $rows));
$sortedArray = array();

for($j = 0; $j < $max_store_products; $j++)
{
    foreach($rows as $store_id => $store_products)
    {
        $sp = $rows[$store_id][$j];  

        if(isset($sp))
        {
            $sortedArray[] = $rows[$store_id][$j]; 
        }  
        else
            unset($rows[$store_id]);

    }
}

But it takes a lot of time and also does not give the expected result that I want.

Is there an easier way to do this?

+4
source share
4 answers

Explanation

Take a simple array to explain:

Array(
    0 => Array(1, 4, 7),
    1 => Array(2, 5, 8),
    2 => Array(3, 6, 9),
)

First an array_shift()anonymous function in your array, so your array looks like this:

Array(
    0 => function(){...}

    //Your values
    1 => Array(1, 4, 7),
    2 => Array(2, 5, 8),
    3 => Array(3, 6, 9),
)

Then we call call_user_func_array()and apply array_map()as a callback. What this will do basically:

//↓ 'array_map()' callback from 'call_user_func_array()'
array_map(function(){
    //Callback from the First array element

//        Second          Third           Fourth
//     array element   array element   array element   more elements...
//          ↓               ↓               ↓           ↓
}, Array(1, 4, 7), Array(2, 5, 8), Array(3, 6, 9) /* , ... */);

Array(1, 4, 7), Array(2, 5, 8), Array(3, 6, 9) /* , ... */
      β”‚  β”‚  β”‚         β”‚  β”‚  β”‚         β”‚  β”‚  └─ 3 iteration in 'array_map()' | 3 value
      β”‚  β”‚  β”‚         β”‚  β”‚  β”‚         β”‚  └──── 2 iteration in 'array_map()' | 3 value
      β”‚  β”‚  β”‚         β”‚  β”‚  β”‚         └─────── 1 iteration in 'array_map()' | 3 value
      β”‚  β”‚  β”‚         β”‚  β”‚  β”‚
      β”‚  β”‚  β”‚         β”‚  β”‚  └───────────────── 3 iteration in 'array_map()' | 2 value
      β”‚  β”‚  β”‚         β”‚  └──────────────────── 2 iteration in 'array_map()' | 2 value
      β”‚  β”‚  β”‚         └─────────────────────── 1 iteration in 'array_map()' | 2 value
      β”‚  β”‚  β”‚
      β”‚  β”‚  └───────────────────────────────── 3 iteration in 'array_map()' | 1 value
      β”‚  └──────────────────────────────────── 2 iteration in 'array_map()' | 1 value
      └─────────────────────────────────────── 1 iteration in 'array_map()' | 1 value

, , , . array_merge() , :

$result = array_merge($result, Array(1, 2, 3));  //First values from each subArray
$result = array_merge($result, Array(4, 5, 6));  //Second values from each subArray
$result = array_merge($result, Array(7, 8, 9));  //Third values from each subArray
//...

<?php

    $arr = []; //Your array here
    $result = [];
    array_unshift($arr, function(...$values)use(&$result){
        $result = array_merge($result, array_filter($values));
    });

    call_user_func_array("array_map", $arr);

    print_r($result);

?>
+2

. , .

$result = array_filter(array_reduce(array_map(null, ...$rows), 'array_merge' ,[]));

var_dump($rows);

:

$columns = array_map(null, ...$rows);

null array_map, , :

$columns = array_map(function (...$vals) {
    return $vals;
}, ...$rows);

$rows, , , .. . null , , docs.

$rawResult = array_reduce($columns, 'array_merge', []);

.

$result = array_filter($rawResult);

array_filter null, .

: ,

, , product_id , , :

$result = array_merge(...$rows);

usort($result, function ($a, $b) {
    return $a['product_id'] % 100 - $b['product_id'] % 100 ?: $a['product_id'] - $b['product_id'];
});

var_dump($result); // merged & sorted result
+2

, , :

<?php

$result = [];
// Get the highest number of items in an entry of `$rows`
$maxItems = 0;
foreach($rows as $row) {
   if (count($row)>$maxItems) $maxItems = $row;
}

for($ii=0; $ii < $maxItems; $ii++) {

   foreach($rows as $row) {

     if (isset($row[$ii])) {
       $result[] = $row[ii];
     }

   }

}

?>

, , , :

if(isset($sp))

$sp . PHP undefined, javascript, , ( , ), , E_NOTICE null. PHP, .

, , , , . :

  • , , , , .
  • , , . , , PHP script , , , .

, , , , , . - , , , , , .

0

function array_flatten($array) { 
 if (!is_array($array)) { 
  return FALSE; 
 } 
 $result = array(); 
 foreach ($array as $key => $value) { 
  if (is_array($value)) { 
   $result = array_merge($result, array_flatten($value)); 
  } 
  else { 
   $result[$key] = $value; 
  } 
 } 
 return $result; 
} 
-1
source

All Articles