2

I have the following PHP array (this is a simplified version for illustration) $myarray:

Array
(
    [0] => Array
        (
        [firstName] => Zaphod
        [show_nav] => yes
        [lastName] => Beeblebrox
        )

    [1] => Array
        (
        [firstName] => Ford
        [show_nav] => 
        [lastName] => Prefect
        )

    [2] => Array
        (
        [firstName] => Arthur
        [show_nav] => yes
        [lastName] => Dent
        )

    [3] => Array
        (
        [firstName] => Tricia
        [show_nav] => 
        [lastName] => McMillan
        )
)

I need to remove the entries where show_nav is not set to yes, and produce a new array with the results. So afterwards it would look like this - $myarray:

Array
(
    [0] => Array
        (
        [firstName] => Zaphod
        [show_nav] => yes
        [lastName] => Beeblebrox
        )

    [1] => Array
        (
        [firstName] => Arthur
        [show_nav] => yes
        [lastName] => Dent
        )
)

The array can be any length, with show_nav being set to yes for any number of entries. I know how to remove specific entries, but I am not sure how to look through the array and remove entries based on the values.

Any help greatly appreciated!

Mike Harrison
  • 1,020
  • 2
  • 15
  • 42
  • Possible duplicate of [Remove empty elements from an array in Javascript](http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript) – Tom Feb 01 '17 at 10:06
  • Read more about [`array_filter()`](http://php.net/manual/en/function.array-filter.php) – axiac Feb 01 '17 at 10:07
  • Possible duplicate of [How to filter a two dimensional array by value](https://stackoverflow.com/questions/27447923/how-to-filter-a-two-dimensional-array-by-value) – mickmackusa Jul 24 '18 at 01:13

4 Answers4

3

There are countless ways to achieve this simple goal. One of them uses the PHP function array_filter()

$filtered = array_filter(
    $myarray,
    function (array $item) {
        return array_key_exists('show_nav', $item) && $item['show_nav'] === 'yes';
    }
);
axiac
  • 68,258
  • 9
  • 99
  • 134
0
$p = array(
    array('firstName' => 'Zaphod', 'show_nav' => 'yes', 'lastName' => 'Beeblebrox'),
    array('firstName' => 'Ford', 'show_nav' => "", 'lastName' => 'Prefect'),
    array('firstName' => 'Arthur', 'show_nav' => "no", 'lastName' => 'Dent'),
    array('firstName' => 'Tricia', 'show_nav' => "", 'lastName' => 'McMillan')
);
foreach ($p as $m) {
    if (!($m['show_nav'] == "")) {
        $m1[] = $m;
    }
}
print_r($m1);
Pavan Patil
  • 51
  • 1
  • 4
-2
<?php

function partition( $list, $p ) {
    $listlen = count( $list );
    $partlen = floor( $listlen / $p );
    $partrem = $listlen % $p;
    $partition = array();
    $mark = 0;
    for ($px = 0; $px < $p; $px++) {
        $incr = ($px < $partrem) ? $partlen + 1 : $partlen;
        $partition[$px] = array_slice( $list, $mark, $incr );
        $mark += $incr;
    }
    return $partition;
}

$citylist = array( "Black Canyon City", "Chandler", "Flagstaff", "Gilbert", "Glendale", "Globe", "Mesa", "Miami",
                   "Phoenix", "Peoria", "Prescott", "Scottsdale", "Sun City", "Surprise", "Tempe", "Tucson", "Wickenburg" );
print_r( partition( $citylist, 3 ) );

?>

Array
(
    [0] => Array
        (
            [0] => Black Canyon City
            [1] => Chandler
            [2] => Flagstaff
            [3] => Gilbert
            [4] => Glendale
            [5] => Globe
        )

    [1] => Array
        (
            [0] => Mesa
            [1] => Miami
            [2] => Phoenix
            [3] => Peoria
            [4] => Prescott
            [5] => Scottsdale
        )

    [2] => Array
        (
            [0] => Sun City
            [1] => Surprise
            [2] => Tempe
            [3] => Tucson
            [4] => Wickenburg
        )

)
methababu
  • 13
  • 4
-2

you need to loop in a loop to find your "show nav" and get the value for test if empty deleting as :

cpt = 0;
foreach $my_arary as $users {
    foreach $users as line {
        if (line['show nav'] === '')
             unset($my_array[cpt];
        cpt++;
    }
}