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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