切片并合并2个数组

时间:2017-08-26 18:55:16

标签: php arrays

有一种很好的方法可以根据空值切片和合并2个数组,例如

第一个数组

      0 => string 'Perfect all gorgeous and arrived in less than 1 month for brazil' (length=64)
      1 => string '' (length=0)
      2 => string '' (length=0)
      3 => string 'Good figures for their money, only instead of bits normal stick child bit rastroilsya' (length=85)
      4 => string '' (length=0)
      5 => string '' (length=0)

第二个数组

 0 => string '' (length=0)
      1 => string 'http://g01.a.alicdn.com/kf/UTB8jjnecFfFXKJk43Otq6xIPFXaw.jpg" data-eid="eid-201782563197' (length=88)
      2 => string 'http://g01.a.alicdn.com/kf/UTB87.bdcNHEXKJk43Jeq6yeeXXaZ.jpg" data-eid="eid-201782563197' (length=88)
      3 => string '' (length=0)
      4 => string 'http://g01.a.alicdn.com/kf/UTB8cxXwg4HEXKJk43Jeq6yeeXXam.jpg" data-eid="eid-201833045441' (length=88)
      5 => string 'http://g04.a.alicdn.com/kf/UTB824Xwg4HEXKJk43Jeq6yeeXXaB.jpg" data-eid="eid-201833045441' (length=88)

我希望它们像这个数组

array (size=2)
  0 => 
    array (size=2)
      'comment' => string 'Perfect all gorgeous and arrived in less than 1 month for brazil' (length=64)
      'images' => 
        array (size=2)
          0 => string 'http://g01.a.alicdn.com/kf/UTB8jjnecFfFXKJk43Otq6xIPFXaw.jpg" data-eid="eid-201782563197' (length=88)
          1 => string 'http://g01.a.alicdn.com/kf/UTB87.bdcNHEXKJk43Jeq6yeeXXaZ.jpg" data-eid="eid-201782563197' (length=88)
  1 => 
    array (size=2)
      'comment' => string 'Good figures for their money, only instead of bits normal stick child bit rastroilsya' (length=85)
      'images' => 
        array (size=2)
          3 => string 'http://g01.a.alicdn.com/kf/UTB8cxXwg4HEXKJk43Jeq6yeeXXam.jpg" data-eid="eid-201833045441' (length=88)
          4 => string 'http://g04.a.alicdn.com/kf/UTB824Xwg4HEXKJk43Jeq6yeeXXaB.jpg" data-eid="eid-201833045441' (length=88)

怎么做?

1 个答案:

答案 0 :(得分:1)

得到了一些有用的东西。如果需要,它可以使用更多输入。如果您的第二个数组有多个空白,则可能效果不佳。只需处理更新的代码即可解决此类问题。

<?php

$arr1 = array("input", "", "", "another input", "", "", "yet another input", "");
$arr2 = array("", "p1", "p2", "", "p01", "p02", "","p11" );

$inp = array("comment" => $arr1, "images" => $arr2);

function mangle_arrays($input) {
    $out = array();
    $gen = 0;
    foreach($input as $key=>$val) {
        $id = $gen?-1:0;
        if ($gen) {
            foreach($val as $v) {
                if ($v) {
                    $out[$id][$key][] = $v;
                } else {
                    $id++;
                }
            }
        } else {
            foreach($val as $v) {
                if ($v) {
                    $out[$id] = array();
                    $out[$id][$key] = $v;
                    $id++;
                }
            }
        }
        $gen++; 
    }
    return $out;
}

// your code goes here
echo "<pre>";
print_r(mangle_arrays($inp));

结果

Array
(
    [0] => Array
        (
            [comment] => input
            [images] => Array
                (
                    [0] => p1
                    [1] => p2
                )

        )

    [1] => Array
        (
            [comment] => another input
            [images] => Array
                (
                    [0] => p01
                    [1] => p02
                )

        )

    [2] => Array
        (
            [comment] => yet another input
            [images] => Array
                (
                    [0] => p11
                )

        )

)