1

I have a multidimensional array:

Array
(
[type] => Array
(
    [0] => text
    [1] => portfolio
    [2] => slide
    [3] => text
)

[grid] => Array
(
    [0] => 3
    [1] => 5
    [2] => 3
    [3] => 4
)

[title] => Array
(
    [0] => title1
    [3] => title2
)

[content] => Array
(
    [0] => content1
    [3] => content2
)

[item] => Array
(
    [1] => 6
    [2] => 7
)

[pagination] => Array
(
    [1] => 8
)

[order] => Array
(
    [1] => desc
    [2] => asc
)

)

And want to group it by [type] key given in the array:

Array (

[0] => Array (
        [type] => text
        [grid] => 3
        [title] => title1
        [content] => content1
    )

[1] => Array (
        [type] => portfolio
        [grid] => 5
        [item] => 6
        [pagination] => 1
        [order] => desc
    )

[2] => Array (
        [type] => slide
        [grid] => 3
        [item] => 7
        [order] => asc
    )

[3] => Array (
        [type] => text
        [grid] => 4
        [title] => title2
        [content] => content2
    )

Is there a way or PHP function to do array grouping like that?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
zourbuth
  • 898
  • 7
  • 16

3 Answers3

6

This snippet achieves that:

$result = array();
foreach ($array as $key => $data) {
    foreach ($data as $offset => $value) {
        if (isset($result[$offset])) {
            $result[$offset][$key] = $value;
        } else {
            $result[$offset] = array($key => $value);
        }
    }
}

Working DEMO

xkeshav
  • 53,360
  • 44
  • 177
  • 245
Florent
  • 12,310
  • 10
  • 49
  • 58
1

array_map() with null for the callback will do exactly what you want. However it will have number for the index instead of names.

If you write your own callback then you can return an array with the names you need.

Since apparently people want the actual code:

array_map(null, $type_array, $grid_array, $title_array, $content_array, $item_array);

It really is as simple as that. Most of the other answers are so large and unnecessary.

Note: This assumes a fixed number of arrays - if it's not fixed then this won't work, and then go with Florent's answer.

Ariel
  • 25,995
  • 5
  • 59
  • 69
  • @diEcho I was still editing it. And it's exactly the answer to his question, so the downvote is unwarranted. – Ariel Jul 12 '12 at 08:49
  • I don't see how `array_map` could become handy in here. Maybe refine your answer and give an example on how to call `array_map` in this specific case. – Christoph Winkler Jul 12 '12 at 09:08
  • @Ariel Ok, for a fixed number of arrays this would be a much more beautiful solution than looping through the arrays. Nevertheless I was hoping for a way to get through one array :). Removed the downvote. – Christoph Winkler Jul 12 '12 at 09:42
  • @Ariel The portfolio and slide type doesn't have "title" or "content" index. – zourbuth Jul 12 '12 at 09:50
  • This answer is unreliable for the sample data because it is not a matrix. The fact that there are gaps in the data structure means that this transposition technique will end up mangling the data and not respecting the original associative keys. – mickmackusa Apr 22 '22 at 19:22
0

You can do it with this function:

$type_array = array('text', 'portfolio', 'slide', 'text');
$grid_array = array(3, 5, 3, 4);
$title_array = array(0 => 'title1', 3 => 'title2');
$content_array = array(0 => 'content1', 3 => 'content2');
$item_array = array(1 => 6, 2 => 7);


function group_arrays($type_array, $grid_array, $title_array, $content_array, $item_array) {
    $temp_array = array();

    for($i = 0; $i < count($type_array); $i++) {
        $temp_array[$i] = array( 'type' => @$type_array[$i],
                                 'grid' => @$grid_array[$i],
                                 'title' => @$title_array[$i],
                                 'content' => @$content_array[$i],
                                 'item' => @$item_array[$i]);
    }

    return $temp_array;
}

print_r(group_arrays($type_array, $grid_array, $title_array, $content_array, $item_array));

Hope this helps!

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sven van Zoelen
  • 6,989
  • 5
  • 37
  • 48
  • The function produces "Undefined offset" and create index for empty value. Eq: it produces "title" and "content" index for portfolio type. – zourbuth Jul 12 '12 at 08:59
  • The arrays must be of the same length. Updated the awnser. Don't understand why it getting downvoted, because the function works :/. But the answer of Florent is a better solution. – Sven van Zoelen Jul 12 '12 at 09:04
  • The function create unavailable index and value for each type. – zourbuth Jul 12 '12 at 09:09
  • Great work, @Sven. But I want it only shows the index and value given in the array. – zourbuth Jul 12 '12 at 09:49