1

I have array like this :

Array
(
    [0] => Array
        (
            [total] => 0
            [month] => May
        )

    [1] => Array
        (
            [total] => 1000000
            [month] => June
        )
)

I have variable like this : $target = 3000000

I want add the variable to array

So, the result like this :

Array
(
    [0] => Array
        (
            [total] => 0
            [month] => May
            [target] => 3000000
        )

    [1] => Array
        (
            [total] => 1000000
            [month] => June
            [target] => 3000000
        )
)

Whether it can be done?

7 Answers 7

1

Try this way,

foreach($myArray as $key=>$value){
    $myArray[$key]['target'] = $target;
}
print_r($myArray); // To print array
Sign up to request clarification or add additional context in comments.

1 Comment

I need you help. Look here : stackoverflow.com/questions/38521602/…
0
<?php 
  foreach($your_array as $k=>$v):
     $your_array[$k]['target'] = $target;
  endforeach;
?>

Comments

0

use array_merge

$target = ['target' => 3000000];

foreach ($array as &$item) {
     $item = array_merge($item, $target);
}

Comments

0

If you always know the variable name, then you can do like below

$target = 3000000
foreach($array as $key => $val){
   $array[$key]['target'] = $target;//create new index "target" and assign the value
}

Note: $array is your current array

The out put

Array
(
[0] => Array
    (
        [total] => 0
        [month] => May
        [target] => 3000000
    )

[1] => Array
    (
        [total] => 1000000
        [month] => June
        [target] => 3000000
    )
)

Comments

0

If your goal is to add the target Key to all the Sub-Arrays in the main array just like the one you posted you may do that with a Loop like this:

    <?php
        $target    = 3000000;
        $mainArray = array(
           array(
             'total' => 0,
             'month' => 'May',
           ),
           array(
             'total' => 1000000,
             'month' => 'June',
           ),
        );

        foreach($mainArray as $key=>&$subArrays){
            $subArrays['target'] = $target;
        }

        var_dump($mainArray);  //<== SHOULD CONTAIN THE ENTRY: target.

        //DUMPS
        array(
           array(
             'total' => 0,
             'month' => 'May',
             'target' => 3000000,
           ),
           array(
             'total' => 1000000,
             'month' => 'June',
             'target' => 3000000,
           ),
        );

Confirm the result here.

Comments

0

This is much simpler way., No need of extra array initialization.

foreach ($array as $key => &$value) { $value['target'] = 5000; }

print_r($array);

Comments

0

Php's array_map lets you apply a function to every element of an array.

<?php

$input = array
    (
        array
            (
                'total' => 0,
                'month' => 'May'
            ),
        array
            (
                'total' => 1000000,
                'month' => 'June'
            )
    );

$output = array_map
    (
        function($item) {
            $item['target'] = '3000000';
            return $item;
        },
        $input
    );

var_export($output);

Output:

array (
  0 => 
  array (
    'total' => 0,
    'month' => 'May',
    'target' => '3000000',
  ),
  1 => 
  array (
    'total' => 1000000,
    'month' => 'June',
    'target' => '3000000',
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.