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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:45:06+00:00 2026-06-15T06:45:06+00:00

How can I split a single array into it’s sub-keys? $arr = array( 0

  • 0

How can I split a single array into it’s sub-keys?

$arr = array(
             0 => array(
                        'foo' => '1',
                        'bar' => 'A'
                       ),
             1 => array(
                        'foo' => '2',
                        'bar' => 'B'
                       ),
             2 => array(
                        'foo' => '3',
                        'bar' => 'C'
                       )
            );

What is the most efficient way to return an array of foo and bar separately?

I need to get here:

$foo = array('1','2','3');
$bar = array('A','B','C');

I’m hoping there’s a clever way to do this using array_map or something similar. Any ideas?

Or do I have to loop through and build each array that way? Something like:

foreach ($arr as $v) {
    $foo[] = $v['foo'];
    $bar[] = $v['bar'];
}
  • 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-15T06:45:07+00:00Added an answer on June 15, 2026 at 6:45 am

    In a lucky coincidence, I needed to do almost the exact same thing earlier today. You can use array_map() in combination with array_shift():

    $foo = array_map('array_shift', &$arr);
    $bar = array_map('array_shift', &$arr);
    

    Note that $arr is passed by reference! If you don’t do that, then each time it would return the contents of $arr[<index>]['foo']. However, again because of the reference – you won’t be able to reuse $arr, so if you need to do that – copy it first.

    The downside is that your array keys need to be ordered in the same way as in your example, because array_shift() doesn’t actually know what the key is. It will NOT work on the following array:

    $arr = array(
        0 => array(
            'foo' => '1',
            'bar' => 'A'
        ),
        1 => array(
            'bar' => 'B',
            'foo' => '2'
        ),
        2 => array(
            'foo' => '3',
            'bar' => 'C'
        )
    );
    

    Update:

    After reading the comments, it became evident that my solution triggers E_DEPRECATED warnings for call-time-pass-by-reference. Here’s the suggested (and accepted as an answer) alternative by @Baba, which takes advantage of the two needed keys being the first and last elements of the second-dimension arrays:

    $foo = array_map('array_shift', $arr);
    $bar = array_map('array_pop', $arr);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can split(string, array, separator) in awk use sequence of whitespaces as the separator (or
How can I split a string such as Mar10 into Mar and 10 in
If I want a part of an array I can use [] or split
I need to know how you break an array into sub-arrays based on the
I'm in need of splitting of the single array into multiple array for some
Do MySQL have any function by which I can split data of a column
When I write: MyClass *obj = [[MyClass alloc]init]; I can split in 2 parts:
How can I split a String by Unicode range in Ruby. I wanted to
how can I split this barcode by group separator with Progress? I've tried chr(29)
I'm trying to build a screen like this: How can I split my cell

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.