Php array add key value pair to existing multidimensional array

Following is the output of my multidimensional array $csmap_data

Array ( [0] => Array ( [cs_map_id] => 84 [cs_subject_id] => 1 ) [1] => Array ( [cs_map_id] => 85 [cs_subject_id] => 5 ) [flag] => 1 )

Initially there was no [flag] => 1 key-value in the array, I added it to the array $csmap_data. But I want to add the [flag] => 1 in the above two array elements, not as a separate array element. In short I wanted following output :

Array ( [0] => Array ( [cs_map_id] => 84 [cs_subject_id] => 1 [flag] => 1 ) [1] => Array ( [cs_map_id] => 85 [cs_subject_id] => 5 [flag] => 1 ) )

The code I was trying to achieve this is as follows, but couldn't get the desired output:

if (!empty($csmap_data)) { foreach($csmap_data as $csm) { $chapter_csmap_details = $objClassSubjects->IsClassSubjectHasChapters($csm['cs_map_id']); $csmap_data ['flag'] = 1; } }

Can anyone help me out in obtaining the desired output as I depicted? Thanks in advance.

asked Apr 18, 2013 at 15:51

PHPLoverPHPLover

7,54337 gold badges100 silver badges187 bronze badges

<? foreach($csmap_data as $key => $csm) { $csmap_data[$key]['flag'] = 1; }

That should do the trick.

answered Apr 18, 2013 at 15:53

2

You can also do it using php array functions

$csmap_data = array_map(function($arr){ return $arr + ['flag' => 1]; }, $csmap_data);

UPDATE: to use multiple variables in callback function of array_map function we can do it by use

$flagValue = 1; $csmap_data = array_map(function($arr) use ($flagValue){ return $arr + ['flag' => $flagValue]; }, $csmap_data);

DazBaldwin

3,7613 gold badges37 silver badges41 bronze badges

answered Feb 23, 2017 at 13:51

ManmohanManmohan

6906 silver badges18 bronze badges

3

Not the answer you're looking for? Browse other questions tagged php arrays multidimensional-array associative-array or ask your own question.

Add values or elements to an array in PHP; In this tutorial, we will learn how to add/push the values/elements to array in PHP with examples.

Here we will learn:-

  • PHP array push with key
  • array push associative array PHP
  • PHP add to multidimensional associative array
  • PHP add to multidimensional array
  • How to push array in multidimensional array
  • PHP append one array to another

Here we will take some examples, like add values in array PHP, PHP array push with key, PHP add to an associative array, PHP add to the multidimensional array, array push associative array PHP, PHP array add key-value pair to an existing array.

Add or Insert elements/values to array In PHP

You can use PHP array_push() function for adding one or more elements/values to the end of an array.

Let’s know about the PHP array_push() function, like array_push function definition, syntax, and examples:

PHP array_push() function

The PHP array_push() function is used to add one or more elements to the end of an array.

Syntax

array_push(array, value1, value2, …)

Example 1 – add values in array PHP

In this example, we have one array “array(“PHP”, “laravel”, “codeigniter”)”, it contains value like (“PHP”, “laravel”, “codeigniter”). If we want to add/push one or more values in the array. You can add/push the values into array see below examples:

Here we will add new values like(“WordPress”,”bootstrap”,”HTML”) in existing array using PHP array_push() function:

<?php $array = array("php", "laravel", "codeigniter"); //before adding new values echo "Before add the value:- "; print_r($array); echo "<br>"; //add elements/values in array array_push($array,"wordpress","bootstrap","html"); //after adding a new values echo "After add the value:- "; print_r($array); ?>

PHP array push with key

Now we have one new array like this ” $array = array(“a”=>”red”,”b”=>”green”); “. If we want to add the values into array with key. You can use the below code:

// add the values in array without using array function $array['c'] = "yello"; $array['d'] = "brown";

Here we will push the values in array with key without using array function:

<?php $array = array("a"=>"red","b"=>"green"); //before adding new values echo "Before add the value:- "; print_r($array); echo "<br>"; // add the values in array without using array function $array['c'] = "yello"; $array['d'] = "brown"; //after adding a new values echo "After add the value:- "; print_r($array); ?>

PHP add to multidimensional array

If we want to add values/elements in a multi-dimensional array. Here we will take an example for adding the values/elements in a multidimensional array.

If you have a multidimensional array like this:

$array = [ 'web' => ['html', 'css', 'bootstrap'], 'p_lang' => ['php', 'python', 'cabbage'], 'framework' => ['laravel', 'codeigniter'] ];

And you want to add values/elements inside the array elements. You can use the below example for adding the values/elements in the multidimensional array:

<?php $array = [ 'web' => ['html', 'css', 'bootstrap'], 'p_lang' => ['php', 'python', 'cabbage'], 'framework' => ['laravel', 'codeigniter'] ]; //before adding new values echo "Before add the value:- "; print_r($array); echo "<br>"; //add elements/values in array array_push($array['framework'], 'wordpress', 'joomla'); print_r($array); //after adding a new values echo "After add the value:- "; print_r($array); ?>

How to push array in multidimensional array

Here we will take an example with the multi-dimensional array. In this example, we will push new array into multidimensional-array.

Let’s see the example below:

<?php $array = [ 'web' => ['html', 'css', 'bootstrap'], 'p_lang' => ['php', 'python', 'cabbage'], 'framework' => ['laravel', 'codeigniter'] ]; $array1['test'] = array('laravel', 'codeigniter'); //before adding new values echo "Before add the value:- "; print_r($array); echo "<br>"; //add elements/values in array array_push($array['framework'], $array1); print_r($array); //after adding a new values echo "After add the value:- "; print_r($array); ?>

PHP append one array to another | PHP push array into an array

Now, we will take example of push one array to another array or push array into an array without using array_push() function.

Add one array into another array in PHP:

<?php $a = array('a', 'b'); $b = array('c', 'd'); $merge = array_merge($a, $b); ?>

Conclusion

Array add/push values PHP tutorial. Here you have learned how to add values in array PHP, PHP array push with key, PHP add to an associative array, PHP add to the multidimensional array, array push associative array PHP, PHP array adds key-value pair to an existing array with examples.

Recommended PHP Tutorials

  1. PHP Array: Indexed,Associative, Multidimensional
  2. To Remove Elements or Values from Array PHP
  3. PHP remove duplicates from multidimensional array
  4. Remove Duplicate Elements or Values from Array PHP
  5. How to Convert String to Array in PHP
  6. Array Push and POP in PHP | PHP Tutorial
  7. PHP Search Multidimensional Array [key and value and return key]
  8. PHP Array to String Conversion – PHP Implode

How add a value to a 2d array in PHP?

You can use PHP array_push() function for adding one or more elements/values to the end of an array.

How do you push a key and value in an array?

Answer: Use the Square Bracket [] Syntax php // Sample array $array = array("a" => "Apple", "b" => "Ball", "c" => "Cat"); // Adding key-value pairs to an array $array["d"] = "Dog"; $array["e"] = "Elephant"; print_r($array); ?>

Which array has pair of value and key in PHP?

In PHP, there are three types of arrays: Indexed arrays - Arrays with numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.

How get key from value in array in PHP?

If you have a value and want to find the key, use array_search() like this: $arr = array ('first' => 'a', 'second' => 'b', ); $key = array_search ('a', $arr); $key will now contain the key for value 'a' (that is, 'first' ).

Chủ đề