If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
जब हम arrays के साथ काम करते हैं, तो कभी कभी हमें particular element को remove करना पड़ता है। ये element किसी भी position पर हो सकता है, या फिर हमें value-based removal करनी होती है।
इन scenarios में हम manual methods use करते हैं, जिसमे predefined functions (जैसे array_splice(), unset(), etc.) का use नहीं होता।
इस blog में हम PHP में array से particular element remove करने के दो different approaches देखेंगे :
Index-based removal
Value-based removal
हम manually यह कैसे कर सकते हैं, बिना predefined functions के? चलिए, step-by-step समझते हैं !
हम सभी examples PHP में लेने वाले हैं , हालाँकि बाकी language में अगर आप practice करेंगे तो concept same ही रहेगा ,बस syntax change हो जायगा।
●●●
Index-based removal का मतलब है कि हम directly एक particular index (position) पर जो element है, उससे remove करेंगे। हम manually array को re-arrange करेंगे ताकि वो element remove हो जाये और array का structure intact रहे।
<?php
// Function to remove an element by index from an array
function removeByIndex($array, $index) {
// Initialize a new array to store the result
$newArray = [];
// Loop through the array
for ($i = 0; $i < count($array); $i++) {
// Skip the element if it is the one to be removed
if ($i != $index) {
$newArray[] = $array[$i];
}
}
return $newArray;
}
// Example usage:
$array = [10, 20, 30, 40, 50];
$index = 2; // We want to remove the element at index 2 (which is 30)
$updatedArray = removeByIndex($array, $index);
echo "Array after removing element at index $index: ";
print_r($updatedArray);
?>Explanation:
हमने removeByIndex() function बनाया जो array और index लेता है।
हम for loop के through array को traverse करते हैं और अगर current index वो नहीं है जो remove करना है, तो उस element को नया array में add करते हैं।
इस process में हम manually array को adjust करते हैं बिना किसी predefined function के।
ऊपर दिए गए example में new array create किया गया है अगर बिना new array create करके same array से remove करना हो तो कुछ ऐसे कर सकते हैं।
// Function to remove an element by index from the same array
function removeByIndexInPlace($array, $index) {
// Loop through the array starting from the index after the removed element
for ($i = $index; $i < count($array) - 1; $i++) {
$array[$i] = $array[$i + 1]; // Shift elements left
}
// Remove the last element of the array since it's duplicated after shifting
array_pop($array);
return $array;
}हम array को loop करते हैं और जो element index के बाद आता है, उससे एक position shift कर देते हैं। array_pop() से हम last element remove करते हैं, जो अब shifting के बाद redundant हो जाता है।
●●●
Value-based removal का मतलब है कि हम directly array में से किसी specific value को remove करेंगे, चाहे वो किसी भी index पर हो।
यहाँ भी हम manual method use करेंगे, जिसमे हम array को traverse करेंगे और जो value match करेगी उससे remove करेंगे।
<?php
// Function to remove an element by value from an array
function removeByValue($array, $value) {
// Initialize a new array to store the result
$newArray = [];
// Loop through the array
foreach ($array as $element) {
// Only add elements that are not equal to the value we want to remove
if ($element != $value) {
$newArray[] = $element;
}
}
return $newArray;
}
// Example usage:
$array = [10, 20, 30, 40, 50];
$value = 30; // We want to remove the value 30 from the array
$updatedArray = removeByValue($array, $value);
echo "Array after removing the value $value: ";
print_r($updatedArray);
?>Explanation:
हमने removeByValue() function बनाया जो array और value लेता है।
हम foreach loop से array को traverse करते हैं , अगर element match नहीं करता with थे value तो be removed, तो उससे नया array में add करते हैं।
इस method में हम value को remove करते हैं बिना index के, बस value match hone पर।
●●●
Index-based Removal: इस method में हम directly index का use करते हैं। अगर आपको specific position पर element remove करना हो तो यह approach best है।
Value-based Removal : इस method में हम किसी value को target करते हैं और उस value को array से remove करते हैं, regardless of its position .
Manual Methods: इन दोनो methods में हम predefined functions का use नहीं कर रहे हैं। सब कुछ manually manage किया जा रहा है।
तो आज हमने सीखा कि कैसे PHP में हम किसी particular element को remove कर सकते हैं बिना predefined functions का use किये।
हमने index-based removal और value-based removal दोनो methods को manually implement किया है। यह approaches especially useful हैं जब आपको custom array manipulation करना हो बिना built-in functions के।
●●●
Loading ...