0

I have an array, which is given below:

$test = Array
(
[0] => Array
    (
        [0] => stud 1
    )

[1] => Array
    (
        [0] => stud 2
    )

[2] => Array
    (
        [0] => stud 3
    )
);

I want to add a common element to above array with out using loop. For example, I want to add "test" to each element of array. After adding "test", array will look like:

$test = Array
(
[0] => Array
    (
        [0] => stud 1
        [1] => 'test'

    )

[1] => Array
    (
        [0] => stud 2
        [1] => 'test'
    )

[2] => Array
    (
        [0] => stud 3
        [1] => 'test'
    )
);

Is there any way to add common element array with out using any kind of loop(for, foreach etc...)?

LF00
  • 27,015
  • 29
  • 156
  • 295
  • 3
    Adding element using loop is a clear way. Nevertheless even with some functions which iterate over array values, loop will be just hidden under the hood. – u_mulder May 11 '17 at 13:13
  • use array_push. – Devsi Odedra May 11 '17 at 13:14
  • @u_mulder is right there is always an iterator somewhere at the very end of all functions you might look for. A way to loop throught the array with a single function (which may not be worth using here) is using `array_map`, but a simple foreach should be enough here. So `foreach($array as $key => $item) { $array[$key][] = 'test'; }` is a good way to go. – Tobias F. May 11 '17 at 13:17
  • My array contains 100 elements. But I want to add one common element to those 100 elements. If I use loop, application will become slow. Is there any built-in function like array_push or array_walk? @ u_mulder – user1197250 May 11 '17 at 13:21
  • All of these functions will loop through ervy single entry, there is no way around this behaviour. A direct `foreach` will be the fastest, and adding 100 times `'test'` this array won't make it significant slower. – Tobias F. May 11 '17 at 13:23
  • Why do you think your application will get slow by looping over 100 entries? If it were 10 million entries then you might notice a slight difference but with 100 you will never notice it. – Dave May 11 '17 at 13:23
  • @user1197250 You can't do this without using a loop somewhere somehow. – Rizier123 May 11 '17 at 13:25
  • @ Dave This is not the first loop. This is an inner loop. – user1197250 May 11 '17 at 13:28
  • 1
    Go ahead and try it yourself: You will see using the foreach directly is much faster than `array_map`: [eval.in](https://eval.in/792294). – Tobias F. May 11 '17 at 13:41
  • Somewhat related: [Push elements from one array into rows of another array (one element per row)](https://stackoverflow.com/q/1558291/2943403) – mickmackusa Sep 23 '22 at 09:36

1 Answers1

0

You can use array_map(), check the live demo

array_map(function($v){$v[] = 'test'; return $v;}, $array);
LF00
  • 27,015
  • 29
  • 156
  • 295
  • This is working .... I need to compare the speed of foreach and array_map. Thank you very much... @Kris Roofe and others. – user1197250 May 11 '17 at 13:38
  • 3
    Take look at the [PHP source code](https://github.com/php/php-src/blob/51b06aa86e6924420e55afebdc9bb229c1b96bbb/ext/standard/array.c#L6081). There you find a `for` loop. – Tobias F. May 11 '17 at 13:46