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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog