user915218
user915218

Reputation: 53

Simplify a multidimensional array in PHP

I've received and XML, converted it into an array for usage. The XML comes in unpredictable multi dimension when I convert it into array. Had been looking around but could not find a suitable solution. An alternative is to simplify the converted array.

I've converted an XML to array in PHP, and the result looked like this:

Array
(
    [GetMLCBRes] => Array
        (
            [0] => Array
                (
                    [Ord] => Array
                        (
                            [0] => Array
                                (
                                    [OrdId] => Array
                                        (
                                            [0] => DP Order ID
                                        )
                                )
                        )
                    [ReqInf] => Array
                        (
                            [0] => Array
                                (
                                    [ReqDat] => Array
                                        (
                                            [0] => Date of Request
                                        )
                                )
                        )
                    [Res] => Array
                        (
                            [0] => PDF Report
                        )
                )
        )
)

May I know how to drop the index like [0] but remain the assoc keys like [Ord], [OrdId], [ReqInf] and [Res], etc.

How to convert it to become like this?

Array
(
    [GetMLCBRes] => Array
        (
            [Ord] => Array
                (
                    [OrdId] => DP Order ID
                )
           [ReqInf] => Array
                (
                    [ReqDat] => Date of Request
                )
            [Res] => PDF Report
        )
)

Upvotes: 3

Views: 184

Answers (2)

Calimero
Calimero

Reputation: 4298

This should work as expected :

function recursive_skip(array $ary) {
    foreach($ary as $k => &$v) {
        if (is_array($v)) {
            // Skip it
            $v = $v[0];
        }
        if (is_array($v)) {
            // If current array item is an array itself, recursively call the function on it
            $v = recursive_skip($v);
        }
    }

    // Return updated array to calling context
    return $ary;

}

$source = Array(
    'GetMLCBRes' => Array(Array(
        'Ord' => Array(Array(
                        'OrdId' => Array('DP Order ID')
        )),
        'ReqInf' => Array(Array(
                        'ReqDat' => Array('Date of Request')
        )),
        'Res' => Array('PDF Report')
    ))
);

$dest = recursive_skip($source);
var_dump($dest);

A few caveats : the function will only skip one array level each time (but could be adapted to handle more if needed) and might come with a significant performance cost if your source array is huge since it's recursive (O(n)), it just walks through the whole array tree.

Upvotes: 0

Spoke44
Spoke44

Reputation: 988

it works but if you change your structure maybe it won't. It's not optimized too :)

$input = Array(
    'GetMLCBRes' => Array(Array(
        'Ord' => Array(Array(
                        'OrdId' => Array('DP Order ID')
        )),
        'ReqInf' => Array(Array(
                        'ReqDat' => Array('Date of Request')
        )),
        'Res' => Array('PDF Report')
    ))
);

foreach($input as &$in){
    $sub = $in[0];
    foreach($sub as $key => &$value){
        $sub2 = $value[0];
        if(!is_array($sub2)){
            $sub[$key] = $sub2;
            continue;
        }
        $final2 = array();
        foreach($sub2 as $key2 => $final)
            $final2[$key2] = $final[0];
        $sub[$key] = $final2;
    }
    $in = $sub;
}

var_dump($input);

You can test it here : http://sandbox.onlinephpfunctions.com/code/a6770c7649d7d277aa1dc3544093cc87bed0951d

Upvotes: 1

Related Questions