[PHP] Return to the basics (Array related functions), return to array_PHP tutorial

WBOY
Release: 2016-07-12 08:52:29
Original
793 people have browsed it

[PHP] Return to the basics (Array related functions), return to array

Use the function array_keys() to get all the keys in the array, parameters: array

<span>$arr</span>=<span>array</span><span>();
</span><span>$arr</span>['one']="one"<span>;
</span><span>$arr</span>['two']="two"<span>;
</span><span>$arr</span>['three']="three"<span>;

</span><span>$newArr</span>=<span>array_keys</span>(<span>$arr</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one [1] => two [2] => three )</span>
Copy after login

Use the function array_values() to get all the values ​​in the array. Parameters: array

<span>$arr</span>=<span>array</span><span>();
</span><span>$arr</span>[20]="one"<span>;
</span><span>$arr</span>[30]="two"<span>;
</span><span>$arr</span>[40]="three"<span>;

</span><span>$newArr</span>=<span>array_values</span>(<span>$arr</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one [1] => two [2] => three )</span>
Copy after login

Use the function array_map() to call a custom function for each element. Parameters: String type function name, array

<span>$arr</span>=<span>array</span><span>();
</span><span>$arr</span>[0]="one"<span>;
</span><span>$arr</span>[1]="two"<span>;
</span><span>$arr</span>[2]="three"<span>;
</span><span>function</span> test(<span>$v</span><span>){
    </span><span>return</span> <span>$v</span>." Hello"<span>;
}
</span><span>$newArr</span>=<span>array_map</span>("test",<span>$arr</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one Hello [1] => two Hello [2] => three Hello )</span>
Copy after login

Use the function array_merge() to merge two arrays into one, parameters: array, array

When associative arrays are merged, those with the same key will be overwritten by the subsequent array

When index arrays are merged, they will be connected together to form a new array

<span>$arr</span>=<span>array</span><span>();
</span><span>$arr</span>[0]="one"<span>;
</span><span>$arr</span>[1]="two"<span>;
</span><span>$arr</span>[2]="three"<span>;

</span><span>$arr1</span>=<span>array</span><span>();
</span><span>$arr</span>[3]="taoshihan1"<span>;
</span><span>$arr</span>[4]="taoshihan2"<span>;
</span><span>$arr</span>[5]="taoshihan3"<span>;

</span><span>$newArr</span>=<span>array_merge</span>(<span>$arr</span>,<span>$arr1</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one [1] => two [2] => three [3] => taoshihan1 [4] => taoshihan2 [5] => taoshihan3 )</span>

<span>$arr</span>=<span>array</span>("one","two","three"<span>);
</span><span>$arr1</span>=<span>array</span>("4","5","6"<span>);
</span><span>$newArr</span>=<span>array_merge</span>(<span>$arr</span>,<span>$arr1</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one [1] => two [2] => three [3] => 4 [4] => 5 [5] => 6 )</span>
Copy after login

Use the function ksort() to sort by key name. Note that no new array is returned, but the original array

<span>$arr</span>=<span>array</span>("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3"<span>);
</span><span>ksort</span>(<span>$arr</span><span>);
</span><span>print_r</span>(<span>$arr</span><span>);
</span><span>//</span><span>Array ( [1] => taoshihan1 [2] => taoshihan2 [3] => taoshihan3 )</span>
Copy after login

Use the function array_search() to search for a certain key value and return the corresponding key

<span>$arr</span>=<span>array</span>("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3"<span>);
</span><span>echo</span> <span>array_search</span>("taoshihan1",<span>$arr</span><span>);
</span><span>//</span><span> 1</span>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1126176.htmlTechArticle[PHP] Return to the basics (Array related functions), return to the array, use the function array_keys() to get the array All keys, parameters: array $arr = array (); $arr ['one']="one" ; $arr ['two']=...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template