What does key mean in php?



Syntax

each ( $array );

Definition and Usage

The each() function returns the current key and value pair from the array array and advances the array cursor. This pair is returned in a four-element array, with the keys 0, 1, key, and value. Elements 0 and key contain the key name of the array element, and 1 and value contain the data.

Parameters

Sr.NoParameter & Description
1

array(Required)

It specifies an array

Return Value

It returns the current key and value pair from an array.

Example

Try out following example −

<?php
   $transport = array('foot', 'bike', 'car', 'plane');
   $key_value = each($transport);
   
   print_r($key_value);
   print "<br />";
   
   $key_value = each($transport);
   print_r($key_value);
   print "<br />";
   
   $key_value = each($transport);
   print_r($key_value);
?> 

This will produce the following result −

Array ( [1] => foot [value] => foot [0] => 0 [key] => 0 )
Array ( [1] => bike [value] => bike [0] => 1 [key] => 1 )
Array ( [1] => car [value] => car [0] => 2 [key] => 2 )

php_function_reference.htm

An array refers to a data structure storing one or more related types of values in a single value. For instance, if you are looking to store 100 numbers, instead of specifying 100 variables, you can simply define an array of length 100.N

There are three types of arrays, and you can assess each array value through an ID c, also known as the array index.

  • Numeric Array - It refers to an array with a numeric index. Values are stored and accessed in a linear fashion
  • Associative Array - It refers to an array with strings as an index. Rather than storing element values in a strict linear index order, this stores them in combination with key values.
  • Multiple indices are used to access values in a multidimensional array, which contains one or more arrays.

Overview

Associative arrays in PHP store key value pairs. For instance, If you need to store marks earned by a student in different subjects in an array, a numerically indexed array may not be the right choice. A better and more effective option would be to use the names of the subjects as the keys in your associative list, with their respective marks as the value.

In terms of features, associative arrays are very similar to numeric arrays, but they vary in terms of the index. The index of an associative array is a string that allows you to create a strong link between key and value.

A numerically indexed array is not the best option for storing employee salaries in an array. Instead, you can use the employees' names as keys in an associative list, with their salaries as the value.

  • “$variable name...” is the variable's name, “['key name']” is the element's access index number, and “value” is the array element's value.
  • Let's say you have a group of people and you want to assign gender to each of them based on their names.
  • To do so, you can use an associative list.
  • The code below will assist you in doing so.

Example:

<?php   

/* First method to create an associative array. */

$student_one = array("Maths"=>95, "Physics"=>90,  

                  "Chemistry"=>96, "English"=>93,  

                  "Computer"=>98); 

/* Second method to create an associative array. */

$student_two["Maths"] = 95; 

$student_two["Physics"] = 90; 

$student_two["Chemistry"] = 96; 

$student_two["English"] = 93; 

$student_two["Computer"] = 98;   

/* Accessing the elements directly */

echo "Marks for student one is:\n"; 

echo "Maths:" . $student_two["Maths"], "\n"; 

echo "Physics:" . $student_two["Physics"], "\n"; 

echo "Chemistry:" . $student_two["Chemistry"], "\n"; 

echo "English:" . $student_one["English"], "\n"; 

echo "Computer:" . $student_one["Computer"], "\n"; 

Output: 

Associative_Array_PHP_1

Traversing the Associative Array

Loops are used to traverse Associative arrays in PHP. There are two ways to loop around the associative array. First, by using the for loop, and then by using the ‘foreach’ command.

Example: In Associative arrays in PHP, the array keys() function is used to find indices with names provided to them, and the count() function is used to count the number of indices.

Example

<?php  

/* Creating an associative array */

$student_one = array("Maths"=>95, "Physics"=>90,  

                  "Chemistry"=>96, "English"=>93,  

                  "Computer"=>98);   

/* Looping through an array using foreach */

echo "Looping using foreach: \n"; 

foreach ($student_one as $subject => $marks){ 

    echo "Student one got ".$marks." in ".$subject."\n"; 

/* Looping through an array using for */

echo "\nLooping using for: \n"; 

$subject = array_keys($student_one); 

$marks = count($student_one);      

for($i=0; $i < $marks; ++$i) { 

    echo $subject[$i] . ' ' . $student_one[$subject[$i]] . "\n"; 

?> 

Output

Associative_Array_PHP_2.

Example 2: 

<?php   

/* Creating an associative array of mixed types */

$arr["xyz"] = 95; 

$arr[100] = "abc"; 

$arr[11.25] = 100; 

$arr["abc"] = "pqr";   

/* Looping through an array using foreach */

foreach ($arr as $key => $val){ 

    echo $key."==>".$val."\n"; 

}  

?> 

Output

Associative_Array_PHP_3

Looking forward to becoming a PHP Developer? Then get certified with the PHP Training Course. Enroll now!

Conclusion

An associative array in PHP represents an ordered map. A map is a data form that associates keys with values. This form is well-suited to a variety of tasks; it can be used as an array, list (vector), a hash table (a map implementation), dictionary, set, stack, queue, and possibly more. 

Trees and multidimensional associative arrays in PHP are also possible since array values may be other arrays. Although it is beyond the reach, this Simplilearn course will help you to explain each of these data structures, at least one example is given for each of them.

Do you have any questions regarding this article? Leave your questions or/and inputs for us on the associative array in PHP in the comments section of this page, and our experts will get back to you at the earliest.

Happy learning!

What is a key in PHP?

The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, key() returns null .

How do I get keys in PHP?

PHP: array_keys() function The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

What do we mean by keys and values in PHP?

In the PHP code: $featured is the associative array being looped through, and as $key => $value means that each time the loop runs and selects a key-value pair from the array, it stores the key in the local $key variable to use inside the loop block and the value in the local $value variable.

What is key and value in array?

What are a key and value in an array? Keys are indexes and values are elements of an associative array. Associative arrays are basically objects in JavaScript where indexes are replaced by user-defined keys. They do not have a length property like a normal array and cannot be traversed using a normal for loop.