如何将两个不同长度的数组组合成一个关联的多维数组?

时间:2017-05-23 00:34:55

标签: php arrays multidimensional-array associative-array array-merge

我有2个由explode()函数生成的数组。

这个数组:

Array ( 
        [0] => uploads/str/evidence/Invoice_INVCZ769885.pdf
        [1] => uploads/str/evidence/keputusantahun1.pdf 
        [2] => uploads/str/evidence/permohonan.pdf 
        [3] => uploads/str/evidence/Invoice_INVCZ7698851.pdf 
        [4] => uploads/str/evidence/keputusantahun11.pdf 
        [5] => uploads/str/evidence/permohonan1.pdf 
        [6] => uploads/str/evidence/Invoice_INVCZ7698852.pdf 
        [7] => uploads/str/evidence/Invoice_INVCZ7698853.pdf 
        [8] => uploads/str/evidence/keputusantahun12.pdf 
    )

和这个数组:

Array ( 
        [0] => Invoice_INVCZ769885.pdf 
        [1] => Invoice_INVCZ769885.pdf 
        [2] => Invoice_INVCZ769885.pdf 
        [3] => Invoice_INVCZ769885.pdf 
        [4] => Invoice_INVCZ769885.pdf 
        [5] => Invoice_INVCZ769885.pdf 
        [6] => Invoice_INVCZ769885.pdf 
        [7] => Invoice_INVCZ769885.pdf 
)

我的问题是如何将这些数组合并到此:

Array (
    [imgs] =>
        Array(
            [img] => uploads/str/evidence/Invoice_INVCZ769885.pdf,
            [file] => Invoice_INVCZ769885.pdf
        ),
    [imgs] =>
        Array(
            [img] => uploads/str/evidence/Invoice_INVCZ769885.pdf,
            [file] => Invoice_INVCZ769885.pdf
        ),
)

1 个答案:

答案 0 :(得分:1)

数组大小不匹配。我不知道这是否是发布错误,但我调整了我的代码以适应问题中的字面意思。如果您的阵列实际上大小均匀,那么我可以将我的编辑回滚到我的第一个发布的答案。

输入:

$a1=[
     'uploads/str/evidence/Invoice_INVCZ769885.pdf',
     'uploads/str/evidence/keputusantahun1.pdf',
     'uploads/str/evidence/permohonan.pdf', 
     'uploads/str/evidence/Invoice_INVCZ7698851.pdf', 
     'uploads/str/evidence/keputusantahun11.pdf', 
     'uploads/str/evidence/permohonan1.pdf', 
     'uploads/str/evidence/Invoice_INVCZ7698852.pdf', 
     'uploads/str/evidence/Invoice_INVCZ7698853.pdf', 
     'uploads/str/evidence/keputusantahun12.pdf'
    ];
$a2=[
     'Invoice_INVCZ769885.pdf',
     'Invoice_INVCZ769885.pdf',
     'Invoice_INVCZ769885.pdf', 
     'Invoice_INVCZ769885.pdf', 
     'Invoice_INVCZ769885.pdf', 
     'Invoice_INVCZ769885.pdf', 
     'Invoice_INVCZ769885.pdf', 
     'Invoice_INVCZ769885.pdf'
    ];

方法(Demo):

foreach($a1 as $i=>$v){
    $result['imgs'][$i]['img']=$v;
    if(isset($a2[$i])){$result['imgs'][$i]['file']=$a2[$i];}
}

var_export($result);

输出:

array (
  'imgs' => 
  array (
    0 => 
    array (
      'img' => 'uploads/str/evidence/Invoice_INVCZ769885.pdf',
      'file' => 'Invoice_INVCZ769885.pdf',
    ),
    1 => 
    array (
      'img' => 'uploads/str/evidence/keputusantahun1.pdf',
      'file' => 'Invoice_INVCZ769885.pdf',
    ),
    2 => 
    array (
      'img' => 'uploads/str/evidence/permohonan.pdf',
      'file' => 'Invoice_INVCZ769885.pdf',
    ),
    3 => 
    array (
      'img' => 'uploads/str/evidence/Invoice_INVCZ7698851.pdf',
      'file' => 'Invoice_INVCZ769885.pdf',
    ),
    4 => 
    array (
      'img' => 'uploads/str/evidence/keputusantahun11.pdf',
      'file' => 'Invoice_INVCZ769885.pdf',
    ),
    5 => 
    array (
      'img' => 'uploads/str/evidence/permohonan1.pdf',
      'file' => 'Invoice_INVCZ769885.pdf',
    ),
    6 => 
    array (
      'img' => 'uploads/str/evidence/Invoice_INVCZ7698852.pdf',
      'file' => 'Invoice_INVCZ769885.pdf',
    ),
    7 => 
    array (
      'img' => 'uploads/str/evidence/Invoice_INVCZ7698853.pdf',
      'file' => 'Invoice_INVCZ769885.pdf',
    ),
    8 => 
    array (
      'img' => 'uploads/str/evidence/keputusantahun12.pdf',
    ),
  ),
)