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 8002851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:29:36+00:00 2026-06-04T16:29:36+00:00

I have an initial array: $arr0 = array( 0 => array( ‘a’ => 1,

  • 0

I have an initial array:

$arr0 = array(
    0 => array(
        'a' => 1,
        'b' => 1
    )
    1 => array(
        'a' => 2,
        'b' => 1
    )
    2 => array(
        'a' => 3,
        'b' => 2
    )
    3 => array(
        'a' => 4,
        'b' => 3
    )
    4 => array(
        'a' => 5,
        'b' => 3
    )
);

I wish to divide it into separate arrays depending on its members’ value of the field ‘b’, like so:

// $arr1 contains $arr0[0] and $arr0[1] because their value of 'b' is 1.
$arr1 = array(
    0 => array(
        'a' => 1,
        'b' => 1
    )
    1 => array(
        'a' => 2,
        'b' => 1
    )
);

// $arr2 contains $arr0[2] because its value of 'b' is 2.
$arr2 = array(
    0 => array(
        'a' => 3,
        'b' => 2
    )
);

// $arr3 contains $arr0[3] and $arr0[4] because their value of 'b' is 3.
$arr3 = array(
    0 => array(
        'a' => 4,
        'b' => 3
    )
    1 => array(
        'a' => 5,
        'b' => 3
    )
);

Now, the field ‘b’ can have any natural number for value, so it is not always three resulting arrays that I will need.

I have found a related question (and answers) here, but my problem is specific because I don’t know in advance what original values ‘b’ has, and how many of different values there are.

Any ideas?

(edit: $arr3 was written as $arr1)

  • 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-04T16:29:37+00:00Added an answer on June 4, 2026 at 4:29 pm
    foreach ($arr0 as $value) {
      ${"arr" . $value['b']}[] = $value;
    }
    // $arr1, $arr2 and $arr3 are defined
    

    There is magic in the second line, which dynamically builds a variable name. However, that way of programming is really unwieldy; you would do better to make the results into yet another array, like this:

    foreach ($arr0 as $value) {
      $allarr[$value['b']][] = $value;
    }
    // $allarr is defined
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A PHP array can have arrays for its elements. And those arrays can have
i want to have a foreach loop where the initial array is changed inside
I have a form with an initial value, but I can't get the initial
I have a property in WCF service class with a initial value, as shown
I have an array with arrays in it, where I want to sort the
Suppose you have an array value => timestamp. The values are increasing with the
I have a set of objects I want to put into an array and
i want to set my array key initial value to a certain number. here
I have a 2D array such as: // construct initial **array int **arr; arr
I have a UIActionSheet that pops up as soon as the initial view in

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.