循环遍历数组,并检查它是否有值

时间:2014-08-12 11:09:09

标签: php arrays wordpress-plugin

目前我正在检查 $ countrySelected 以查看是否与 $ countries 数组列表匹配。我需要这个,所以我可以回应选择的html选项。

使用 in_array()时非常昂贵,尤其是当国家/地区数组较大时。我希望有更有效的检查方式吗?

感谢帮助!


当前代码

foreach ( $arr as $k => $v ){ // See output of $arr below

    $countrySelected[$k] = $mfwp_options_data[$k]['brand'][$v->post_name]['country'];

    foreach ($countries as $key => $val) {
       if ( in_array($val, $countrySelected[$k]) ) {
           echo "<option value='" . $val . "' selected>" . $val . "</option>";
       } else {
           echo "<option value='" . $val . "'>" . $val . "</option>";
       }
    }
}

$ countries 数组示例:

Array
(
    [AF] => Afghanistan
    [AX] => Aland Islands
    [AL] => Albania
    [DZ] => Algeria
)

$ countrySelected 数组示例:

Array
(
    [0] => Array
        (
            [0] => Afghanistan
            [1] => Albania
            [2] => Antarctica
            [3] => Botswana
        )

    [1] => Array
        (
            [0] => Afghanistan
            [1] => Aland Islands
        )

    [2] => Array
        (
            [0] => Afghanistan
            [1] => Aland Islands
            [2] => Albania
        )
)
来自wordpress的

$ arr 输出

Array
[0] => stdClass Object
(
    [ID] => 12
    [post_name] => brand-example
    [post_status] => publish
    [post_title] => Brand Example
)

$ mfwp_options_data 是来自wordpress的选项数组,其输出:

Array
(
    [0] => Array
        (
            [brand] => Array
                (
                    [brand-example] => Array
                        (
                            [total] => 7
                            [country] => Array
                                (
                                    [0] => Afghanistan
                                    [1] => Aland Islands
                                 )
                        )
                )
        )
)  

0 个答案:

没有答案
相关问题