根据值从数组中删除元素

时间:2017-02-01 10:05:01

标签: php arrays

我有以下PHP数组(这是用于说明的简化版本)$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
        )
)

我需要删除show_nav未设置为yes的条目,并生成包含结果的新数组。所以事后看起来像这样 - $myarray

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

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

数组可以是任意长度,对于任意数量的条目,show_nav都设置为yes。我知道如何删除特定条目,但我不知道如何查看数组并根据值删除条目。

任何帮助都非常感谢!

4 个答案:

答案 0 :(得分:3)

有无数种方法可以实现这个简单的目标。其中一个使用PHP函数array_filter()

np.repeat(x,3,axis=1)

答案 1 :(得分: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);

答案 2 :(得分:-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
        )

)

答案 3 :(得分:-2)

你需要循环回来找到你的&#34; show nav&#34;如果为空,则获取test的值为:

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