PHP:将复杂多维数组转换为简单数组
我想将下面的数组转换为更简单的数组,以便更容易理解。顺便说一句,这里有不止一个订单,我只是展示了第一个,因为它已经太长了PHP:将复杂多维数组转换为简单数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我想将下面的数组转换为更简单的数组,以便更容易理解。顺便说一句,这里有不止一个订单,我只是展示了第一个,因为它已经太长了 Array ( [0] => Array ( [no] => 1 [FL] => Array ( [0] => Array ( [val] => ORDERID [content] => 2244668800
Array
(
[0] => Array
(
[no] => 1
[FL] => Array
(
[0] => Array
(
[val] => ORDERID
[content] => 2244668800
)
[1] => Array
(
[val] => Account Name
[content] => John Doe
)
[3] => Array
(
[val] => Product Details
[product] => Array
(
[0] => Array
(
[no] => 1
[FL] => Array
(
[0] => Array
(
[val] => Product Id
[content] => 1122334455
)
[1] => Array
(
[val] => Product Name
[content] => Samsung Monitor
)
)
)
[1] => Array
(
[no] => 2
[FL] => Array
(
[0] => Array
(
[val] => Product Id
[content] => 1122334456
)
[1] => Array
(
[val] => Product Name
[content] => Fractal Case
)
)
)
)
)
)
)
)
我希望它是这样的
[0] => Array(
'orderid' => '2244668800',
'accountname' => 'John Doe',
'monitorproductid' => '1122334455',
'monitor' => 'Samsung Monitor',
'caseproductid' => '1122334456',
'case' => 'Fractal Case'
)
$order = $jsonarray; //Array Shown Above
//Init Simple Array
$simplearray = array();
foreach ( $order['FL'] as $key => $val ) {
//Get Account Name
if ( $val['val'] == 'Account Name' ) {
$accountname = $val['content'];
}
//Get Order ID
if ( $val['val'] == 'ORDERID' ) {
$orderid = $val['content'];
}
//Generate Simple Array
$simplearray[] = array(
'accountname' => $accountname,
'orderid' => $orderid,
'monitorproductid' => '', //Need to Figure Out This
'monitor' => '', //Need to Figure Out This
'caseproductid' => '', //Need to Figure Out This
'case' => '' //Need to Figure Out This
);
}
我在获取orderid
和accountname
方面没有问题,但是我确实在monitorproductid
,monitor
,caseproductid
,case
方面有问题
我正在使用foreach获取orderid
和accountname
如下所示
[0] => Array(
'orderid' => '2244668800',
'accountname' => 'John Doe',
'monitorproductid' => '1122334455',
'monitor' => 'Samsung Monitor',
'caseproductid' => '1122334456',
'case' => 'Fractal Case'
)
$order = $jsonarray; //Array Shown Above
//Init Simple Array
$simplearray = array();
foreach ( $order['FL'] as $key => $val ) {
//Get Account Name
if ( $val['val'] == 'Account Name' ) {
$accountname = $val['content'];
}
//Get Order ID
if ( $val['val'] == 'ORDERID' ) {
$orderid = $val['content'];
}
//Generate Simple Array
$simplearray[] = array(
'accountname' => $accountname,
'orderid' => $orderid,
'monitorproductid' => '', //Need to Figure Out This
'monitor' => '', //Need to Figure Out This
'caseproductid' => '', //Need to Figure Out This
'case' => '' //Need to Figure Out This
);
}
提前谢谢各位朋友。我是多维数组的新手。我在不同的网站上查找过,但找不到与我类似的案例。您是如何创建阵列的?我想这就是我们想要看到的代码。。。认为您的代码正在创建一个逻辑性不强的数组,这是来自特定CRM伙伴的响应。它是使用
JSON\u decode
转换为PHP的JSON。这很烦人,但我有点喜欢这个挑战。我想改变CRM哈哈哈,返回的数据不应该如此分割,CRM是如何工作的?从逻辑上讲,它应该只返回单级数组(或多个单级数组)中的GET数据——呃,不用担心,如果是CRM,那么初始数组如何返回就不受您的控制了。最好的办法可能是在foreach循环中创建一个新数组,然后只指定key namemate,您认为可以更新我的foreach循环以显示您的意思吗。我有这个想法,但我似乎不能使它正常工作。基本上,我为产品详细信息
部分添加了一个新的foreach
循环,但我不知道如何获取值。产品
数组包含多个产品-因此是否要在新数组中为每个产品创建一个条目?那么最终会有几个项目具有相同的订单id和帐户名?