“php array_search” Réponses codées

Recherche PHP sur le tableau

//array_search
$result = array_search("apple", $fruit_array); // return index or false

//in_array
$result = in_array("apple", $fruit_array); // return true or false
Snippets

array_search en php


<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?>

Kaotik

array_search en php

$key = array_search(3, array_column($users, 'user_id'));

$userName = $users[$key]['first_name'];
Energetic Echidna

array_search en php

<?php
$a = [
'Canceled' => 1,
'Traded / Filled'=> 2,
'(Not used currently)'=> 3,
'Transit'=> 4,
'Rejected'=> 5,
'Pending'=> 6,
];
echo array_search("5",$a);
?>
Jaskaran

Recherche de valeur du tableau en php

function searchForId($id, $array) {
   foreach ($array as $key => $val) {
       if ($val['uid'] === $id) {
           return $key;
       }
   }
   return null;
}
Half Unicorn, Half Potato

php array_search

PHP function array_search(mixed $needle, array $haystack, bool $strict = false) false|int|string
---------------------------------------------------------------------------------------------
  
Searches the array for a given value and returns the first corresponding key if successful.
  
Parameters:
mixed--$needle--The searched value.If needle is a string, the comparison is done in a case-sensitive manner.
array--$haystack--The array.
bool--$strict--[optional] If the third parameter strict is set to true then the array_search function will also check the types of the needle in the haystack.
  
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.
Imtiaz Epu

Réponses similaires à “php array_search”

Questions similaires à “php array_search”

Plus de réponses similaires à “php array_search” dans PHP

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code