Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 709353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:29:42+00:00 2026-05-14T04:29:42+00:00

As I was writing a for loop earlier today, I thought that there must

  • 0

As I was writing a for loop earlier today, I thought that there must be a neater way of doing this… so I figured I’d ask. I looked briefly for a duplicate question but didn’t see anything obvious.

The Problem:

Given N arrays of length M, turn them into a M-row by N-column 2D array

Example:

$id   = [1,5,2,8,6]
$name = [a,b,c,d,e]
$result = [[1,a],
           [5,b],
           [2,c],
           [8,d],
           [6,e]]

My Solution:

Pretty straight forward and probably not optimal, but it does work:

<?php
// $row is returned from a DB query
// $row['<var>'] is a comma separated string of values
$categories = array();
$ids = explode(",", $row['ids']);
$names = explode(",", $row['names']);
$titles = explode(",", $row['titles']);
for($i = 0; $i < count($ids); $i++) {
    $categories[] = array("id" => $ids[$i],
                          "name" => $names[$i],
                          "title" => $titles[$i]);
}
?>

note: I didn’t put the name => value bit in the spec, but it’d be awesome if there was some way to keep that as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:29:42+00:00Added an answer on May 14, 2026 at 4:29 am

    Maybe this? Not sure if it’s more efficient but it’s definitely cleaner.

    /*
    Using the below data:
    
    $row['ids'] = '1,2,3';
    $row['names'] = 'a,b,c';
    $row['titles'] = 'title1,title2,title3';
    */
    
    $categories = array_map(NULL,
      explode(',', $row['ids']),
      explode(',', $row['names']),
      explode(',', $row['titles'])
    );
    
    // If you must retain keys then use instead:
    $withKeys = array();
    
    foreach ($row as $k => $v) {
        $v = explode(',', $v);
    
        foreach ($v as $k2 => $v2) {
            $withKeys[$k2][$k] = $v[$k2];
        }
    }
    
    print_r($categories);
    print_r($withKeys);
    
    /*
    $categories:
    
    array
      0 => 
        array
          0 => int 1
          1 => string 'a' (length=1)
          2 => string 'title1' (length=6)
    ...
    
    $withKeys:
    
    array
      0 =>
        array
          'ids' => int 1
          'names' => string 'a' (length=1)
          'titles' => string 'title1' (length=6)
    ...
    */
    

    Just did a quick simple benchmark for the 4 results on this page and got the following:

    // Using the following data set:
    $row = array(
        'ids'       =>  '1,2,3,4,5',
        'names'     =>  'abc,def,ghi,jkl,mno',
        'titles'    =>  'pqrs,tuvw,xyzA,BCDE,FGHI'
    );
    
    /*
    For 10,000 iterations,
    
    Merge, for:
    0.52803611755371
    
    Merge, func:
    0.94854116439819
    
    Merge, array_map:
    0.30260396003723
    
    Merge, foreach:
    0.40261697769165
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think there is a shorter way of writing this foreach loop that creates
In Java is there any way of writing a loop so that you can
I'm writing a script and I need to create a loop that will execute
I'm writing some code so that at each iteration of a for loop it
I've run into this while writing a Traveling Salesman program. For an inner loop,
I was enjoying The Humble Programmer earlier today and ran across this choice quote:
I am writing a loop that takes two files per run e.g.a0.txt and b0.txt.
I am writing a Tkinter program that requires a loop. I can't run the
I am writing my code like this: function updates_cb() { in a while loop
I am writing this loop where in the initializing i am intializing using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.