← Back to PHP Course | Chapter 7: Arrays | Lesson 10 of 13

PHP Array Functions

Array functions lists के लिए ready-made tools हैं, जैसे items count करना, lists जोड़ना, या सिर्फ कुछ चुनना। PHP ने पहले से इन्हें बनाया है ताकि आपको इन्हें खुद न लिखना पड़े।
Syntax
php
count($array);
array_merge($array1, $array2);
array_keys($array);
array_values($array);
array_map(fn($x) => expression, $array);
array_filter($array, fn($x) => condition);

Array Length पाना

PHP सौ से ज़्यादा built-in array functions के साथ आता है, values transform करने से लेकर filtering, combining, और arrays inspect करने तक हर चीज़ को cover करते हुए — common वालों को जानना आपको routine tasks के लिए हाथ से loops लिखने से बचाता है।

उदाहरण: Getting Array Length

php
<?php
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Print `count($fruits)` to the output
echo count($fruits);
?>

Arrays को Merge करना

array_map() किसी array के हर element पर एक दिया गया function apply करता है और results का एक नया array return करता है, जो एक पूरी list को एक साथ transform करने का idiomatic तरीका है, जैसे हर number को double करना या हर string को uppercase करना।

उदाहरण: Merging Arrays

php
<?php
// Declare `$numbers` as an array: `[1, 2, 3]`
$numbers = [1, 2, 3];
$doubled = array_map(function ($n) {
    // Return `$n * 2` from this function
    return $n * 2;
}, $numbers);
// Print a human-readable dump of `$doubled`
print_r($doubled);
?>

Keys और Values पाना

array_filter() सिर्फ उन elements को रखता है जो एक callback function satisfy करते हैं, बाकी हटाए जाने के साथ एक नया array return करते हुए — किसी पूरी user list में से सिर्फ active users निकालने जैसे tasks के लिए उपयोगी।

उदाहरण: Getting Keys and Values

php
<?php
$users = [
    ["name" => "Alice", "active" => true],
    ["name" => "Bob", "active" => false],
];
$activeUsers = array_filter($users, function ($u) {
    // Return `$u["active"]` from this function
    return $u["active"];
});
// Print a human-readable dump of `$activeUsers`
print_r($activeUsers);
?>

Arrays Filter करना

array_reduce() बार-बार एक callback apply करके किसी array को एक single value में collapse कर देता है जो हर element के साथ एक accumulator combine करता है, जो है कि आप किसी list से running total या concatenated string जैसा कुछ कैसे compute करेंगे।

उदाहरण: Filtering Arrays

php
<?php
// Declare `$numbers` as an array: `[1, 2, 3, 4]`
$numbers = [1, 2, 3, 4];
$total = array_reduce($numbers, function ($carry, $n) {
    // Return `$carry + $n` from this function
    return $carry + $n;
}, 0);
// Print `$total` to the output
echo $total;
?>

Array Elements को Map करना

in_array() और array_key_exists() क्रमशः एक value या एक key check करते हैं, और इन्हें confuse करना आसान है — in_array() values search करता है, array_key_exists() keys search करता है, और उन्हें mix up करना bugs का एक common source है।

उदाहरण: Mapping Array Elements

php
<?php
// Declare `$fruits` as an array: `["apple", "banana"]`
$fruits = ["apple", "banana"];
// Print a detailed dump (with types) of `in_array("apple", $fruits)`
var_dump(in_array("apple", $fruits));

// Declare `$person` as an array: `["name" => "Alice"]`
$person = ["name" => "Alice"];
// Print a detailed dump (with types) of `array_key_exists("name", $person)`
var_dump(array_key_exists("name", $person));
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि array_merge numeric keys रखेगा, जबकि यह उन्हें फिर से number कर देता है; पहले array की keys रखने के लिए + इस्तेमाल करें।
  2. यह उम्मीद करना कि array_filter result को reindex करेगा, जबकि यह original keys preserve करता है।
  3. यह भूल जाना कि array_map एक नया array return करता है और original को modify नहीं करता।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.