2017-08-31 169 views
1

有很多答案,但我似乎無法得到這個權利,畢竟,這個帖子看起來很大,但似乎只是。這就是我所擁有的,以及我所嘗試過的。如何通過使用一個鍵來組合兩個數組?

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 
      ) 

     ) 

) 
+2

請分享您預期的結果。 –

+0

要保留密鑰名稱,請檢查內部的'foreach()'循環。您正在跳過該數組的索引,並通過'[]' – insider

+0

構建帶有數字索引的新數組感謝您指出@insider – Craig

回答

1

希望這一個將有所幫助。這裏我們使用簡單foreach循環array_keysarray_combinearray_column

Try this code snippet here

$result=array(); 
foreach($images as $key => $image) 
{ 
    $result[]=array(
     "text"=>$image, 
     "image_data"=>array_combine(
       array_keys($instructions), 
       array_column($instructions,$key)) 
    ); 
} 
print_r($result); 
+1

儘管它很有幫助,但我去閱讀理解爲什麼我得到了不同的結果,即使我寫了與你共享的完全相同的代碼,發現你簡單地混合了指令和圖像變量名哈哈!這是完美的,謝謝你指導我在正確的方向Sahil。 – Craig

+1

@SahilGulati你的腳本丟失了'image_data'子項目鍵( - ; – Neodan

+0

@John很高興幫助你我的朋友...... :) –

2

你需要寫一個你的邏輯組合這些陣列自定義腳本。對於這個任務,你可以使用這個數組函數:array_combine,array_keys,array_column

實施例:

<?php 
$a1 = ['image 1', 'image 2']; 
$a2 = [ 
    'name' => ['0.14997300-1503597010599f11d2249df30.jpg', '0.24654000-150113339659797a543c31f24.jpg'], 
    'type' => ['image/jpeg', 'image/jpeg'], 
    'tmp_name' => ['C:\Users\--\AppData\Local\Temp\php509E.tmp', 'C:\Users\--\AppData\Local\Temp\php509F.tmp'], 
    'error' => [0, 0], 
    'size' => [55560, 9425] 
]; 

$result = []; 
foreach ($a1 as $k => $v) { 
    $result[] = [ 
     'text' => $v, 
     'image_data' => array_combine(array_keys($a2), array_column($a2, $k)) 
    ]; 
} 

print_r($result); 
0

這將幫助你 $解析度=陣列(0 => '圖像1',1 => '圖像2');

  $valu=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) 
        ); 

      $newarr=array(); 
      foreach($res as $key=>$val) 
      { 
       $newarr[$key]['text']=$val; 
       $newarr[$key]['image_data']=array('name'=>$valu['name'][$key],'type'=>$valu['type'][$key],'tmp_name'=>$valu['tmp_name'][$key],'error'=>$valu['error'][$key],'size'=>$valu['size'][$key]); 
      } 
      echo '<pre>'; 
      print_r($newarr); 
0

試試這個

$fileData = array(
    'name' => ['0.14997300-1503597010599f11d2249df30.jpg','0.24654000-150113339659797a543c31f24.jpg'], 
    'type' => ['image/jpeg','image/jpeg'], 
    'tmp_name' => ['C:\Users\--\AppData\Local\Temp\php509E.tmp','C:\Users\--\AppData\Local\Temp\php509F.tmp'], 
    'error' => [0,0], 
    'size' => [55560,9425]); 

$someArr = array('image 1','image 2'); 
$fileData['somedata'] = $someArr; 


function reFileData($fileData) { 
    $arr = array(); 
    $keys = array_keys($fileData); 
    for ($i=0; $i < count($fileData['name']); $i++) { 
     foreach ($keys as $key) { 
      $arr[$i][$key] = $fileData[$key][$i]; 
     } 
    } 
    return $arr; 
} 

print_r(reFileData($fileData)); 
相關問題