我正在尝试通过将“ s”和“ d”键字符串值替换为其各自的键“ bandnumber”数组来从关联数组构建多维数组树,但似乎无法破解它。我只能使它适用于数组的第一个节点。
例如,我有以下数组:
$coiArray = array (
array('bandnumber' => '02-BELG-2129929', 's' =>'94-BELG-3237180', 'd' => '96-BELG-3156295' ),
array('bandnumber' => '94-BELG-3237180', 's' =>'88-BELG-3206112', 'd' => '88-BELG-3206173' ),
array('bandnumber' => '88-BELG-3206112', 's' =>'81-BELG-3238253', 'd' => '87-BELG-3008002' ),
array('bandnumber' => '88-BELG-3206173', 's' =>'', 'd' => '' ),
array('bandnumber' => '96-BELG-3156295', 's' =>'88-BELG-3206112', 'd' => '85-BELG-3049648' ),
array('bandnumber' => '85-BELG-3049648', 's' =>'', 'd' => '' ),
array('bandnumber' => '81-BELG-3238253', 's' =>'', 'd' => '' ),
array('bandnumber' => '87-BELG-3008002', 's' =>'', 'd' => '' ),
);
我正在尝试以编程方式将上述数组转换为以下多维数组树:
$coiNestedArray = array('bandnumber' => '02-BELG-2129929',
's' => array('bandnumber' => '94-BELG-3237180',
's' => array('bandnumber' => '88-BELG-3206112',
's' => array('bandnumber' => '81-BELG-3238253',
's' =>'',
'd' => ''
),
'd' => array('bandnumber' => '87-BELG-3008002',
's' =>'',
'd' => ''
)
),
'd' => array('bandnumber' => '88-BELG-3206173',
's' =>'',
'd' => ''
)
),
'd' => array('bandnumber' => '96-BELG-3156295',
's' => array('bandnumber' => '88-BELG-3206112',
's' => array('bandnumber' => '81-BELG-3238253',
's' =>'',
'd' => ''
),
'd' => array('bandnumber' => '87-BELG-3008002',
's' =>'',
'd' => ''
)
),
'd' => array('bandnumber' => '85-BELG-3049648',
's' =>'',
'd' => ''
)
)
);
这是我到目前为止最接近的,但是它只会更新数组的第一个节点:
function findKey($coiarray, $bandnumber){
$thisCol = array_column($coiarray, 'bandnumber');
$found_key = array_search($bandnumber, $thisCol);
return $found_key;
}
foreach ($coiArray as $key => $value) {
$s = '';
$found_key = findKey($coiArray,$coiArray[$key]['s']);
if(isset($coiArray[$found_key])){
$s = $coiArray[$found_key];
}
$d = '';
$found_key = findKey($coiArray,$coiArray[$key]['d']);
if(isset($coiArray[$found_key])) {
$d = $coiArray[$found_key];
}
$coiArray[$key] = array('bandnumber' => $coiArray[$key]['bandnumber'], 's' => $s, 'd' => $d );
}
我将重新排列数组的整个转储,但这是$coiArray
中var_dump($coiArray)
的第一个节点,您会注意到所有最内层的嵌套[“ s“]和[” d“]键是字符串,而不是它们各自的数组。
[0]=>
array(3) {
["bandnumber"]=>
string(15) "02-BELG-2129929"
["s"]=>
array(3) {
["bandnumber"]=>
string(15) "94-BELG-3237180"
["s"]=>
string(15) "88-BELG-3206112"
["d"]=>
string(15) "88-BELG-3206173"
}
["d"]=>
array(3) {
["bandnumber"]=>
string(15) "96-BELG-3156295"
["s"]=>
string(15) "88-BELG-3206112"
["d"]=>
string(15) "85-BELG-3049648"
}
}
下面的示例是$coiNestedArray
中的第一个节点,我手动创建了该节点,以说明我要实现的目标。请注意,每个[“ s”]和[“ d”]都是从$coiArray
派生的数组。
array(3) {
["bandnumber"]=>
string(15) "02-BELG-2129929"
["s"]=>
array(3) {
["bandnumber"]=>
string(15) "94-BELG-3237180"
["s"]=>
array(3) {
["bandnumber"]=>
string(15) "88-BELG-3206112"
["s"]=>
array(3) {
["bandnumber"]=>
string(15) "81-BELG-3238253"
["s"]=>
string(0) ""
["d"]=>
string(0) ""
}
["d"]=>
array(3) {
["bandnumber"]=>
string(15) "87-BELG-3008002"
["s"]=>
string(0) ""
["d"]=>
string(0) ""
}
}
["d"]=>
array(3) {
["bandnumber"]=>
string(15) "88-BELG-3206173"
["s"]=>
string(0) ""
["d"]=>
string(0) ""
}
}
我该如何解决这个问题?
答案 0 :(得分:1)
您将需要创建一个以bandnumbers为键的关联数组,因此您可以直接通过bandnumbers查找行。然后访问这些子项,并通过引用将每个子项替换为该关联数组中的相应值。
(可选)检测哪个带号从未作为子级被引用:它是根。但是,如果您知道根带号,或者知道它始终是第一输入行中的根带号,则可以跳过最后一步。最后提取该根的值(假设正好有一个):
Select distinct (customer_login),
MAX(delivery_date_local) as Last_order_date,
max(case when seqnum = 2 then
delivery_date_local end) as Penultimate_order_date
from (select
^^^