PHP Array Manipulation
In this page:
array_push($array, $value); // add to the end
array_pop($array); // remove from the end
array_unshift($array, $value); // add to the start
array_shift($array); // remove from the start
$part = array_slice($array, offset, length);
Ends से Add और Remove करना
array_push() और array_pop() क्रमशः किसी array के अंत से elements add और remove करते हैं, आपको एक PHP array को एक stack की तरह इस्तेमाल करने का एक simple तरीका देते हुए जब आपको last-in-first-out behavior चाहिए।
उदाहरण: Adding and Removing from Ends
<?php
// Declare `$stack` as an array: `[1, 2]`
$stack = [1, 2];
// Call `array_push($stack, 3)`
array_push($stack, 3);
// Call `array_pop($stack)`
array_pop($stack);
// Print a human-readable dump of `$stack`
print_r($stack);
?>
Login to try C/C++/Java/PHP code in the editor
शुरुआत से Add और Remove करना
array_shift() और array_unshift() अंत के बजाय किसी array की शुरुआत में वही करते हैं, लेकिन ध्यान दें कि वे सभी मौजूदा numeric keys को भी re-index करते हैं, जो बड़े arrays पर एक expensive operation हो सकता है।
उदाहरण: Adding and Removing from Beginning
<?php
// Declare `$queue` as an array: `[2, 3]`
$queue = [2, 3];
// Call `array_unshift($queue, 1)`
array_unshift($queue, 1);
// Call `array_shift($queue)`
array_shift($queue);
// Print a human-readable dump of `$queue`
print_r($queue);
?>
Login to try C/C++/Java/PHP code in the editor
Arrays को Slice करना
array_slice() original को modify किए बिना किसी array का एक हिस्सा extract करता है, जबकि array_splice() in place एक हिस्सा remove करता है और optionally replace करता है — इन दोनों को mix up करना unexpected side effects का एक common source है।
उदाहरण: Slicing Arrays
<?php
$arr = [1, 2, 3, 4, 5];
$slice = array_slice($arr, 1, 2);
print_r($slice);
print_r($arr); // unchanged
?>
Login to try C/C++/Java/PHP code in the editor
Arrays को Splice करना
array_merge() दो या ज़्यादा arrays को एक में combine करता है, बाद वाले arrays matching string keys पर पहले वालों को overwrite करते हुए लेकिन matching numeric keys पर बस append और renumber करते हुए — एक distinction जिसे carefully test करना उचित है।
उदाहरण: Splicing Arrays
<?php
// Declare `$arr1` as an array: `["a" => 1, "b" => 2]`
$arr1 = ["a" => 1, "b" => 2];
// Declare `$arr2` as an array: `["b" => 3, "c" => 4]`
$arr2 = ["b" => 3, "c" => 4];
// Print a human-readable dump of `array_merge($arr1, $arr2)`
print_r(array_merge($arr1, $arr2));
?>
Login to try C/C++/Java/PHP code in the editor
Duplicate Elements हटाना
unset($array[$key]) बाकी numeric keys को re-index किए बिना key से एक single element हटाता है, एक gap छोड़ते हुए — अगर आपको बाद में एक clean, renumbered list चाहिए, इसके बाद array_values() इस्तेमाल करें।
उदाहरण: Removing Duplicate Elements
<?php
// Declare `$arr` as an array: `[10, 20, 30]`
$arr = [10, 20, 30];
// Remove `$arr[1]`
unset($arr[1]);
// Print a human-readable dump of `$arr`
print_r($arr);
// Print a human-readable dump of `array_values($arr)`
print_r(array_values($arr));
?>
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
array_shiftयाarray_popनया array return करेंगे, जबकि वे removed element return करते हैं। - यह भूल जाना कि
array_sliceएक नया array return करता है और original को modify नहीं करता। array_splice(array को in place बदलता है) कोarray_slice(नहीं बदलता) से confuse करना।
- Arrays कई values store करते हैं और indexed, associative, या multidimensional हो सकते हैं।
- आप array items create, access, update, add, और remove कर सकते हैं।
- Array functions sorting, searching, और manipulation handle करते हैं।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: