1

So, imagine you have 3 arrays:

1,2,3,4,5
6,7,8,9,10
11,12,13,14,15

And you want to combine them into new arrays based on index:

1,6,11
2,7,12
3,8,13
4,9,14
5,10,15

What on earth could achieve this? Also, the total number of arrays is not known.

EDIT: Here's a snippet of my code so far (pulling data from a DB):

<?php
$ufSubmissions = $wpdb->get_results( $wpdb->prepare("SELECT * FROM wp_user_feedback WHERE user = '$ufUser' ORDER BY date DESC") );


$cleanedResponses = array();

foreach ($ufSubmissions as $submission) {
    $cleanedResponses[] = unserialize($submission->responses);
}

array_map(null, $cleanedResponses));
?>

Doesn't seem to be working though, even $cleaned responses is an array of arrays.

3
  • 2
    please post your code that you have attempted so far. Commented Sep 13, 2011 at 15:07
  • 2
    I would take a look at the answers provided here: stackoverflow.com/questions/2815162/… Commented Sep 13, 2011 at 15:14
  • please post var_export() of expected result Commented Sep 13, 2011 at 15:14

7 Answers 7

2

Mostly like Alex Barrett's answer, but allows for an unknown number of arrays.

<?php

$values = array(
    array(1,2,3,4,5),
    array(6,7,8,9,10),
    array(11,12,13,14,15),
);

function array_pivot($values)
{
    array_unshift($values, null);
    return call_user_func_array('array_map', $values);

}

print_r(array_pivot($values));
Sign up to request clarification or add additional context in comments.

Comments

1

If your arrays are all the same length, you can pass as many as you want to the array_map function with null as the callback parameter.

array_map(null,
          array(1, 2, 3, 4, 5),
          array(6, 7, 8, 9, 10),
          array(11, 12, 13, 14, 15));

The above will return the following two-dimensional array:

array(array(1, 6, 11),
      array(2, 7, 12),
      array(3, 8, 13),
      array(4, 9, 14),
      array(5, 10, 15));

This is a documented trick, so quite safe to use.

3 Comments

normally I would say thats right, but the OP doesnt know how many arrays he will have.
because you would have to load in an unknown number of arrays into array_map. If these are programmatically generated arrays there is no way to do that.
I should clarify... the arrays aren't known ahead of time... but I'm getting arrays from a DB so at some point, I'll know how mand arrays were retrieved.
0
$ret = array();
for ($i =0; $i < count($input[0]); $i++){
    $tmp = array();
    foreach ($input as $array) {
        $tmp[] = $array[$i];
    }
    $ret[] = $tmp;      
}

Comments

0

Em... What's the problem? If they are equal sized, then you do

<?php
$a = array(1,2,3,4,5);
$b = array(6,7,8,9,10);
$c = array(11,12,13,14,15);

$d = array();

for ($i = 0; $i < sizeof($a); $i++) {
  $d[] = array($a[$i], $b[$i], $c[$i]);
}

var_dump($d);

3 Comments

doesn't quite work. $d ends up with count($a) copies of $a $b and $c.
Fixed: $a[$i] instead of $a, same for $b and $c
If I don't know how many arrays I'll have, then I'll need to add additional statements, no?
0

This is not tested, read it to get the idea instead of paste it.

The point is to put everything alltoghether in a feed and then redistribute it onto new arrays of a max length, the last one could not be full.

<?php

  // initial vars

  $max_size = 3; // of the new arrays
  $total_array = $a + $b + $c; // the three arrays summed in the right order
  $current_size = length($total_array);
  $num_of_arrays = ceil($current_size / $max_size);

  // redistributing

  $result_arrays = array();
  for($i = 0; $i < $num_of_arrays; $i++){ // iterate over the arrays
    $new_array= array();
    for($t = 0; $t < $max_size){
     $pos = $num_of_arrays * $t + $i;
     if(isset($total_array[$pos]) {
       $new_array[] = $total_array[$pos];
     }
    }
    $result_arrays[] = $new_array;
  }
?>

Comments

0
// This takes an unlimited number of arguments and merges into arrays on index
// If there is only 1 argument it is treated as an array of arrays
// returns an array of arrays
function merge_on_indexes () {
  $args = func_get_args();
  $out = array();
  if (count($args) == 1) for ($i = 0; isset($args[0][$i]); $i++) for ($j = 0; isset($args[0][$i][$j]); $j++) $out[$j][] = $args[0][$i][$j]; else for ($i = 0; isset($args[$i]); $i++) for ($j = 0; isset($args[$i][$j]); $j++) $out[$j][] = $args[$i][$j];
  return $out;
}

// Usage examples
// Both return array('data1','data3','data5'),array('data2','data4','data6')

$arr1 = array('data1','data2');
$arr2 = array('data3','data4');
$arr2 = array('data5','data6');
$result = merge_on_indexes($arr1,$arr2);
print_r($result);

$multiDimArr = array(
  array('data1','data2'),
  array('data3','data4'),
  array('data5','data6')
);
$result = merge_on_indexes($multiDimArr);
print_r($result);

Comments

-1
$arr = get_defined_vars();  //gets all your variables
$arrCount = 0;

$arrOfarrs = array();

foreach($arr as $var){    //go through each variable
    if(is_array($var)){   //and see if it is an array
       $arrCount++;       //we found another array
       for($i == 0;$i < count($var); $i++){   //run through the new array
           $arrOfarrs[$i][] == $var[$i];   //and add the corresponding elem
       } 

    }
}   

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.