zourbuth
zourbuth

Reputation: 908

Arrange PHP Multidimensional Array By Inner Index

How to arrange this array by last inner index ( 0, 1, 2 ) and get the value of the last inner index as the value of each second index:

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

            [image] => Array
                (
                    [0] => 
                    [1] => 
                    [2] => 
                )

            [align] => Array
                (
                    [0] => left
                    [1] => right
                    [2] => left
                )

            [title] => Array
                (
                    [0] => 
                    [1] => 
                    [2] => 
                )

            [content] => Array
                (
                    [0] => 
                    [1] => 
                    [2] => 
                )

        )

)

And have the results as below:

Array
(
    [text] => Array
        (
            [0] => Array
                (
                    [grid] => 3
                    [image] => 
                    [align] => left
                    [title] => 
                    [content] => 
                )

            [1] => Array
                (
                    [grid] => 4
                    [image] => 
                    [align] => right
                    [title] => 
                    [content] => 
                )

            [2] => Array
                (
                    [grid] => 5
                    [image] => 
                    [align] => left
                    [title] => 
                    [content] => 
                )

        )

)

Upvotes: 0

Views: 197

Answers (3)

Strae
Strae

Reputation: 19445

As SiGanteng suggested, i dont see other ways than a for/foreach loop:

function buildArray($source, $key = false)
{
    // Build the new array
    $new_array = array();
    // Define groups
    $groups = $key === false ? array_keys($source) : array($key);

    foreach($groups AS $group)
    {
        // Get the keys
        $keys = array_keys($array[$group]);
        // Count the values
        $num_entries = count($array[$group][$keys[0]]);

        for($i = 0; $i < $num_entries; $i++)
        {
            foreach($keys AS $key)
            {
                $new_array[$group][$i][$key] = $array[$group][$key][$i];
            }
        }
    }
    return $new_array;
}

This allow you to define the key you need to build; If not specified, the function build the array for every key.

Upvotes: 1

FatalError
FatalError

Reputation: 964

This should work.

function formatit($arr) {

  $new = array();

  foreach($arr as $k=>$v) {
    foreach($v as $k1=>$v1) {
     $new[$k1][$k] = $v1;
    }
  }
  return $new;
}

Tested. Call it as

$arr['text'] = formatit($arr['text']);

http://ideone.com/rPzuR

Upvotes: 0

swapnilsarwe
swapnilsarwe

Reputation: 1300

This will do the work

function restructure($arr){
    $newArr = array();
    foreach($arr as $k => $v){
        foreach($v as $k1 => $v1){
            foreach($v1 as $k2 => $v2){
                $newArr[$k][$k2][$k1] = $v2; 
            }
        }
    }
    return $newArr;
}

Upvotes: 2

Related Questions