欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 二维数组的键如何改成数字?

程序员文章站 2022-04-21 14:34:19
...
dump($array);
array(2) {
  [0] => array(6) {
    ["record_id"] => string(1) "1"
    ["record_create_time"] => string(10) "1470650276"
    ["record_create_people"] => string(12) "测试人员"
    ["record_name"] => string(9) "vpn出售"
    ["record_comment"] => string(20) "出售10个包月VPN"
    ["record_money"] => string(6) "100000"
  }
  [1] => array(6) {
    ["record_id"] => string(1) "2"
    ["record_create_time"] => string(10) "1470650739"
    ["record_create_people"] => string(12) "测试人员"
    ["record_name"] => string(9) "服务器"
    ["record_comment"] => string(28) "双核CPU2G内存40SDD硬盘"
    ["record_money"] => string(5) "15000"
  }
}

我想改成这样

array(2) {
  [0] => array(6) {
    ["0"] => string(1) "1"
    ["1"] => string(10) "1470650276"
    ["2"] => string(12) "测试人员"
    ["3"] => string(9) "vpn出售"
    ["4"] => string(20) "出售10个包月VPN"
    ["5"] => string(6) "100000"
  }
  [1] => array(6) {
    ["0"] => string(1) "2"
    ["1"] => string(10) "1470650739"
    ["2"] => string(12) "测试人员"
    ["3"] => string(9) "服务器"
    ["4"] => string(28) "双核CPU2G内存40SDD硬盘"
    ["5"] => string(5) "15000"
  }
}

回复内容:

dump($array);
array(2) {
  [0] => array(6) {
    ["record_id"] => string(1) "1"
    ["record_create_time"] => string(10) "1470650276"
    ["record_create_people"] => string(12) "测试人员"
    ["record_name"] => string(9) "vpn出售"
    ["record_comment"] => string(20) "出售10个包月VPN"
    ["record_money"] => string(6) "100000"
  }
  [1] => array(6) {
    ["record_id"] => string(1) "2"
    ["record_create_time"] => string(10) "1470650739"
    ["record_create_people"] => string(12) "测试人员"
    ["record_name"] => string(9) "服务器"
    ["record_comment"] => string(28) "双核CPU2G内存40SDD硬盘"
    ["record_money"] => string(5) "15000"
  }
}

我想改成这样

array(2) {
  [0] => array(6) {
    ["0"] => string(1) "1"
    ["1"] => string(10) "1470650276"
    ["2"] => string(12) "测试人员"
    ["3"] => string(9) "vpn出售"
    ["4"] => string(20) "出售10个包月VPN"
    ["5"] => string(6) "100000"
  }
  [1] => array(6) {
    ["0"] => string(1) "2"
    ["1"] => string(10) "1470650739"
    ["2"] => string(12) "测试人员"
    ["3"] => string(9) "服务器"
    ["4"] => string(28) "双核CPU2G内存40SDD硬盘"
    ["5"] => string(5) "15000"
  }
}

键用"0"是被解析为数字的。
php 二维数组的键如何改成数字?

相关文档:http://php.net/manual/zh/lang...

你要转换的话:

foreach($array as &item) {
    $item = array_values($item);
}

事例代码如下,你试试看

foreach($array as $key=>$val){
    $array[$key] = array_values($val);
}

array_values

"Bill","Age"=>"60","Country"=>"USA");
print_r(array_values($a));
?>
相关标签: php