skip to Main Content

In PHP, what is quickest way to turn the following array variables:

$id = [11,12,13];
$code = ['1234','5678','9012'];
$qty = [3,4,5];
$amount = [12.34,23.45,34.56];

Into an array of associative arrays, like the following:

[
  ['id'=>11,'code'=>'1234','qty'=>3,'amount'=>12.34],
  ['id'=>12,'code'=>'5678','qty'=>4,'amount'=>23.45],
  ['id'=>13,'code'=>'9012','qty'=>5,'amount'=>34.56],
]

Currently, I’m doing the following to convert the data.

$max = count($id);
$data = [];
for ($i=0; $i<$max; $i++) {
  $data[] = [
    'id' => $id[$i],
    'code' => $code[$i],
    'qty' => $qty[$i],
    'amount' => $amount[$i]
  ];
}

My application does this a lot, and looking if there are ways to decrease processing time.
Currently using PHP version 5.6

3

Answers


  1. My below code will optimize the loop by reducing the loop execution to half and will gives your expected result.

    $id     = [11,12,13,56,34,23,34];
    $code   = ['1234','5678','9012','4343','4543','4642','534'];
    $qty    = [3,4,5,6,7,8,3];
    $amount = [12.34,23.45,34.56,66.34,75.32,54.3,23.2];
    
    
    $max = count($id);
    $loopRun = 0;
    $data = [];
    $halfCount = ceil($max/2);
    for ($i=0; $i < $halfCount; $i++) {
      $loopRun++;
      $data[$i] = [
        'id'        => $id[$i],
        'code'      => $code[$i],
        'qty'       => $qty[$i],
        'amount'    => $amount[$i]
      ];
    
      $data[$max-($i+1)] = [
        'id'        => $id[$max-($i+1)],
        'code'      => $code[$max-($i+1)],
        'qty'       => $qty[$max-($i+1)],
        'amount'    => $amount[$max-($i+1)]
      ];
    }
    echo "Loop Run Count : ".$loopRun."<br>";
    ksort($data);
    

    Demo Link

    Login or Signup to reply.
  2. you can use foreach as well. I am not sure it will more efficient way but it can help you.

        $id = [11,12,13];
        $code = ['1234','5678','9012'];
        $qty = [3,4,5];
        $amount = [12.34,23.45,34.56];
        foreach ($id as $key=>$val)
        {
            $array[$key]["id"]= $val;
            $array[$key]["code"]= $code[$key];
            $array[$key]["qty"]= $qty[$key];
            $array[$key]["amount"]= $amount[$key];
        }
    
    Login or Signup to reply.
  3. foreach is typically the fastest method of the “general” approaches used to accomplish your desired end-results. This is due to the count() call prior to issuing for() accompanied with an incremental variable to determine the size and placement of the array to iterate over.

    Benchmarks: https://3v4l.org/ejIl5
    Benchmark 1-5000 https://3v4l.org/IOlAm

    $data = [];
    foreach($id as $i => $v) {
        $data[] = [
            'id' => $v,
            'code' => $code[$i],
            'qty' => $qty[$i],
            'amount' => $amount[$i]
        ];
    }
    //Execution time: ~0.00000200 seconds
    
    $max = count($id);
    $data = [];
    for ($i=0; $i<$max; $i++) {
      $data[] = [
        'id' => $id[$i],
        'code' => $code[$i],
        'qty' => $qty[$i],
        'amount' => $amount[$i]
      ];
    }
    //Execution time ~0.00000600 seconds
    

    array_map was the slowest

    $data = array_map(function($a, $b, $c, $d) {
        return [
            'id' => $a,
            'code' => $b,
            'qty' => $c,
            'amount' => $d
        ];
    }, $id, $code, $qty, $amount);
    //Execution time: ~0.00001000 seconds
    

    The benchmark used executes a dry-run for each of the approaches to
    reduce OP code optimization issues that would typically be implemented
    in a production environment.


    As an added bonus from the “general” approaches, I also ran a benchmark of an optimized version of the double-ended iteration approach (for ($i=0; $i<ceil($max/2); $i++)). https://3v4l.org/KHUul and 1-5000 https://3v4l.org/Mg95n which had wildly different values with the smaller array sizes, ranging from 0.00030208 seconds to 0.00000095 seconds, but on average was slower than the general for() loop.

    As with any benchmarks, the results may vary for your particular environment, settings and are only meant to serve as a generalization of what could be. Please be sure to benchmark your preferred methods in your specific environment to determine which is best.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search