有很多答案,但我似乎無法得到這個權利,畢竟,這個帖子看起來很大,但似乎只是。這就是我所擁有的,以及我所嘗試過的。如何通過使用一個鍵來組合兩個數組?
Array
(
[0] => image 1
[1] => image 2
)
Array
(
[name] => Array
(
[0] => 0.14997300-1503597010599f11d2249df30.jpg
[1] => 0.24654000-150113339659797a543c31f24.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[tmp_name] => Array
(
[0] => C:\Users\--\AppData\Local\Temp\php509E.tmp
[1] => C:\Users\--\AppData\Local\Temp\php509F.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 55560
[1] => 9425
)
)
我已經試過:
$instructions = $_POST['instructions'];
$image = $_FILES['instructions_image'];
$result = array();
foreach($instructions as $index => $key){
$t = array();
foreach($image as $img){
$t[] = $img;
}
$result[$key] = $t;
}
而且結果是:
Array
(
[image 1] => Array
(
[0] => Array
(
[0] => 0.14997300 1503597010599f11d2249df30.jpg
[1] => 0.24654000 150113339659797a543c31f24.jpg
)
[1] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[2] => Array
(
[0] => C:\Users\--\AppData\Local\Temp\phpBAD5.tmp
[1] => C:\Users\--\AppData\Local\Temp\phpBAE6.tmp
)
[3] => Array
(
[0] => 0
[1] => 0
)
[4] => Array
(
[0] => 55560
[1] => 9425
)
)
[image 2] => Array
(
[0] => Array
(
[0] => 0.14997300 1503597010599f11d2249df30.jpg
[1] => 0.24654000 150113339659797a543c31f24.jpg
)
[1] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[2] => Array
(
[0] => C:\Users\--\AppData\Local\Temp\phpBAD5.tmp
[1] => C:\Users\--\AppData\Local\Temp\phpBAE6.tmp
)
[3] => Array
(
[0] => 0
[1] => 0
)
[4] => Array
(
[0] => 55560
[1] => 9425
)
)
)
我不確定爲什麼結果有相同的2兩個指標,但我還想知道我們如何保留$ _FILES提供的數組鍵名,例如:name,type,tmp_name,error和size。
以下是我期待的內容(我包含了額外的信息,例如我沒有提供的密鑰名稱,也沒有用我的代碼,對此感到抱歉,一直停留了12個小時,但只是任何解釋來設置我在正確的道路將幫助我巨大):
Array
(
[0] => Array
(
[text] => image 1,
[image_data] => Array (
[name] => 0.14997300 1503597010599f11d2249df30.jpg
[type] => image/jpeg
[tmp_name] => C:\Users\--\AppData\Local\Temp\php509E.tmp
[error] => 0
[size] => 55560
)
)
[1] => Array
(
[text] => image 2,
[image_data] => Array (
[name] => 0.24654000 150113339659797a543c31f24.jpg
[type] => image/jpeg
[tmp_name] => C:\Users\--\AppData\Local\Temp\php509E.tmp
[error] => 0
[size] => 9425
)
)
)
請分享您預期的結果。 –
要保留密鑰名稱,請檢查內部的'foreach()'循環。您正在跳過該數組的索引,並通過'[]' – insider
構建帶有數字索引的新數組感謝您指出@insider – Craig