PHP में एक कुंजी के आधार पर एक सरणी तत्व को हटाने के लिए, कोड इस प्रकार है-
उदाहरण
<?php $arr = array( " John ", "Jacob ", " Tom ", " Tim "); echo "Array with leading and trailing whitespaces...\n"; foreach( $arr as $value ) { echo "Value = $value \n"; } echo "\nComma separated list...\n"; print_r(implode(', ', $arr)); $result = array_map('trim', $arr); echo "\nUpdated Array...\n"; foreach( $result as $value ) { echo "Value = $value \n"; } unset($result[1]); echo "\nUpdated Array...\n"; foreach( $result as $value ) { echo "Value = $value \n"; } ?>
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा-
Array with leading and trailing whitespaces... Value = John Value = Jacob Value = Tom Value = Tim Comma separated list... John , Jacob , Tom , Tim Updated Array... Value = John Value = Jacob Value = Tom Value = Tim Updated Array... Value = John Value = Tom Value = Tim
उदाहरण
आइए अब एक और उदाहरण देखें -
<?php $marks = array( "kevin" => array ( "physics" => 95, "maths" => 90, ), "ryan" => array ( "physics" => 92, "maths" => 97, ), ); echo "Marks for kevin in physics : " ; echo $marks['kevin']['physics'] . "\n"; echo "Marks for ryan in maths : "; echo $marks['ryan']['maths'] . "\n"; unset($marks["ryan"]); echo "Marks for ryan in maths : "; echo $marks['ryan']['maths'] . "\n"; ?>
आउटपुट
यह निम्नलिखित आउटपुट का उत्पादन करेगा। अब, एक त्रुटि दिखाई देगी क्योंकि हमने तत्व को हटा दिया है और इसे एक्सेस करने का प्रयास कर रहे हैं-
Marks for kevin in physics : 95 Marks for ryan in maths : 97 Marks for ryan in maths : PHP Notice: Undefined index: ryan in /home/cg/root/6985034/main.php on line 25