php associative array – Best 3 Ways To Create an Associative Array in PHP?

Today, We want to share with you php associative array.In this post we will show you php multidimensional associative array, hear for PHP array key value we will give you demo and example for implement.In this post, we will learn about Sort associative array by specific key in PHP with an example.

php associative array

Example 1 : PHP Associative Arrays

<!DOCTYPE html>
<html>
<body>

<?php
$members = array("Virat"=>"35", "Ben"=>"37", "Jagruti"=>"43");
echo "Virat is " . $members['Virat'] . " years old.";
?>

</body>
</html>

Example 2 : Loop Through an Associative Array

<!DOCTYPE html>
<html>
<body>

<?php
$members = array("Virat"=>"35", "Ben"=>"37", "Jagruti"=>"43");

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

</body>
</html>

Example 3

<html>
   <body>
      
      <?php
         /* First method to associate make an array. */
         $earnings = array("pihushree" => 2000, "bhagyashri" => 1000, "dharmishta" => 500);
         
         echo "Earning of pihushree is ". $earnings['pihushree'] . "<br />";
         echo "Earning of bhagyashri is ".  $earnings['bhagyashri']. "<br />";
         echo "Earning of dharmishta is ".  $earnings['dharmishta']. "<br />";
         
         /* Second method to make an array. */
         $earnings['pihushree'] = "high";
         $earnings['bhagyashri'] = "medium";
         $earnings['dharmishta'] = "low";
         
         echo "Earning of pihushree is ". $earnings['pihushree'] . "<br />";
         echo "Earning of bhagyashri is ".  $earnings['bhagyashri']. "<br />";
         echo "Earning of dharmishta is ".  $earnings['dharmishta']. "<br />";
      ?>
   
   </body>
</html>

New Example

<!DOCTYPE html>
<html lang="en">
<head>
    <title>PHP Associative Array</title>
</head>
<body>

<?php
$members["Virat"] = "22";
$members["Kinara"] = "32";
$members["Dhara"] = "28";

// Printing array structure
var_dump($members); 
?>

</body>
</html>

I hope you get an idea about php associative array.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.