0

I have this array

Array
        (
            [0] => Array
                (
                    [post_category] => cat-1
                )

            [1] => Array
                (
                    [posts_count] => 33
                )

            [2] => Array
                (
                    [cells_num] => 1
                )

            [3] => Array
                (
                    [grid_num] => 0
                )

            [4] => Array
                (
                    [post_category] => cat-2
                )

            [5] => Array
                (
                    [posts_count] => 334
                )

            [6] => Array
                (
                    [cells_num] => 1
                )

            [7] => Array
                (
                    [grid_num] => 0
                )

            [8] => Array
                (
                    [post_category] => cat-3
                )

            [9] => Array
                (
                    [posts_count] => 321
                )

            [10] => Array
                (
                    [cells_num] => 2
                )

            [11] => Array
                (
                    [grid_num] => 0
                )

        )

note the repeated keys in the children arrays
I want to output something like

<?php

 array(
  [0] => array(
   [post_category] => cat-1
   [posts_count] => 33
   [cells_num] => 1
   [grid_num] => 0
  ),
  [1] => array(
  [post_category] => cat-2
  [posts_count] => 334
  [cells_num] => 1
  [grid_num] => 0
  ),
  [2] => array(
   [post_category] => cat-3
   [posts_count] => 321
   [cells_num] => 2
   [grid_num] => 0
  )
 );


?>

Edited by Amr87

2
Contributors
2
Replies
13
Views
3 Years
Discussion Span
Last Post by Amr87
1

I assume $sourcearr is the array you shown above, your result array is $destarr

    $rowindex=-1;
    foreach($sourcearr as $key => $valuearr) 
    { 
        if ($key%4==0)
            $rowindex++;
        $value=current($valuearr);
        $destarr[$rowindex][key($valuearr)]=$value;   
    }


    echo "<pre>";
    print_r($destarr);
    echo "</pre>";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.