我正在編寫(xiě)一個(gè)代碼,將一個(gè)數(shù)組導(dǎo)入到另一個(gè)數(shù)組中。將持續(xù)存在的陣列汽車(chē):array ( 0 => array ( 'Brand' => 'Volvo', 'Stock Value' => 100, 'Models' => 96, ), 1 => array ( 'Brand' => 'BMW', 'Stock Value' => 60, 'Models' => 59, ), 2 => array ( 'Brand' => 'Toyota', 'Stock Value' => 110, 'Models' => 100, ),)我要導(dǎo)入的數(shù)組 emobile:array ( 0 => array ( 'Maker' => 'Tesla', 'Price' => 100, 'Factories' => 96, 'Employees' => 10, ), 1 => array ( 'Maker' => 'Nikola', 'Price' => 60, 'Factories' => 59, 'Employees' => 10, ), 2 => array ( 'Maker' => 'Thor', 'Price' => 110, 'Factories' => 100, 'Employees' => 10, ),)發(fā)布數(shù)據(jù)(鍵 = 目標(biāo)結(jié)構(gòu),值 = 當(dāng)前結(jié)構(gòu),---未分配):$pst_dataarray ( 'Brand' => 'Maker', 'Stock_Value' => '---', 'Models' => '---', 'flag' => '---',)我的代碼。在當(dāng)前階段,我可以讓它碰巧顯示導(dǎo)入數(shù)組中的值,例如制造商的名稱(chēng),或者我選擇兩個(gè)(“股票價(jià)值”作為第二個(gè)),并且有結(jié)構(gòu),但沒(méi)有值。foreach ($kar as $xk => $vx) { if(!in_array($vx, $pst_data)){ foreach ($emobile as &$xkone) { unset($xkone[$vx]); } } else { foreach ($pst_data as $pstd => $pstd_val) { foreach ($emobile as &$xkone) { $xkone[$pstd] = $xkone[$pstd_val]; unset($xkone[$pstd_val]); } } }}我的目標(biāo)結(jié)果:Array( [0] => Array ( [Brand] => Volvo [Stock Value] => 100 [Models] => 96 [flag] => 1 ) [1] => Array ( [Brand] => BMW [Stock Value] => 60 [Models] => 59 [flag] => 1 ) [2] => Array ( [Brand] => Toyota [Stock Value] => 110 [Models] => 100 [flag] => 1 )所以我的目標(biāo)是選擇一個(gè)數(shù)組導(dǎo)入到另一個(gè)數(shù)組中。在表單中,我將導(dǎo)入數(shù)組中的鍵分配給現(xiàn)有數(shù)組,并提交它以創(chuàng)建一個(gè)具有鍵和分配的鍵作為值的數(shù)組。如果分配的值(如 Maker)存在,則此鍵將保留在導(dǎo)入的數(shù)組中。所有其他鍵都將被刪除。接下來(lái)是將鍵重命名為,例如Maker到Brand,以及Price到Stock Value。然后,我將這兩個(gè)數(shù)組與array_merge合并。如何解決使用導(dǎo)入數(shù)組中的單個(gè)和多個(gè)值的問(wèn)題?
1 回答

弒天下
TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊
切換環(huán)路。在外部循環(huán)中,遍歷數(shù)據(jù)。在內(nèi)部循環(huán)中,遍歷屬性。
對(duì)映射的項(xiàng)使用新變量。它可以在循環(huán)結(jié)束時(shí)進(jìn)行替換以修改現(xiàn)有數(shù)組。$xkone
foreach ($emobile as &$xkone) {
$car = [];
foreach ($pst_data as $pstd => $pstd_val) {
$car[$pstd] = $xkone[$pstd_val] ?? null;
}
$xkone = $car;
}
- 1 回答
- 0 關(guān)注
- 105 瀏覽
添加回答
舉報(bào)
0/150
提交
取消