在 PHP 中返回一个具有数字键的数组?
为此,请使用 foreach 循环以及 [] 并实现 array_values() 方法。
示例
<!DOCTYPE html> <html> <body> <?php $details = [ [ "id" => "10001", "firstName" => "John", "lastName" => "Doe" ], [ "id" => "10002", "firstName" => "David", "lastName" => "Miller" ] ]; foreach ($details as $k=> $v) { $someDetails[$v['id']][] = ['LASTNAME' => $v['lastName'], 'FIRSTNAME'=> $v['firstName']]; } print_r(array_values($someDetails)); ?> </body> </html>
输出
Array ( [0] => Array ( [0] => Array ( [LASTNAME] => Doe [FIRSTNAME] => John ) ) [1] => Array ( [0] => Array ( [LASTNAME] => Miller [FIRSTNAME] => David ) ) )
广告