How to get specific key value from array in PHP ? Last Updated : 04 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will see how to get specific key values from the given array. PHP array is a collection of items that are stored under keys. There are two possible types of keys: strings and integers. For any type of key, there is a common syntax to get a specific value by key — square brackets.Example: PHP <?php $mixedArr = [ 10, 20, 'hello' => 'world', 30, ]; // Get a specific value by index $firstItem = $mixedArr[0]; echo "An item by index 0: {$firstItem}\n"; // Get a specific value by string key $stringItem = $mixedArr['hello']; echo "An item by key 'hello': {$stringItem}\n"; ?> OutputAn item by index 0: 10 An item by key 'hello': worldExample 2: Sometimes we can accidentally try to get a non-existent item from the array. In this case, PHP throws a NOTICE. To avoid the issue, we have to check the key existence before accessing it. PHP <?php $arr = [1, 2, 3]; // Check the key existence with // the built-in 'isset' function if (isset($arr[10])) { echo "index 10 value is: {$arr[10]}\n"; } else { echo "There are no value under the index 10\n"; } $value = $arr[10] ?? 'unknown'; echo "A value under the index 10: {$value}\n"; ?> OutputThere are no value under the index 10 A value under the index 10: unknownExample 3: Using array_key_exists FunctionThe array_key_exists function is another way to check if a specific key exists in an array. This function checks if the given key or index exists in the array. Unlike isset, array_key_exists will return true even if the value associated with the key is null. PHP <?php $arr = ['a' => null, 'b' => 2]; // Nikunj Sonigara // Check the key existence with // the built-in 'array_key_exists' function if (array_key_exists('a', $arr)) { echo "The key 'a' exists in the array.\n"; } else { echo "The key 'a' does not exist in the array.\n"; } if (array_key_exists('c', $arr)) { echo "The key 'c' exists in the array.\n"; } else { echo "The key 'c' does not exist in the array.\n"; } ?> OutputThe key 'a' exists in the array. The key 'c' does not exist in the array. Using isset()In PHP, isset() checks if a variable is set and not null. To get a specific key's value from an array, use isset() to ensure the key exists before accessing its value, avoiding errors if the key is missing.Example: PHP <?php $array = ["name" => "John", "age" => 25, "country" => "USA"]; $key = "age"; if (isset($array[$key])) { echo $array[$key]; // Output: 25 } else { echo "Key is not set"; } ?> Output25 Using array_key_first and array_key_last FunctionsThe array_key_first function returns the first key of the given array without affecting the internal array pointer, while the array_key_last function returns the last key of the given array.Example: PHP <?php $mixedArr = [ 10, 20, 'hello' => 'world', 30, ]; // Get the first key's value $firstKey = array_key_first($mixedArr); $firstValue = $mixedArr[$firstKey]; echo "First key's value: {$firstValue}\n"; // Get the last key's value $lastKey = array_key_last($mixedArr); $lastValue = $mixedArr[$lastKey]; echo "Last key's value: {$lastValue}\n"; ?> OutputFirst key's value: 10 Last key's value: 30 Comment More infoAdvertise with us Next Article How to get specific key value from array in PHP ? forpelevin Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-Questions Similar Reads How to create an array with key value pairs in PHP? In PHP, an array with key-value pairs is called an associative array. It maps specific keys to values, allowing you to access elements using custom keys rather than numerical indices. Keys are strings or integers, while values can be of any data type.Here we have some common approaches to create an 2 min read How to get the POST values from serializeArray in PHP ? When working with forms in web development, it's common to use JavaScript to serialize form data into a format that can be easily sent to the server. One popular method for serializing form data is using jQuery's serializeArray() function. However, once the data is serialized and sent to the server 2 min read How to check foreach Loop Key Value in PHP ? In PHP, the foreach loop can be used to loop over an array of elements. It can be used in many ways such asTable of ContentUsing the for-each loop with simple valuesUsing the foreach loop with Key-Value pairsUsing array_keys Function to Access Keys and ValuesUsing array_walk Function for IterationUs 3 min read How to Get Multiple Selected Values of Select Box in PHP? Given a list of items, the task is to retrieve the multiple selected values from a select box in PHP. Use multiple attributes in HTML to select multiple values from drop-down list. Selecting multiple values in HTML depends on operating system and browsers. For Windows users: hold down + CTRL key to 2 min read How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra 2 min read How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he 2 min read How to display array structure and values in PHP ? In this article, we will discuss how to display the array structure and values in PHP. To display the array structure and its values, we can use var_dump() and print_r() functions. It includes Array sizeArray valuesArray value with IndexEach value data type We will display the array structure using 2 min read How to reset Array in PHP ? You can reset array values or clear the values very easily in PHP. There are two methods to reset the array which are discussed further in this article. Methods: unset() Functionarray_diff() Function Method 1: unset() function: The unset() function is used to unset a specified variable or entire arr 2 min read How to remove duplicate values from array using PHP? In this article, we will discuss removing duplicate elements from an array in PHP. We can get the unique elements by using array_unique() function. This function will remove the duplicate values from the array.Syntax:array array_unique($array, $sort_flags);Note: The keys of the array are preserved i 4 min read How to Encode Array in JSON PHP ? Encoding arrays into JSON format is a common task in PHP, especially when building APIs or handling AJAX requests. Below are the approaches to encode arrays into JSON using PHP: Table of Content Using json_encode()Encoding Associative ArraysCustom JSON SerializationUsing json_encode()PHP provides a 2 min read Like