douzhi7082 2018-10-02 21:15
浏览 190
已采纳

如何从PHP数组中取消设置数字索引元素?

I have a collection of array it contains both numeric index as well as non numeric. I want to unset all the numeric indexes.

My array is something like this .

Array
(

    [554] => Array
        (
            [0] => 554
            [1] => Jiaqi Zheng
            [2] => Female
            [3] => 28
            [4] => Table Tennis
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [rank] => 554
            [athlet_name] => Jiaqi Zheng
            [gender] => Female
            [sport] => Table Tennis
        )

    [555] => Array
        (
            [0] => 555
            [1] => Zach Ziemek
            [2] => Male
            [3] => 23
            [4] => Athletics
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [rank] => 555
            [athlet_name] => Zach Ziemek
            [gender] => Male
            [sport] => Athletics
        )
)

Here i have to unset all the numeric index .

I used unset like this and its working fine for me .

unset(
                     $history_years_wise_country_wise_details_arr[     $history_years_wise_country_wise_details_info[0]][0],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][1],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][2],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][3],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][4],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][5],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][6],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][7],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][8]
                      );

Is there any way I will reduce the lines of codes? here 0 to 8 are in one series.

Can I unset all index in one line of code , as all are numeric?

Is it possible to use regular expression instead?

I want something like

unset(
                     $history_years_wise_country_wise_details_arr[     $history_years_wise_country_wise_details_info[0]][anything_which_will_take_index_from_0_to_8]);

Any suggestions?

Thank you

展开全部

  • 写回答

4条回答 默认 最新

  • dtbi27903 2018-10-02 21:51
    关注

    You could use array_filter() with is_string() function as its callback function:

    $array = array_filter($array, 'is_string', ARRAY_FILTER_USE_KEY);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部