我已将 xml 字符串转换为 php 数组。但要求有所不同。以下是使用 XML 字符串创建的数组,如下
$xml = simplexml_load_string($mystin, "SimpleXMLElement", LIBXML_NOCDATA); $json = json_encode($xml); $数组= json_decode($json,TRUE);
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);
我从凯瓦尔的回答中得到了想法。做了一个小小的改变后,它就很有魅力了。
$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);
我担心这个问题的例子不够复杂。另外,我不明白为什么您要费心在结果数组的两个级别中保留原始子数组索引。 最后,我不明白当 2 维数组可以保存重要数据时创建 3 维数组的意义。 演示
$result = [];
foreach ($array[0]['data'] as $i => $data) {
$result[$i][$i] = $data;
}
var_export($result);