phparraysmultidimensional-arrayreduce

Remove an unnecessary level from an multidimensional array and transcribe keys


I have converted xml string into php array. But the requirement is something different. Following is an array which is created using XML string by following

$xml = simplexml_load_string($mystin, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

Output:

Array
(
    [0] => Array
        (
            [data] => Array
                (
                    [0] => Array
                        (
                            [attribute_set] => default
                            [description] => productdescription
                            [is_in_stock] => 1
                            [meta_description] => Product meta description
                            [meta_keyword] => Product meta keyword
                            [meta_title] => Product meta title
                            [name] => myproduct6
                            [price] => 100
                            [qty] => 1000
                            [re_skus] => sdfefwef
                            [short_description] => this is a short descriptio
                            [sku] => myproduct_surabhi7
                            [status] => 1
                            [store] => admin
                            [tax_class_id] => 4
                            [type] => simple
                            [url_key] => my-product
                            [url_path] => my-product.html
                            [visibility] => 4
                            [weight] => 10
                        )

                    [1] => Array
                        (
                            [attribute_set] => default
                            [description] => productdescription
                            [is_in_stock] => 1
                            [meta_description] => Product meta description
                            [meta_keyword] => Product meta keyword
                            [meta_title] => Product meta title
                            [name] => myproduct6
                            [price] => 100
                            [qty] => 1000
                            [re_skus] => sdfefwef
                            [short_description] => this is a short descriptio
                            [sku] => myproduct_surabhi7
                            [status] => 1
                            [store] => admin
                            [tax_class_id] => 4
                            [type] => simple
                            [url_key] => my-product
                            [url_path] => my-product.html
                            [visibility] => 4
                            [weight] => 10
                        )

                )

        )

)

I need the above array as following array:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => Product1
                    [sku] => Product1
                    [description] => Product description
                    [short_description] => Product short description
                    [weight] => 10
                    [status] => 1
                    [url_key] => wat12
                    [url_path] => wat12.html
                    [visibility] => 4
                    [price] => 100
                    [tax_class_id] => 4
                    [meta_title] => Product meta title
                    [meta_keyword] => Product meta keyword
                    [meta_description] => Product meta description
                    [store] => admin
                    [attribute_set] => default
                    [type] => simple
                    [is_in_stock] => 1
                    [color] => Orange
                    [re_skus] => Testsimple2,Testsimple1
                )

        )

    [1] => Array
        (
            [1] => Array
                (
                    [name] => Product2
                    [sku] => Product2
                    [description] => Product description
                    [short_description] => Product short description
                    [weight] => 10
                    [status] => 1
                    [url_key] => wat12
                    [url_path] => wat12.html
                    [visibility] => 4
                    [price] => 100
                    [tax_class_id] => 4
                    [meta_title] => Product meta title
                    [meta_keyword] => Product meta keyword
                    [meta_description] => Product meta description
                    [store] => admin
                    [attribute_set] => default
                    [type] => simple
                    [is_in_stock] => 1
                    [color] => Orange
                    [re_skus] => Testsimple2,Testsimple1
                )

        )

)

Solution

  • Try this may be it help

    $array = array(
        0 => array(
            'data' => array(0 => array('abc2' => 1, 'adsg' =>2),1=>array('abc' => 1, 'adsg4' =>2)))
    );
    $new_data = array();
    foreach ($array as $row)
    {
        for($i=0; $i<count($row['data']); $i++){
        $newArray = array();    
       $newArray[$i] = $row['data'][$i];
       $new_data[] = $newArray;
        }
    }
    
    print_r($new_data);