PHP: How to find out the position of a value in an array?
The problem is that you don't have a numerical index on your array. Using array_values() will create a zero indexed array that you can then search using array_search() bypassing the need to use a for loop.,Other folks have suggested array_search() which gives the key of the array element where the value is found. You can ensure that the array keys are contiguous integers by using array_values():,You could loop over the array manually and find the index but why do it when there's a function for that. This function always returns a key and it will work well with associative and normal arrays., sorry, i didn't specify the type of array. The array i use is a multi-dimensional array please see my answer for the solution – Aakash Chakravarthy Jun 3 '10 at 4:38
From the docs:
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); $key = array_search('green', $array); // $key = 2; $key = array_search('red', $array); // $key = 1;
You want to know if an element is in an array, and, if it is, you want to know where it is located. ,If a value is in the array multiple times, array_search() is only guaranteed to return one of the instances, not the first instance. , Use the !== check against false because if your string is found in the array at position 0, the if evaluates to a logical false, which isn’t what is meant or wanted. ,Take O’Reilly with you and learn anywhere, anytime on your phone and tablet.
array_values() - Return all the values of an array, Returns the key for needle if it is found in the array, false otherwise. , If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead. ,in_array() - Checks if a value exists in an array
$people = array( 2 => array( 'name' => 'John', 'fav_color' => 'green' ), 5 => array( 'name' => 'Samuel', 'fav_color' => 'blue' )); $found_key = array_search('blue', array_column($people, 'fav_color'));
Iterate associative array using foreach loop in PHP,How to delete an array element based on key in PHP?,How to loop through an associative array and get the key in PHP?,How to access an associative array by integer index in PHP?
array array_keys($input_array, $search_value, $strict)
The array_search() function searches an array for a given value and returns the key. The function returns the key for val if it is found in the array. It returns FALSE if it is not found. If val is found in the array arr more than once, then the first matching key is returned.,The array_search() function returns the key for val if it is found in the array. It returns FALSE if it is not found. If val is found in the array arr more than once, then the first matching key is returned.,strict − Possible values are TRUE or FALSE. Search for identical elements in the array, set to TRUE.,arr − The array to be searched
array_search(val, arr, strict)
The PHP array_search() is an inbuilt function that is widely used to search and locate a specific value in the given array. If it successfully finds the specific value, it returns its corresponding key value. If the element is found twice or more, then the first occurrence of the matching value's key will be returned.,The array_search() function returns the corresponding element's key that is passed.,$value (required):This parameter represents the value that the user wishes to search in the given array.,Example 3: In the below program, we learn how to search for an array value with the help of array_search() function if the strict_parameter is set to TRUE.
Dilip is at position 1
If you want to access an individual value form an indexed, associative or multidimensional array you can either do it through using the array index or key.,Is this website helpful to you? Please give us a like, or share your feedback to help us improve. Connect with us on Facebook and Twitter for the latest updates.,How to print or echo all the values of an array in PHP,How to remove duplicate values from an array in PHP
<?php // Indexed array $sports = array("Baseball", "Cricket", "Football", "Shooting"); // Associative array $cities = array("France"=>"Paris", "India"=>"Mumbai", "UK"=>"London", "USA"=>"New York"); // Multidimensional array $superheroes = array( array( "name" => "Peter Parker", "character" => "Spider-Man", ), array( "name" => "Tony Stark", "character" => "Iron-Man", ), array( "name" => "Clark Kent", "character" => "Super-Man", ) ); echo $sports; // Outputs: Baseball echo "<br>"; echo $sports; // Outputs: Cricket echo "<br>"; echo $cities["France"]; // Outputs: Paris echo "<br>"; echo $cities["USA"]; // Outputs: New York echo "<br>"; echo $superheroes["name"]; // Outputs: Peter Parker echo "<br>"; echo $superheroes["character"]; // Outputs: Iron-Man ?>
Other "position-undefined" queries related to "PHP: How to find out the position of a value in an array?"
- Finding file in document in firestore
- FindOneAndDelete on MERN stack would return successful but the data is still in database?
- Facing Expected atom at position error in regular expression
- Material-UI Drawer: How to position drawer in a specific div
- I am getting findDOMNode is deprecated in StrictMode for my own select field component using material UI [duplicate]
- How to fix : Cannot find module 'webpack/bin/config-yargs'
- 'position: fixed' on button within a div hides the button
- How can I find an attribute in 2*2 and 3*3 object in reactjs?
- Cannot find module '@fortawesome/react-fontawesome
- AG Grid Community React version complaining “could not find matching row model for rowModelType clientSide”
- While drawing on a HTML5 canvas using mouse, mouse position is not correctly positioned
- Typescript react - Could not find a declaration file for module ''react-materialize'. 'path/to/module-name.js' implicitly has an any type
- Npm cant find package.json when installing react
- Typescript: Cannot find module 'react'
- Uncaught TypeError: Cannot read property 'getZoom' of undefined. Not able to get current zoom position
- Why react js cannot find my exported modules?
- How to change a slide out drawer in react grid to position fixed
- Scroll on mouse position [closed]
- Typescript cannot find module when import svg file
- “SyntaxError: Unexpected token < in JSON at position 0”
- How to avoid can't find module
- Scroll horizontally and vertically on mouse position
- How to get parent props from child composition component React
- PHP show no messages in GitBash under Windows 10
- How to make MySQL treat underscore as a word separator for fulltext search?
- Read_exif_data not showing the correct data
- How to install php7?
- SQLSTATE: Integrity constraint violation: 1048 Column 'post' cannot be null
- Saving request to database
- Custom php file directory in Wordpress with Timber Twig
- Skip a few columns in a row and then write
- Edit woocommerce shipping calculator
- Can't make a custom Signature validator class [Laravel 8 Spatie Webhook Client]
- Trying to add versioning using filemtime but fail
- Php multiple curl urls with while loop
- How to align all td to center except a td with class
- PHP image crop from CSS Transform
- Jquery mobile logging in and accessing/rejecting page
- How to store credentials for basic HTTP authenication into the database on server side?
- How do I make these links go horizontally and not vertically?
- Transaction issue from one card (Vue App) to other card (in Laravel App)
- Use a while loop to display unlimited data in a 5 column Bootstrap Carousel