Php associative array add element

Add Element to an Associative Array in PHP

Add Element to an Associative Array in PHP

  1. Add Elements to the End of an Associative Array in PHP
  2. Use the array_merge() Function to Add Elements at the Beginning of an Associative Array in PHP
  3. Use the AddBetween Function to Add an Element in Between Associative Array in PHP

PHP has different ways to add items to an associative array.

If we want to add items to the start of the array, we can use built-in functions like array_merge() .

We need to do it dynamically to add elements before a particular key of an associative array.

Add Elements to the End of an Associative Array in PHP

We can add elements to the end of an associative array by adding keys with values or by concatenating a new key-value to the array.

php //First Method  $demo_array = array('Jack' => '10'); $demo_array['Michelle'] = '11'; // adding elements by pushing method  $demo_array['Shawn'] = '12'; echo "By Simple Method: 
"
;
print_r($demo_array); echo "
"
;
echo "Replacing the value:
"
;
$demo_array['Jack'] = '13'; // replaces the value at Jack print_r($demo_array); echo "
"
;
//Second method //$demo_array += [$key => $value]; $demo_array += ['John' => '14']; echo "By Concating Method:
"
;
print_r($demo_array); ?>

The code above tries to add elements to the end of an array by two methods.

By Simple Method: Array ( [Jack] => 10 [Michelle] => 11 [Shawn] => 12 ) Replacing the value: Array ( [Jack] => 13 [Michelle] => 11 [Shawn] => 12 ) By Concating Method: Array ( [Jack] => 13 [Michelle] => 11 [Shawn] => 12 [John] => 14 ) 

Use the array_merge() Function to Add Elements at the Beginning of an Associative Array in PHP

To add elements at the beginning of an associative, we can use the array union of the array_merge() function.

php $demo_array = array('Senior Developer' => 'Jack', 'Junior Developer' => 'Michelle', 'Intern' => 'John'); echo "The original array : "; print_r($demo_array); $temp_array = array('Project Manager' => 'Shawn'); //As per hierarchy Project manager should be at number 1.  // Using array union  $union_array = $temp_array + $demo_array; echo " 
The new associative array using union : "
;
print_r($union_array); // Using array_merge() function $merge_array = array_merge($temp_array, $demo_array); echo "
The new associative array using Array_merge : "
;
print_r($merge_array); ?>

The code above uses the union_array method and array_merge() to add elements at the beginning of an array.

The original array : Array ( [Senior Developer] => Jack [Junior Developer] => Michelle [Intern] => John ) The new associative array using union : Array ( [Project Manager] => Shawn [Senior Developer] => Jack [Junior Developer] => Michelle [Intern] => John ) The new associative array using Array_merge : Array ( [Project Manager] => Shawn [Senior Developer] => Jack [Junior Developer] => Michelle [Intern] => John ) 

Use the AddBetween Function to Add an Element in Between Associative Array in PHP

PHP has no built-in functionality to add elements between the given array. But we can create a function that adds an element before the given key.

php function AddBetween( $original_array, $before_key, $new_key, $new_value )    $added_array = array();  $added_key = false;   foreach( $original_array as $key => $value )   if( !$added_key && $key === $before_key )   $added_array[ $new_key ] = $new_value;  $added_key = true;  >  $added_array[ $key ] = $value;  >  return $added_array; >  $demo_array = array('Project Manager' => 'Shawn', 'Senior Developer' => 'Jack', 'Intern' => 'John'); echo "The Original Array is: 
"
;
print_r($demo_array); echo "
"
;
//Add 'Junior Developer' => 'Michelle' before intern as per hierarchy. $added_array = AddBetween( $demo_array, 'Intern', 'Junior Developer', 'Michelle' ); echo "The Array With Added Element is:
"
;
print_r($added_array); ?>

The function AddBetween tries to add an element before a given key.

The Original Array is: Array ( [Project Manager] => Shawn [Senior Developer] => Jack [Intern] => John ) The Array With Added Element is: Array ( [Project Manager] => Shawn [Senior Developer] => Jack [Junior Developer] => Michelle [Intern] => John ) 

The Junior Developer was added before the intern as per the hierarchy.

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

Related Article — PHP Array

Источник

Push Items to Associative Array in PHP

Push Items to Associative Array in PHP

  1. What Is an Associative Array in PHP
  2. Use the array_push() Method to Insert Items to an Associative Array in PHP
  3. Use the array_merge() Method to Insert Items to an Associative Array in PHP

In this tutorial, we will see how to add items or elements into an associative array.

First, we will cover what and how to create an associative array. Then we will add elements into our associative array.

What Is an Associative Array in PHP

An associative array is an array with strings instead of indices. We store elements of an associative array using key values rather than linear indices.

Here is an example of an associative array and how we can use it.

php // Create an array called age.  $age = array('Mike' => '24','Ann' => '19', 'Alice' => '32' ); echo "Mike is " . $age['Mike'] . ' years old.'; ?> 

Use the array_push() Method to Insert Items to an Associative Array in PHP

