3

Refer to 2 array 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 have value, so I would like to remove this index. In the meantime$bar_arr[0][bar] would 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 to compare this two array and remove both item in a specific index if either of the array have empty value.

Thanks

3 Answers 3

1

If you're just checking the value of banner and you assume that the two arrays are ordered identically, this is fairly simple (You might 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]);
    }
}

More likely though, the order of the arrays can't be relied upon. In this case, just use an additional array of positions and track all the positions that need to be removed, and unset those:

$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 corresponding positions:

foreach ($bar_arr as $key => $bar) {
    if (in_array($bar['position'], $positions)) {
        unset($bar_arr[$key]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Basically I'd just loop through the array and store the valid values in 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++;
}

Comments

0

Assuming your count lines up like you've 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'd just make a new function and use array map still.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.