324
It is quite easy to delete an array or change value of array key in PHP. When we know an array (one dimensional or multi-dimensional)
Function : unset()
This function destroys key and its value. On other words, we remove a key from an array
Syntax:
unset($variable);
Parameters:
This function only one parameter.
Example 1:
OutPut:
Array
(
[0] => A
[1] => B
[2] => C
[3] => D
[4] => E
)
Array
(
[0] => A
[1] => B
[2] => C
[4] => E
)
Now we will look at how we remove array key and its value in associative array
Example 2:
array(
// the key value pair
"price" => 5,
"qty" => 2,
),
"grapes" => array(
// the key value pair
"price" => 4,
"qty" => 3,
),
"mango" => array(
// the key value pair
"price" => 4,
"qty" => 3,
),
);
echo "Orginal array
";
// Display Results
print_r($products);
// Now Use unset() function to delete elements
unset($products["mango"]);
echo "After delete the element
";
// Display Results
print_r($products);
Output:
Orginal array
Array
(
[apple] => Array
(
[price] => 5
[qty] => 2
)
[grapes] => Array
(
[price] => 4
[qty] => 3
)
[mango] => Array
(
[price] => 4
[qty] => 3
)
)
After delete the element
Array
(
[apple] => Array
(
[price] => 5
[qty] => 2
)
[grapes] => Array
(
[price] => 4
[qty] => 3
)
)
PHP is a server side programming and after so many years, this is most common web programming language. WordPress is a most common CMS and this CMS is built using PHP programming.
You Might Be Interested In
- How to Get attributes of product from product ID ?
- Custom redirect on clicking add to cart in WooCommerce
- How can you trigger a Bootstrap modal programmatically?
- How to Change WooCommerce Coupon Error Messages?
- What is the difference between add_filter and apply_filters?
- How to add meta box in WordPress?
Post Views: 443