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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:08:53+00:00 2026-05-27T08:08:53+00:00

I have an array that looks something like that array( [0] => array( ‘id’

  • 0

I have an array that looks something like that

array(
     [0] => array(
               'id' => 1,
               'title' => 'title 1',
     ),
     [1] => array(
               'id' => 10,
               'title' => 'title 10',
     ),
     [2] => array(
               'id' => 11,
               'title' => 'title 11',
     ),
     [...]
);

I want to add an element to all the sub array. It’s the same element i’m adding. so the new array will look like :

array(
     [0] => array(
               'id' => 1,
               'title' => 'title 1',
               'type'  => 'bag',
     ),
     [1] => array(
               'id' => 10,
               'title' => 'title 10',
               'type'  => 'bag',
     ),
     [2] => array(
               'id' => 11,
               'title' => 'title 11',
               'type'  => 'bag',
     ),
     [...]
);

Is there a way to do it without iterating on the the first array?
It’s gonna be a big array. I’m looking for the fastest way to do it.

  • 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-27T08:08:54+00:00Added an answer on May 27, 2026 at 8:08 am

    Whatever speed one might hope to gain by using array_walk, is lost with function overhead. Since you stated in your comments that the array is a result of a db query, you can simply include the bag value in your result set by adding SELECT 'bag' AS 'type' to your SQL statement.

    $start = 0; $end = 0;
    
    $orig = array(
        array('id' => 1,  'title' => 'title 1'),
        array('id' => 10, 'title' => 'title 10'),
        array('id' => 11, 'title' => 'title 11')
    );
    
    // A
    $start = microtime(true);
    for ($a=0; $a<1000; $a++) {
        $els1 = $orig;
        array_walk($els1, function(&$val, $key){$val['type'] = 'bag';});
    }
    $end = microtime(true);
    echo 'A: ', $end - $start,  "<br />\n";
    
    // B
    $start = microtime(true);
    for ($b=0; $b<1000; $b++) {
        $els2 = $orig;
        foreach ($els2 as &$el) {
            $el['type'] = 'bag';
        }
        unset($el);
    }
    $end = microtime(true);
    echo 'B: ', $end - $start,  "<br />\n";
    
    /* output:
    
    A: 0.0076138973236084
    B: 0.0047528743743896
    
    A: 0.0075309276580811
    B: 0.0045361518859863
    
    A: 0.0075531005859375
    B: 0.062379837036133
    
    A: 0.0075340270996094
    B: 0.0044951438903809
    
    A: 0.0074868202209473
    B: 0.0044751167297363
    
    A: 0.0076088905334473
    B: 0.0048189163208008
    
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that looks like $numbers = array('first', 'second', 'third'); I want
I have a multidimensional array that looks something like this: ourThing = array( 'id'
I have a ruby array that looks something like this: my_array = ['mushroom', 'beef',
I have an array that looks something like this array(7) { [0]=> hello,pat1 [1]=>
I have an array that looks something like this array( 'cars' => 10, 'trucks'
I have an array that looks something like this: Array ( [Erik] => Array
I have a JSON string that looks something like this: {addresses:{address:[{@array:true,@id:888888,@uri:xyz,household:{@id:44444,@uri:xyz},person:{@id:,@uri:},addressType:{@id:1,@uri:xyz,name:Primary},address1:xyz,address2:null,address3:null,city:xyz,postalCode:111111}]}} What would be
I have my shared user defaults plist that looks something like: menuItems (Array) Item
So I have a Cake query that looks something like this: $forthcomingReleases = $this->Release->find('all',
I have some code that looks something like this: <? $addr = array(city =>

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.