Ahmadz Issa
Ahmadz Issa

Reputation: 765

slice and merge 2 arrays

is there a good way to slice and merge 2 arrays based on empty values for example

first array

      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)

second array

 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)

I want them to be like this array

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)

How to do it ?

Upvotes: 1

Views: 148

Answers (1)

Marcin Gałczyński
Marcin Gałczyński

Reputation: 590

Got something that will help. It will work with more inputs if you need. It might not work best if your second array has more than one breaking blank. Just working on updated code to solve such issues.

<?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));

Results

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
                )

        )

)

Upvotes: 1

Related Questions