PHP Array Searching
In this page:
in_array($value, $array); // true or false
array_search($value, $array); // key, or false
array_key_exists($key, $array);
in_array से Values ढूँढना
in_array() check करता है कि कोई दी गई value किसी array में कहीं exist करती है या नहीं और एक simple boolean return करता है, जो 'क्या यह email पहले से registered है?' जैसे yes/no membership question का जवाब देने का सबसे तेज़ तरीका है।
उदाहरण: Finding Values with in_array
<?php
// Declare `$emails` as an array: `["[email protected]", "[email protected]"]`
$emails = ["[email protected]", "[email protected]"];
// Print a detailed dump (with types) of `in_array("[email protected]", $emails)`
var_dump(in_array("[email protected]", $emails));
?>
Login to try C/C++/Java/PHP code in the editor
array_search से Keys ढूँढना
array_search() in_array() जैसा ही है लेकिन सिर्फ true/false के बजाय पहले matching element की key return करता है, आपको exactly यह locate करने देते हुए कि कोई value कहाँ रहती है ताकि आप इसे update या remove कर सकें।
उदाहरण: Finding Keys with array_search
<?php
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Declare `$key`, set to `array_search("banana", $fruits)`
$key = array_search("banana", $fruits);
// Print `$key` to the output
echo $key;
?>
Login to try C/C++/Java/PHP code in the editor
Key Existence Check करना
array_key_exists() एक value के बजाय एक key की presence check करता है, जो मायने रखता है क्योंकि एक key null या empty value के साथ exist कर सकती है जिसे in_array() strict comparison settings के आधार पर miss कर देगा।
उदाहरण: Checking Key Existence
<?php
// Declare `$data` as an array: `["email" => null]`
$data = ["email" => null];
// Print a detailed dump (with types) of `array_key_exists("email", $data)`
var_dump(array_key_exists("email", $data));
// Print a detailed dump (with types) of `in_array(null, $data, true)`
var_dump(in_array(null, $data, true));
?>
Login to try C/C++/Java/PHP code in the editor
array_keys से Search करना
in_array() और array_search() दोनों एक optional strict parameter accept करते हैं, और true pass करना default loose == के बजाय === comparison force करता है, पुराने PHP versions में 0 == abc जैसे surprising matches से बचते हुए।
उदाहरण: Searching with array_keys
<?php
// Declare `$values` as an array: `[0, "abc", false]`
$values = [0, "abc", false];
// Print a detailed dump (with types) of `in_array("abc", $values)`
var_dump(in_array("abc", $values));
// Print a detailed dump (with types) of `in_array("abc", $values, true)`
var_dump(in_array("abc", $values, true));
?>
Login to try C/C++/Java/PHP code in the editor
array_filter का इस्तेमाल करके Advanced Search
Associative arrays के लिए, array_column() को array_search() के साथ combine करना आपको records की एक list में किसी specific field से search करने देता है, जैसे उस user का array index ढूँढना जिसका id किसी दी गई value से match करता है।
उदाहरण: Advanced Search using array_filter
<?php
// Declare `$users` as an array: `[["id" => 1, "name" => "Alice"], ["id" => 2, "name" => "Bob"]]`
$users = [["id" => 1, "name" => "Alice"], ["id" => 2, "name" => "Bob"]];
// Declare `$ids`, set to `array_column($users, 'id')`
$ids = array_column($users, 'id');
// Declare `$index`, set to `array_search(2, $ids)`
$index = array_search(2, $ids);
// Print `$index` to the output
echo $index;
?>
Login to try C/C++/Java/PHP code in the editor
in_arrayको mixed types पर तीसरे argumenttrueके बिना इस्तेमाल करना, ताकि loose comparison की वजह सेin_array("1e1", ["10"])true हो।array_searchकोif (array_search(...))से test करना, जो false है जब key 0 हो;!== falseसे compare करें।- एक null value वाली key check करने के लिए
issetइस्तेमाल करना, जो false return करता है; इसके बजायarray_key_existsइस्तेमाल करें।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: