function searchForId($id, $array) { foreach ($array as $key => $val) { if ($val['uid'] === $id) { return $key; } } return null; }
This will work. You should call it like this:
$id = searchForId('100', $userdb);
It is important to know that if you are using ===
operator compared types have to be exactly same, in this example you have to search string
or just use ==
instead ===
.
Based on angoru answer. In later versions of PHP (>= 5.5.0
) you can use one-liner.
$key = array_search('100', array_column($userdb, 'uid'));
Here is documentation: http://php.net/manual/en/function.array-column.php.
Related Posts:
- How to Sort a Multi-dimensional Array by Value
- Difference between “as $key => $value” and “as $value” in PHP foreach
- Array to String PHP?
- “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
- Convert a PHP object to an associative array
- How to echo or print an array in PHP?
- Deleting an element from an array in PHP
- How to insert an item at the beginning of an array in PHP?
- what is a good method to sanitize the whole $_POST array in php?
- Sort array of objects by object fields
- In_array not working
- Fatal error: Cannot use object of type mysqli_result
- php foreach with multidimensional array
- Convert php array to Javascript
- PHP reindex array?
- php – How do I fix this illegal offset type error
- Mysql where id is in array
- How to convert array to SimpleXML
- Array of PHP Objects
- Is there a function to make a copy of a PHP array to another?
- PHP array printing using a loop
- Fastest way to implode an associative array with keys
- PHP array delete by value (not key)
- PHP Multiple Checkbox Array
- Display array values in PHP
- Reset PHP Array Index
- How to push both value and key into PHP array
- Remove empty array elements
- currently unable to handle this request HTTP ERROR 500
- How to print the array?
- Form submission: PHP S_SESSION statements within a foreach loop
- PDOException: SQLSTATE[HY000] [2002] No such file or directory
- How do I get a file extension in PHP?
- mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
- How to parse a CSV file using PHP
- Cannot use object of type stdClass as array?
- Is a new line = \n OR \r\n?
- Elegant way to search for UTF-8 files with BOM?
- Email Not Sending from Byethost Hosting
- Illegal string offset Warning PHP
- mysqli_real_connect(): (HY000/2002): No such file or directory
- Converting string to Date and DateTime
- How can I do ‘insert if not exists’ in MySQL?
- How to solve PHP error ‘Notice: Array to string conversion in…’
- How do I fix “Undefined variable” error in PHP?
- The localhost page isn’t working localhost is currently unable to handle this request. HTTP ERROR 500
- How do you parse and process HTML/XML in PHP?
- What is the difference between the ‘www’ folder and ‘htdocs’ folder?
- Notice: Trying to get property of non-object error
- undefined offset PHP error
- Call to undefined function mysql_query() with Login
- What is Options +FollowSymLinks?
- Call to undefined method mysqli_stmt::get_result
- How to reformat date in PHP?
- Cannot simply use PostgreSQL table name (“relation does not exist”)
- How do I expire a PHP session after 30 minutes?
- How to make a countdown using PHP
- Package php5 have no installation candidate (Ubuntu 16.04)
- how to get id from URL using php
- mysql_field_name to the new mysqli
- mysqli_select_db() expects parameter 1 to be mysqli, string given
- Remove all special characters from a string
- how to convert object into string in php
- PHP Fatal error: Call to undefined function mssql_connect()
- Call to a member function on null?
- how to ping ip addresses in php and give results
- do_shortcode not working
- Only variable references should be returned by reference – Codeigniter
- PHP Unset Session Variable
- “Fatal error: Cannot redeclare function”
- What does MYSQLI_NUM mean and do?
- Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted (CodeIgniter + XML-RPC)
- php get all url variables
- How to remove unused dependencies from composer?
- how to get date of yesterday using php?
- How can I fetch all items from a DynamoDB table without specifying the primary key?
- How to extract and access data from JSON with PHP?
- WordPress query single post by slug
- TCPDF ERROR: Some data has already been output, can’t send PDF file
- Limit String Length
- Convert a 1D array to a 2D array in numpy
- How to POST JSON Data With PHP cURL?
- htmlentities() vs. htmlspecialchars()
- How to run PHP locally on MacOS
- Call PHP function from jQuery?
- PhpMailer SMTP NOTICE: EOF caught while checking if connected
- Creating and iterating a 2d array in Ruby
- What is the meaning of sprintf(): Too few arguments
- Paypal IPN Verification Postback with HTTPS
- Using PHP 7 with WAMP
- Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given… what I do wrong?
- PHP: bootstrap.php
- Php create a file if not exists
- What is a templating language?
- Page redirect after certain time PHP
- Logout button php
- Determining Referer in PHP
- Unset cookies php
- Check if $_POST exists
- PHP – Fatal error: Unsupported operand types