对于一位数组,重置索引,可使用array_values();(具体用法,参考手册)
$array = array("size" => "XL", "color" => "gold");print_r(array_values($array));
以上例程会输出:
Array( [0] => XL [1] => gold)
$post = [ 0 => [ 'title' => 'xxx联票', 'price' => 368.00, 'items' => [ '33' => [ 'scenic_id' => 33, 'products' => [ 'goods_id' => 13 ] ], '1500' => [ 'scenic_id' => 1500, 'products' => [ 'goods_id' => 57 ] ], ], 'tickets' => 9 ], 10 => [ 'title' => 'zzz联票', 'price' => 500.00, 'items' => [ '157' => [ 'scenic_id' => 157, 'products' => [ 'goods_id' => 77 ], 'attr' => [ '50' => [ 'item' => '午场' ], '55' => [ 'item' => '可退' ] ] ], '19' => [ 'scenic_id' => 19, 'products' => [ 'goods_id' => 87 ] ] ], 'tickets' => 3 ],];//print_r($post);function reform_keys($array){ if(!is_array($array)){ return $array; } $keys = implode('', array_keys($array)); if(is_numeric($keys)){ $array = array_values($array); } $array = array_map('reform_keys', $array); return $array;};$list = reform_keys($post);print_r($list);
以上结果会输出:
Array
( [0] => Array ( [title] => xxx联票 [price] => 368 [items] => Array ( [0] => Array ( [scenic_id] => 33 [products] => Array ( [goods_id] => 13 ))
[1] => Array
( [scenic_id] => 1500 [products] => Array ( [goods_id] => 57 ))
)
[tickets] => 9
)[1] => Array
( [title] => zzz联票 [price] => 500 [items] => Array ( [0] => Array ( [scenic_id] => 157 [products] => Array ( [goods_id] => 77 )[attr] => Array
( [0] => Array ( [item] => 午场 )[1] => Array
( [item] => 可退 ))
)
[1] => Array
( [scenic_id] => 19 [products] => Array ( [goods_id] => 87 ))
)
[tickets] => 3
))
里面所有层级的索引已经被重置。0,1,2.。。。。。