douzhi7082 2018-10-03 05:15
浏览 189
已采纳

如何从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-03 05: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条)

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多