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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:05:57+00:00 2026-05-15T05:05:57+00:00

This is my array Array ( [0] => Array ( [sample_id] => 3 [time]

  • 0

This is my array

Array
(
    [0] => Array
        (
            [sample_id] => 3
            [time] => 2010-05-30 21:11:47
        )

    [1] => Array
        (
            [sample_id] => 2
            [time] => 2010-05-30 21:11:47
        )

    [2] => Array
        (
            [sample_id] => 1
            [time] => 2010-05-30 21:11:47
        )
)

And I want to get all the sample_ids in one array. can someone please help ?

Can this be done without for loops (because arrays are very large).

  • 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-15T05:05:57+00:00Added an answer on May 15, 2026 at 5:05 am

    This is a problem I’ve had MANY times. There isn’t an easy way to flatten arrays in PHP. You’ll have to loop them adding them to another array. Failing that rethink how you’re working with the data to use the original structure and not require the flatten.

    EDIT: I thought I’d add a bit of metric information, I created an array $data = array(array('key' => value, 'value' => other_value), ...); where there were 150,000 elements in my array. I than ran the 3 typical ways of flattening

    $start = microtime();
    $values = array_map(function($ele){return $ele['key'];}, $data);
    $end = microtime();
    

    Produced a run time of: Run Time: 0.304405 Running 5 times averaged the time to just below 0.30

    $start = microtime();
    $values = array();
    foreach ($data as $value) {
        $values[] = $value['key'];
    }
    $end = microtime();
    

    Produced a run time of Run Time: 0.167301 with an average of 0.165

    $start = microtime();
    $values = array();
    for ($i = 0; $i < count($data); $i++) {
        $values[] = $data[$i]['key'];
    }
    $end = microtime();
    

    Produced a run time of Run Time: 0.353524 with an average of 0.355

    In every case using a foreach on the data array was significantly faster. This is likely related to the overhead of the execution of a function for each element in the array for hte array_map() implementation.

    Further Edit: I ran this testing with a predefined function. Below are the average numbers over 10 iterations for ‘On the Fly’ (defined inline) and ‘Pre Defined’ (string lookup).

    Averages:
      On the fly: 0.29714539051056
      Pre Defined: 0.31916437149048
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can any one know the, convert mysql query in to an php array: this
Wondering whether any improvement can be done to speed up this array? test array
In this simple example, i want to create a String array populated with each
I have an array in JavaScript that can go as deep as I want:
Please Help!! I'm seeing a lot of mixed results when I search for this,
I have an array that looks like this (sample): Array ( [1600] => Array
When I try to use this in my Javascript prototype like so: Array.prototype.sample =
I'm reading this post . It defines a simple array: var myArray = [1,
This would be easy to do with regular array with a simple for statement.
Up to this moment I was using simple arrays to enter and get necessary

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.