joskes
joskes

Reputation: 61

reorder multidimensional php array trouble

I need to reorder an multidimensional array in php but I can't get it right and I am struggling with foreach, for and while loops for some time now.

This is what I have:

Array
(
 [dimension] => Array
    (
        [dimension.part] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [0] => geheel
                    )

                [1] => SimpleXMLElement Object
                    (
                        [0] => geheel
                    )

            )

        [dimension.type] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [0] => hoogte
                    )

                [1] => SimpleXMLElement Object
                    (
                        [0] => breedte
                    )

            )

        [dimension.value] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [0] => 73
                    )

                [1] => SimpleXMLElement Object
                    (
                        [0] => 84
                    )

            )

        [dimension.unit] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [0] => cm
                    )

                [1] => SimpleXMLElement Object
                    (
                        [0] => cm
                    )

            )

    )

[material] => Array
    (
        [material.part] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [0] => deelmateriaal
                    )

                [1] => SimpleXMLElement Object
                    (
                        [0] => deelmateriaal2
                    )

            )

        [material_type] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [0] => typemateriaal
                    )

                [1] => SimpleXMLElement Object
                    (
                        [0] => typemateriaal2
                    )

            )

        [material] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [0] => materiaal
                    )

                [1] => SimpleXMLElement Object
                    (
                        [0] => materiaal2
                    )

            )

        [material.notes] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [0] => notemateriaal
                    )

                [1] => SimpleXMLElement Object
                    (
                        [0] => notemateriaal2
                    )

            )

    )

)

And I need to transform it to:

dimensions => Array ( 1 => array(dimension.part => geheel) 
                        => array (dimension.type => hoogte) )
              .....
           => Array  ( 2 => array(dimension.part => ...)
                         => array (dimension.type => ...) )
              ....
             ....
material   => Array ( 1 => ...
                    2 => ...
           => Array ( 1 => 
           =>
             ....

Anyone got a good approach to this?

Thanks,

Joris

Upvotes: 0

Views: 105

Answers (2)

alex
alex

Reputation: 1297

    $new_array = new array();
    $count = count($array['dimension']['dimension_part']);
    for($i=0;$i<$count;$i++) {
      $new_array[$i]['dimension.part'] = $array['dimension']['dimension.part'][$i][0];
      $new_array[$i]['dimension.type'] = $array['dimension']['dimension.type'][$i][0];
      $new_array[$i]['dimension.value'] = $array['dimension']['dimension.value'][$i][0];
    }

I am not quite sure how you access simplexmlobjects, so I used the array type. maybe change it to [$i] -> 0

Upvotes: 0

stefandoorn
stefandoorn

Reputation: 1032

foreach($arr['dimension'] as $keyType => $type) {
    foreach($type as $key => $value) {
        $aNewArray[$key][$keyType] = $value;
    }
}

Something like that.. And then process it a bit more to suit your needs..

What it does: walk through every dimension array (and store the key of it in $keyType) and then walk through every item and add it to a new array in the way you want it. It assumes every type has the same amount of items under it.

Upvotes: 1

Related Questions