PHP. Compare two arrays and remove both elements at a specific index if either of the arrays is empty

Refer to the 2 arrays below:

$bar_arr = 

Array
(
    Array
    (
        [bar] => bar01.jpg
        [position] => 1
    )
    Array
    (
        [bar] => bar02.jpg
        [position] => 2
    )
    Array
    (
        [bar] => bar03.jpg
        [position] => 3
    )
)

$banner_arr = 

Array
(
    Array
    (
        [banner] => 
        [position] => 1
    )
    Array
    (
        [banner] => banner02.jpg
        [position] => 2
    )
    Array
    (
        [banner] => banner03.jpg
        [position] => 3
    )
)

      

$banner_arr[0][banner]

don't matter, so I would like to remove that index. In the meantime $bar_arr[0][bar]

will also be removed, I want to end up like this:

$bar_arr = 

Array
(
    Array
    (
        [bar] => bar02.jpg
        [position] => 2
    )
    Array
    (
        [bar] => bar03.jpg
        [position] => 3
    )
)

$banner_arr = 

Array
(
    Array
    (
        [banner] => banner02.jpg
        [position] => 2
    )
    Array
    (
        [banner] => banner03.jpg
        [position] => 3
    )
)

      

My question is how do I compare these two arrays and remove both elements at a specific index if either of the arrays is empty.

thank

+3


source to share


3 answers


If you are just checking the banner value, and you think the two arrays are ordered the same, this is pretty straightforward (you may need to make a copy of banner_arr first ... not sure):

foreach ($banner_arr as $key => $banner) {
    if (empty($banner['banner'])) {
        unset($banner_arr[$key]);
        unset($bar_arr[$key]);
    }
}

      

Most likely, the order of the arrays cannot be relied upon. In this case, just use an additional array of positions and track down all the positions that need to be removed and disable them:



$positions = array();
foreach ($banner_arr as $key => $banner) {
    if (empty($banner['banner'])) {
        $positions[] = $banner['position'];
        unset($banner_arr[$key]);
    }
}

      

then search through $ bar_arr for the corresponding positions:

foreach ($bar_arr as $key => $bar) {
    if (in_array($bar['position'], $positions)) {
        unset($bar_arr[$key]);
    }
}

      

+1


source


I am assuming that both arrays are the same length and that only possible missing values ​​are in ['bar'] or ['banner'].

Basically, I just iterate over the array and store the valid values ​​in the new arrays;



$new_bar_arr = array();
$new_banner_arr = array();

$count = count($banner_arr);
$index = 0;
while($index < $count){
    if(!empty($bar_arr[$index]['bar']) && !empty($banner_arr[$index]['banner'])){
        $new_bar_arr[] = $bar_arr[$index];
        $new_banner_arr[] = $banner_arr[$index];
    }
    $index++;
}

      

+1


source


Assuming your counts are the same as you suggested:

$newArray = array_map( NULL, $banner_arr, $bar_arr );

 foreach( $newArray as $key => $array ){   
   foreach( $array as $arr ){
     if( $arr === NULL ){
       unset( $newArray[$key] );
     }
   }
 }

      

Even if it doesn't, I just create a new function and still use the array map.

0


source







All Articles