Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组_filter()未按预期工作_Php_Arrays_Array Filter - Fatal编程技术网

Php 数组_filter()未按预期工作

Php 数组_filter()未按预期工作,php,arrays,array-filter,Php,Arrays,Array Filter,也许我在这里遗漏了一些基本知识。我有一个数组,并使用array_filter()函数过滤值。我在filter函数中使用echo来查看过滤后的值是否工作 <?php $columns = array( 0 => 'ISO', 1 => 'Country', 2 => 'Country Code', 3 => 'Type of number', 4 => 'Voice Ena

也许我在这里遗漏了一些基本知识。我有一个数组,并使用array_filter()函数过滤值。我在filter函数中使用echo来查看过滤后的值是否工作

<?php

    $columns = array(
        0 => 'ISO',
        1 => 'Country',
        2 => 'Country Code',
        3 => 'Type of number',
        4 => 'Voice Enabled',
        5 => 'SMS Enabled',
        6 => 'MMS Enabled',
        7 => 'Domestic Voice Only',
        8 => 'Domestic SMS only',
        9 => 'Price /num/month',
        10 => 'Inbound Voice price/min',
        11 =>  'Inbound SMS price/msg ',
        12 =>  'Inbound MMS price/msg ',
        13 => 'Beta Status',
        14 => 'Address Required',
    );

        echo '<pre>';
        $columns = array_filter($columns, '_filter_column_names');
        echo '</pre>';

        function _filter_column_names($column_name){
            $column_name = str_replace(' /', '_', $column_name);
            $column_name = strtolower(str_replace(array(' ', '/'), '_', trim($column_name)));

            echo $column_name.'<br>';
            return $column_name;
        }

        echo '<pre>';
        print_r($columns);
        echo '</pre>';

生成的过滤数组根本未过滤。尽管filter函数中的数组值似乎正在正确过滤。您还可以在此处看到它的实时性

您没有正确使用回调,如下所示:

迭代数组中的每个值,并将其传递给回调 作用如果回调函数返回true,则当前值 从数组返回到结果数组中


对于输出数组中不需要的元素,回调需要返回FALSE。

您没有正确使用回调,如下所示:

迭代数组中的每个值,并将其传递给回调 作用如果回调函数返回true,则当前值 从数组返回到结果数组中


对于输出数组中不需要的元素,您的回调需要返回FALSE。

我认为您误解了它的作用。正如文档所说,它“使用回调函数过滤数组中的元素”,这意味着回调应该返回true/false,这取决于是否应该包含它


您可能想使用的是对每个项目运行回调并返回修改后的项目。

我想您误解了它的作用。正如文档所说,它“使用回调函数过滤数组中的元素”,这意味着回调应该返回true/false,这取决于是否应该包含它


您可能想使用的是,它对每个项目运行回调并返回修改后的项目。

哦,天哪!我真傻!!天哪!我真傻!!
iso
country
country_code
type_of_number
voice_enabled
sms_enabled
mms_enabled
domestic_voice_only
domestic_sms_only
price_num_month
inbound_voice_price_min
inbound_sms_price_msg
inbound_mms_price_msg
beta_status
address_required

Array
(
    [0] => ISO
    [1] => Country
    [2] => Country Code
    [3] => Type of number
    [4] => Voice Enabled
    [5] => SMS Enabled
    [6] => MMS Enabled
    [7] => Domestic Voice Only
    [8] => Domestic SMS only
    [9] => Price /num/month
    [10] => Inbound Voice price/min
    [11] => Inbound SMS price/msg 
    [12] => Inbound MMS price/msg 
    [13] => Beta Status
    [14] => Address Required
)