<?php $id = [11,12,13]; $code = ['1234','5678','9012']; $qty = [3,4,5]; $amount = [12.34,23.45,34.56]; //dry run to cache function calls $op = '%s executed in %.8f seconds'; microtime(true); $max = count($id); $start = $end = 0; $data = []; for ($i=0; $i<$max; $i++) { $data[] = [ 'id' => $id[$i], 'code' => $code[$i], 'qty' => $qty[$i], 'amount' => $amount[$i] ]; } $data = []; foreach($id as $i => $v) { $data[] = [ 'id' => $v, 'code' => $code[$i], 'qty' => $qty[$i], 'amount' => $amount[$i] ]; } $data = array_map(function($a, $b, $c, $d) { return [ 'id' => $a, 'code' => $b, 'qty' => $c, 'amount' => $d ]; }, $id, $code, $qty, $amount); //end dry-run $start = microtime(true); $data = []; foreach($id as $i => $v) { $data[] = [ 'id' => $v, 'code' => $code[$i], 'qty' => $qty[$i], 'amount' => $amount[$i] ]; } $end = microtime(true); printf($op, 'foreach', $end - $start); echo PHP_EOL; //---- $start = microtime(true); $max = count($id); $data = []; for ($i=0; $i<$max; $i++) { $data[] = [ 'id' => $id[$i], 'code' => $code[$i], 'qty' => $qty[$i], 'amount' => $amount[$i] ]; } $end = microtime(true); printf($op, 'for', $end - $start); echo PHP_EOL; $start = microtime(true); $data = array_map(function($a, $b, $c, $d) { return [ 'id' => $a, 'code' => $b, 'qty' => $c, 'amount' => $d ]; }, $id, $code, $qty, $amount); $end = microtime(true); printf($op, 'array_map', $end - $start);
You have javascript disabled. You will not be able to edit any code.