Php array push associative array


PHP Associative Arrays

Associative arrays are arrays that use named keys that you assign to them.

There are two ways to create an associative array: 

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

or:

$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";

The named keys can then be used in a script:

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>

Try it Yourself »


Loop Through an Associative Array

To loop through and print all the values of an associative array, you could use a foreach loop, like this:

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "
";
}
?>

Try it Yourself »



Complete PHP Array Reference

For a complete reference of all array functions, go to our complete PHP Array Reference.

The reference contains a brief description, and examples of use, for each function!


PHP Exercises



Topic: PHP / MySQLPrev|Next

Answer: Use the Square Bracket [] Syntax

You can simply use the square bracket [] notation to add or push a key and value pair into a PHP associative array. Let's take a look at an example to understand how it basically works:

 "Apple", "b" => "Ball", "c" => "Cat");

// Adding key-value pairs to an array
$array["d"] = "Dog";
$array["e"] = "Elephant";

print_r($array);
?>


Here are some more FAQ related to this topic:

  • How to check if a key exists in an array in PHP
  • How to check if a value exists in an array in PHP
  • How to get all the keys of an associative array in PHP
  • How to get all the values from an associative array in PHP

PHPServer Side ProgrammingProgramming




Php array push associative array

Kickstart HTML, CSS and PHP: Build a Responsive Website

Featured

59 Lectures 8.5 hours

Ogbemudia Terry Osayawe

More Detail

To create associative arrays in PHP, use [] brackets. You don't need to use array_push().

Example

 Live Demo




"101",
   'employeeFirstName'=>"John",
   'employeeLastName'=>"Doe",
   'employeeCountryName'=>"AUS"
];
$employeeDetails[] = [
   'emp_id' => $emp->employeeId,
   'emp_first_name' => $emp->employeeFirstName,
   'emp_last_name' => $emp->employeeLastName,
   'emp_country_name' => $emp->employeeCountryName
];
print_r(array_values($employeeDetails));
?>

Output

Array ( [0] => Array ( [emp_id] => 101 [emp_first_name] => John [emp_last_name] => Doe [emp_country_name] => AUS ) )

Php array push associative array

AmitDiwan

Updated on 12-Oct-2020 13:47:40

  • Related Questions & Answers
  • Creating an associative array in JavaScript with push()?
  • PHP Associative Array
  • PHP Pushing values into an associative array?
  • Convert an object to associative array in PHP
  • How to access an associative array by integer index in PHP?
  • How to build dynamic associative array from simple array in php?
  • Creating an associative array in JavaScript?
  • JavaScript in filter an associative array with another array
  • Remove duplicated elements of associative array in PHP
  • How to push an array in MongoDB?
  • How to get numeric index of associative array in PHP?
  • MongoDB query to push document into an array
  • Sorting an associative array in ascending order - JavaScript
  • Updating an array with $push in MongoDB
  • Cannot push into an array from MongoDB?

Previous Page Print Page Next Page  

Advertisements

PHPServer Side ProgrammingProgramming




Php array push associative array

Kickstart HTML, CSS and PHP: Build a Responsive Website

Featured

59 Lectures 8.5 hours

Ogbemudia Terry Osayawe

More Detail

To push values into an associative array, use the brackets [] []. At first create an associative array −

$details= array (
   'id' => '101',
   'name' => 'John Smith',
   'countryName' => 'US'
);

The PHP code is as follows to insert values −

Example

 Live Demo




 '101',
      'name' => 'John Smith',
      'countryName' => 'US'
   );
   $all_details['studentDetails'][] = $details;
   print_r($all_details);
?>

Output

Array (
 [studentDetails] => Array (
    [0] => Array (
       [id] => 101 [name] => John Smith [countryName] => US
      )
   )
)

Php array push associative array

AmitDiwan

Updated on 20-Nov-2020 05:38:42

  • Related Questions & Answers
  • PHP Associative Array
  • Pushing values into array with multi field set to TRUE?
  • PHP array_push() to create an associative array?
  • Convert an object to associative array in PHP
  • How to access an associative array by integer index in PHP?
  • Creating an associative array in JavaScript?
  • Remove duplicated elements of associative array in PHP
  • Associative Arrays in PHP
  • Merging duplicate values into multi-dimensional array in PHP
  • How to build dynamic associative array from simple array in php?
  • JavaScript in filter an associative array with another array
  • PHP program to split a given comma delimited string into an array of values
  • Creating an associative array in JavaScript with push()?
  • Sorting an associative array in ascending order - JavaScript
  • How to get numeric index of associative array in PHP?

Previous Page Print Page Next Page  

Advertisements

How do you push an associative array?

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

What is Array_push in PHP?

Definition and Usage. The array_push() function inserts one or more elements to the end of an array. Tip: You can add one value, or as many as you like. Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).

What is the difference between indexed array and associative array?

Indexed array: Indexed array is an array with a numeric key. It is basically an array wherein each of the keys is associated with its own specific value. ... PHP..

What is multidimensional array in PHP?

A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.