How to get specific key value from array in PHP ? Last Updated : 04 Jul, 2024 Summarize Comments Improve Suggest changes Share 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 ? F forpelevin Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-Questions Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Like