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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:21:47+00:00 2026-05-23T13:21:47+00:00

I’m having trouble with multidimensional arrays in PHP. I’m assuming this should be simple

  • 0

I’m having trouble with multidimensional arrays in PHP.

I’m assuming this should be simple but, I’m having difficulty wrapping my mind around the multi-dimensions.

The array that I have looks like this:

[
    ['projects_users' => ['project_id' => 1]],
    ['projects_users' => ['project_id' => 2]],
]

I would like to somehow alter this array to look like this, where all I see is an array of the project_id:

Array
(
  [0] => 1
  [1] => 2
)
  • 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-23T13:21:48+00:00Added an answer on May 23, 2026 at 1:21 pm
    $arr = ...
    $new_arr = array();
    foreach ( $arr as $el ) {
        $new_arr[] = $el['projects_users']['project_id'];
    }
    

    Or, with PHP version >= 5.3:

    $new_arr = array_map(function ($e) { return $e['projects_users']['project_id']; }, $arr);
    

    A third fun way, with reset:

    $arr = ...
    $new_arr = array();
    foreach ( $arr as $el ) {
        $new_arr[] = reset(reset($el));
    }
    

    ###Performance

    Out of curiosity / boredom I benchmarked the iterative / functional styles with and without reset. I was surprised to see that test 4 was the winner in every run — I thought that array_map had a bit more overhead than foreach, but (at least in this case) these tests show otherwise! Test code is here.http://pastie.org/2183604

    $ php -v
    PHP 5.3.4 (cli) (built: Dec 15 2010 12:15:07) 
    Copyright (c) 1997-2010 The PHP Group
    Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    $ php test.php
    Test 1, Iterative - 34.093856811523 microseconds
    Test 2, array_map - 37.908554077148 microseconds
    Test 3, Iterative with reset - 107.0499420166 microseconds
    Test 4, array_map with reset - 25.033950805664 microseconds
    $ php test.php
    Test 1, Iterative - 32.186508178711 microseconds
    Test 2, array_map - 39.100646972656 microseconds
    Test 3, Iterative with reset - 35.04753112793 microseconds
    Test 4, array_map with reset - 24.080276489258 microseconds
    $ php test.php
    Test 1, Iterative - 31.948089599609 microseconds
    Test 2, array_map - 36.954879760742 microseconds
    Test 3, Iterative with reset - 32.901763916016 microseconds
    Test 4, array_map with reset - 24.795532226562 microseconds
    $ php test.php
    Test 1, Iterative - 29.087066650391 microseconds
    Test 2, array_map - 34.093856811523 microseconds
    Test 3, Iterative with reset - 33.140182495117 microseconds
    Test 4, array_map with reset - 25.98762512207 microseconds
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build

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.