Home > Article > Backend Development > Detailed explanation of how to find and delete array values in php_PHP tutorial
Arrays in PHP are divided into array values and array keys. The editor below will summarize the commonly used operation methods of array values in PHP, including: adding values to the array, judging the values in the array, and deleting specific array values. Students in need can refer to it.
php delete specific array value
First of all
代码如下 | 复制代码 |
var_dump($context['linktree']); |
Get
代码如下 | 复制代码 |
array(3) { [0]=> array(2) { ["url"]=> string(52) “http://127.0.0.1/testforum.cityofsteam.com/index.php” ["name"]=> string(28) “City of Steam Official Forum” } [1]=> array(2) { ["url"]=> string(55) “http://127.0.0.1/testforum.cityofsteam.com/index.php#c1″ ["name"]=> string(28) “City of Steam Official Forum” } [2]=> array(2) { ["url"]=> string(62) “http://127.0.0.1/testforum.cityofsteam.com/index.php?board=4.0″ ["name"]=> string(12) “Announcement” } } |
I'm going to get rid of the middle one.
Use: unset($context['linktree']['1']);
Result:
The code is as follows | Copy code | ||||||||||||
[0]=> array(2) {["url"]=> string(52) “http://127.0.0.1/testforum.cityofsteam.com/index.php”
string(28) “City of Steam Official Forum”
|
The code is as follows | Copy code |
Array ( [0] => apple [1] => banana [3] => dog ) |
The code is as follows | Copy code |
<🎜>function array_remove(&$arr, $offset)<🎜> <🎜>{ <🎜> <🎜> array_splice($arr, $offset, 1);<🎜> <🎜>}<🎜> <🎜>$arr = array('apple','banana','cat','dog'); <🎜> <🎜> array_remove($arr, 2); <🎜> <🎜> print_r($arr);<🎜> <🎜>?> |
After testing, we can know that the element at position 2 has been truly deleted and the index has been re-established.
Program execution result:
The code is as follows | Copy code | ||||
|
in_array
in_array — Check if a value exists in an array
Description
Search for needle in haystack and return TRUE if found, otherwise return FALSE.
If the value of the third parameter strict is TRUE, the in_array() function will also check whether the type of needle is the same as that in haystack.
Note: If needle is a string, the comparison is case-sensitive.
Note: Prior to PHP version 4.2.0, needle was not allowed to be an array.
Example 292. in_array() example
代码如下 | 复制代码 |
$os = array("Mac", "NT", "Irix", "Linux"); if (in_array("Irix", $os)) { echo "Got Irix"; } if (in_array("mac", $os)) { echo "Got mac"; } ?> |
The code is as follows | Copy code | ||||||||||||||||||||||||
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) { echo "Got Irix"; }if (in_array("mac", $os)) { echo "Got mac";
?>
We can implement this through functions, inserting one or more elements into the array, or adding them directly.
print_r ( $shili ) ; http://www.bkjia.com/PHPjc/631553.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631553.htmlTechArticleArrays in php are divided into array values and array keys. The following editor will give you a summary of arrays in php Commonly used operations on values include: adding values to arrays, judging values in arrays, deleting...Statement: The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn Previous article:PHP matching character link address program code_PHP tutorialNext article:PHP matching character link address program code_PHP tutorial Related articlesSee more |