转换数组到多维数组PHP



我已经将xml字符串转换为php数组。但要求是不同的。下面是使用

创建的XML字符串数组

$xml = simplexml_load_string($mystin, "SimpleXMLElement",LIBXML_NOCDATA);$json = json_encode($xml);$数组=json_decode()美元json,真的);

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
                        )
                )
        )
)

我需要上面的数组作为以下数组:

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
                )
        )
)

试试这个可能会有帮助

$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);

看一下这个例子:

$array = array(
    98 => array(
        'City' => 'Caracas',
        'Country' => 'Venezuela',
        'Continent' => 'Latin America',
    ),
    99 => array(
        'City' => 'Cairo',
        'Country' => 'Egypt',
        'Continent' => 'Middle East',
    ),
    105 => array(
        'City' => 'Abu Dhabi',
        'Country' => 'United Arab Emirates',
        'Continent' => 'Middle East',
    ),
    106 => array(
        'City' => 'Dubai',
        'Country' => 'United Arab Emirates',
        'Continent' => 'Middle East',
    ),
    107 => array(
        'City' => 'Montreal',
        'Country' => 'Canada',
        'Continent' => 'North America',
    )
);
$newArray = array();
foreach ($array as $row)
{
   $newArray[$row['Continent']][$row['Country']][] = $row['City'];
}
print_r($newArray);

我从Keval的回答中得到了灵感。在做了一个小小的改变之后,它就像魅力一样工作了。

$new_data = array();
foreach ($array as $row)
{
    for($i=0; $i<count($row); $i++){
    $newArray = array();    
   $newArray[$i] = $row[$i];
   $new_data[] = $newArray;
    }
}
print_r($new_data);

相关内容

  • 没有找到相关文章

最新更新