Added the php array value and added the delete search operation method. _ PHP Tutorial

Source: Internet
Author: User
Add the delete search operation method to the php array value. The php array is divided into array values and array keys. the following small series will summarize the common operations for array values in php: adding values to the array, judging the values in the array, and deleting the values in the php array are divided into array values and array keys, the following is a summary of the common operations for array values in php: if you need to add values to the array, determine the values in the array, and delete specific array values, you can refer.

Php deletes a specific array value

First

The code is as follows:
Var_dump ($ context ['linktree']);

Get

The code is as follows:
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 want to remove the middle one.

Use: unset ($ context ['linktree'] ['1']);

Result:

The code is as follows:

Array (2 ){
[0] =>
Array (2 ){
["Url"] =>
String (52) "http: // 127.0.0.1/testforum.cityofsteam.com/index.php"
["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"
}
}

One [1] is missing.

Let the middle 1 be automatically numbered:

The code is as follows:


Array ([0] => apple [1] => banana [3] => dog)

However, the biggest disadvantage of this method is that the array index is not rebuilt, that is, the third element of the array is gone.
After checking the information, PHP provided this function, but it was indirect. This function is array_splice ().
For ease of use, I encapsulated a function for your convenience:

The code is as follows:

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 2 is actually deleted and the index is re-created.

Program running result:

The code is as follows:

Array ([0] => apple [1] => banana [2] => dog)


Php determines the value in the array


There are special functions. do not use a for loop. system functions can achieve quick search:

In_array
(PHP 4, PHP 5)

In_array-check whether a value exists in the array

Description
Bool in_array (mixed $ needle, array $ haystack [, bool $ strict])

Search for needle in haystack. If yes, TRUE is returned. otherwise, FALSE is returned.

If the value of the third strict parameter is TRUE, the in_array () function checks whether the needle type is the same as that in haystack.

Note: If the needle is a string, the comparison is case sensitive.

Note: Before PHP version 4.2.0, needle cannot be an array.

Example 292. in_array ()

The code is as follows:
$ OS = array ("Mac", "NT", "Irix", "Linux ");
If (in_array ("Irix", $ OS )){
Echo "Got Irix ";
}
If (in_array ("mac", $ OS )){
Echo "Got mac ";
}
?>

The second condition fails, because in_array () is case sensitive, so the above program is shown:

Got Irix

Example 293. in_array () strict type check example

The code is as follows:

$ A = array ('1', 12.4, 1.13 );

If (in_array ('12. 4', $ a, true )){
Echo "'12. 4' found with strict checkn ";
}
If (in_array (1.13, $ a, true )){
Echo "1.13 found with strict checkn ";
}
?>
The above example will output:

1.13 found with strict check

Example 294. in_array () uses arrays as needle

The code is as follows:

$ A = array ('P', 'H'), array ('P', 'r'), 'o ');

If (in_array (array ('P', 'H'), $ )){
Echo "'ph' was foundn ";
}
If (in_array (array ('F', 'I'), $ )){
Echo "'Fi 'was foundn ";
}
If (in_array ('O', $ )){
Echo "'O' was foundn ";
}
?>
The above example will output:

'Ph 'was found
'O' was found


Add a value to an array

We can use functions to insert one or more elements into an array or add them directly.
(1) add data directly to the array. the subscript of the new element starts after the maximum value of the original array subscript.
(2) the array_unshift () function adds one or more elements at the beginning of the array.
Syntax:
Int array_unshift (array & array, mixed var [, mixed...]);
Array_unshift () inserts the input elements into the beginning of the array. Elements are inserted as a whole, and the incoming elements are in the same order. The names of all numeric keys start from 0 and remain unchanged.
(3) the array_push () function adds one or more units to the end of the array.
Syntax:
Int array_push (array & array, mixed var [, mixed...]);
Array_push () treats array as a stack and adds the passed variables to the end of array. This function returns the total number of new elements in the array. An example of adding data to an array is as follows.
Example:

The code is as follows:
$ Shili = array ("1", "2", "3", "4 ″);
$ Shili [] = 5; // add data directly
Print_r ($ shili );
Echo"
";
$ Shili2 = array ("m", "n ");
Array_unshift ($ shili2, "o", "p"); // add the element at the beginning of the array
Print_r ($ shili2 );
Echo"
";
$ Shili3 = array ("Php ");
Array_push ($ shili3, "MySQL", "Apache"); // add the element to the end of the array
Print_r ($ shili3 );
?>
Result:
Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5)
Array ([0] => o [1] => p [2] => m [3] => n)
Array ([0] => Php [1] => MySQL [2] => Apache)

Values in the values array, delete...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.