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

  • Home
  • SEARCH
  • 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 8513271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:31:04+00:00 2026-06-11T04:31:04+00:00

This code: $arr1 = array(); for ($i=0; $i<10; $i++) { $arr1[] = array(‘A’=>rand(0,5), ‘B’=>rand(0,4));

  • 0

This code:

$arr1 = array();

for ($i=0; $i<10; $i++)
{
  $arr1[] = array('A'=>rand(0,5), 'B'=>rand(0,4));
}

generates array like this:

[0] array('A'=>0,[B]=>4)
[1] array('A'=>1,[B]=>3)
[2] array('A'=>3,[B]=>1)
//etc.

I need to get the different structure, i.e.:

$arr2 = array('A' => array(0,1,3),'B' => array(4,3,1));

so that later I can use $arr2['A']. If the array is created like it’s shown in the first example, then $arr1['A'] will not work.

How can I get $arr2 using FOR loop like for $arr1. Hope I explained myself clear enough.

  • 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-06-11T04:31:06+00:00Added an answer on June 11, 2026 at 4:31 am

    Try with:

    $arr2 = array(
      'A' => array(),
      'B' => array()
    );
    
    for ($i=0; $i<10; $i++)
    {
      $arr2['A'][] = rand(0,5);
      $arr2['B'][] = rand(0,4);
    }
    

    If you want to convert existing $arr1 into $arr2, try with:

    $arr2 = array(
      'A' => array(),
      'B' => array()
    );
    
    foreach ( $arr1 as $value )
    {
      $arr2['A'][] = $value['A'];
      $arr2['B'][] = $value['B'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this code to get a String array of headings used in a
This code produces a different output in Python 2 and Python 3 . class
Consider this code: using namespace std; int* get() { unique_ptr<int> p (new int[4]); return
Consider the following code: Dim arr1 As New List(Of Double) Dim arr2 As New
I am able to create a matrix using this code #!/usr/bin/perl -w @arr1 =
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
This code below allows me to find the word error in all my files
This code is the core of a much larger script that works great in
This code disabled mouse scroll functionality, when i click on the document. $(document).on(click, function
This code only renders a dodecahedron and completely ignores the glBegin(GL_TRIANGLES) block: glutSolidDodecahedron(); glBegin(GL_TRIANGLES);

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.