If we had an associative array shown below, how would we add new entries?

$color = array('a' => 'Red', 'b' => 'Blue' ) 

We will add two new colors to the above array in the example code below.

php $color = array('a' => 'Red', 'b' => 'Blue' ); // Add Green and White to the array.  array_push($color, 'Green', 'White');  print_r($color); ?> 
Array (  [a] => Red  [b] => Blue  [0] => Green  [1] => White ) 

Anytime you add an item to an array, it will assign numeric index keys.

Use the array_merge() Method to Insert Items to an Associative Array in PHP

At some point, you will have an associative array like the one shown below.

$age = array("Mike" => "24","Ann" => "19", "Alice" => "19" ) 

How do we add an entry like John, aged 22?

The method array_push() will not work in such a case. It would be best to use array_merge() instead, as shown below.

php $age = array("Mike" => "24","Ann" => "19", "Alice" => "19" ); $age1 = array("John" => "22"); //Merge the two arrays.  print_r(array_merge($age, $age1)); ?> 
Array (  [Mike] => 24  [Ann] => 19  [Alice] => 19  [John] => 22 ) 

In the code above, we decided to add our new entry in the form of a new array. The function array_merge() combines the two to form one array.

You can merge as many arrays as you want. If more elements share the same key, the last element will override the first one.

If you are confused, here is an example.

php $color = array('a' => 'Red', 'b' => 'Blue' ); $color1 = array('b' => 'Neon', 'c' => 'Green'); print_r(array_merge($color,$color1)); ?> 
Array (  [a] => Red  [b] => Neon  [c] => Green ) 

As seen in the output, Blue has been overwritten by Neon .

As shown below, we use the array_merge_recursive() to remedy this.

php $color = array('a' => 'Red', 'b' => 'Blue' ); $color1 = array('b' => 'Neon', 'c' => 'Green'); print_r(array_merge_recursive($color,$color1)); ?> 
Array (  [a] => Red  [b] => Array  (  [0] => Blue  [1] => Neon  )   [c] => Green ) 

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

Related Article — PHP Array

Источник

Push Items to Associative Array in PHP

Push Items to Associative Array in PHP

  1. What Is an Associative Array in PHP
  2. Use the array_push() Method to Insert Items to an Associative Array in PHP
  3. Use the array_merge() Method to Insert Items to an Associative Array in PHP

In this tutorial, we will see how to add items or elements into an associative array.

First, we will cover what and how to create an associative array. Then we will add elements into our associative array.

What Is an Associative Array in PHP

An associative array is an array with strings instead of indices. We store elements of an associative array using key values rather than linear indices.

Here is an example of an associative array and how we can use it.

php // Create an array called age.  $age = array('Mike' => '24','Ann' => '19', 'Alice' => '32' ); echo "Mike is " . $age['Mike'] . ' years old.'; ?> 

Use the array_push() Method to Insert Items to an Associative Array in PHP

If we had an associative array shown below, how would we add new entries?

$color = array('a' => 'Red', 'b' => 'Blue' ) 

We will add two new colors to the above array in the example code below.

php $color = array('a' => 'Red', 'b' => 'Blue' ); // Add Green and White to the array.  array_push($color, 'Green', 'White');  print_r($color); ?> 
Array (  [a] => Red  [b] => Blue  [0] => Green  [1] => White ) 

Anytime you add an item to an array, it will assign numeric index keys.

Use the array_merge() Method to Insert Items to an Associative Array in PHP

At some point, you will have an associative array like the one shown below.

$age = array("Mike" => "24","Ann" => "19", "Alice" => "19" ) 

How do we add an entry like John, aged 22?

The method array_push() will not work in such a case. It would be best to use array_merge() instead, as shown below.

php $age = array("Mike" => "24","Ann" => "19", "Alice" => "19" ); $age1 = array("John" => "22"); //Merge the two arrays.  print_r(array_merge($age, $age1)); ?> 
Array (  [Mike] => 24  [Ann] => 19  [Alice] => 19  [John] => 22 ) 

In the code above, we decided to add our new entry in the form of a new array. The function array_merge() combines the two to form one array.

You can merge as many arrays as you want. If more elements share the same key, the last element will override the first one.

If you are confused, here is an example.

php $color = array('a' => 'Red', 'b' => 'Blue' ); $color1 = array('b' => 'Neon', 'c' => 'Green'); print_r(array_merge($color,$color1)); ?> 
Array (  [a] => Red  [b] => Neon  [c] => Green ) 

As seen in the output, Blue has been overwritten by Neon .

As shown below, we use the array_merge_recursive() to remedy this.

php $color = array('a' => 'Red', 'b' => 'Blue' ); $color1 = array('b' => 'Neon', 'c' => 'Green'); print_r(array_merge_recursive($color,$color1)); ?> 
Array (  [a] => Red  [b] => Array  (  [0] => Blue  [1] => Neon  )   [c] => Green ) 

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

Related Article — PHP Array

Источник

Читайте также:  Демо
Оцените статью