PHP multi-dimensional array element operation method, php multi-dimensional array element

Source: Internet
Author: User

PHP multi-dimensional array element operation method, php multi-dimensional array element

In my framework, a multi-dimensional array element operation class is mainly used to read the configuration data in the array. You can use the string Node Method:. b. c To obtain and set elements and overwrite multi-dimensional arrays. For more information, see!

<? Php/*** Created by PhpStorm. * User: ZHOUZ * Date: 14-5-25 * Time: pm */namespace Snail IL \ Component; /*** multi-dimensional array Node read/write * Class Node * @ package Snail IL \ Component */class Node extends AComponent {/*** multi-dimensional array Node separator * @ var string */const SP = '. ';/*** generate a string Node for non-string data * <code> * Node: create (array ('A',' B ', 1, true )) => 'a. B .1.1 '* Node: create ('A',' B ', null, false) => 'a. B '* </code> * @ param mixed $ no Des supports arrays and any number of parameters * @ return string */public static function create ($ nodes ){! Is_array ($ nodes) & $ nodes = func_get_args (); return join (static: SP, array_filter (array_map ('strval ', $ nodes )));} /*** get the multi-dimensional array node and convert it to the array key name * @ param string $ node * @ return array */public static function explode ($ node) {return explode (static :: SP, $ node);}/*** set the multi-dimensional array node value * Note: * because the PHP array value key name length only supports the maximum length of int, * To avoid confusion such as coverage, when converting all nodes to a key name, * is forcibly converted to a string type * @ param array & $ arr references the memory address of the operated array * @ param string $ node path, for example,. b. c => $ arr ['a'] ['B'] ['C']; * @ param mixed $ value * @ return void */public static function set (array & $ arr, $ node, $ value = null) {$ arr = $ arr2 = (array) $ arr; $ keys = static: explode ($ node); foreach ($ keys as $ key) {$ key = strval ($ key ); if (isset ($ arr [$ key]) {$ arr = & $ arr [$ key];} else {$ arr [$ key] = array (); $ arr = & $ arr [$ key] ;}$ arr = $ value ;} /*** get the specified node value of the multi-dimensional array * @ param array & $ arr reference the memory address of the operated array * @ param string $ node path, for example,. b. c => $ arr ['a'] ['B'] ['C']; * @ param null | mixed $ default returns the default value if the node does not exist * @ return mixed */public static function get (array & $ arr, $ node, $ default = null) {if (empty ($ arr) return $ default; $ keys = static: explode ($ node); foreach ($ keys as $ key) {$ key = strval ($ key); if (isset ($ arr [$ key]) {$ arr = & $ arr [$ key];} else {return $ default;} return $ arr ;} /*** overwrite or add the second multi-dimensional array to the first multi-dimensional array for merging * @ param array & $ arraySrc reference the updated array * @ param array & $ arrayReplace reference required the merged data * @ return void address operation does not return a value */public static function merge (array & $ arraySrc, array & $ arrayReplace) {if (empty ($ arrayReplace) return; foreach ($ arrayReplace as $ k =>&$ v) {if (is_array ($ v) & isset ($ arraySrc [$ k]) {static: merge ($ arraySrc [$ k], $ v );} else {$ arraySrc [$ k] = $ v ;}}/ *** determines whether a specified node exists in a multi-dimensional array, * The result is the same as the isset keyword. (If the key exists but the value is NULL, false is returned) * @ param array & $ arr reference the original array * @ param string $ node * @ return bool */public static function has (array & $ arr, $ node) {if (empty ($ arr) return false; $ keys = static: explode ($ node); foreach ($ keys as $ key) {$ key = strval ($ key); if (isset ($ arr [$ key]) {$ arr = & $ arr [$ key];} else {return false ;}} return true ;} /*** determine whether a specified key exists in the multi-dimensional array * @ param array & $ arr references the original array * @ param string $ node * @ return bool */public static function hasKey (array & $ arr, $ node) {if (empty ($ arr) return false; $ keys = static: explode ($ node); foreach ($ keys as $ key) {$ key = strval ($ key); if (array_key_exists ($ key, $ arr) {$ arr = & $ arr [$ key];} else {return false ;}} return true ;} /*** deregister the specified node element in the array * @ param array $ arr * @ param string $ node * @ return void */public static function clear (array & $ arr, $ node) {if (empty ($ arr) return; $ keys = static: explode ($ node); foreach ($ keys as $ key) {$ key = strval ($ key); if (isset ($ arr [$ key]) {$ arr = & $ arr [$ key];} else {return; // The specified node does not exist and is considered as successful.} $ arr = null; unset ($ arr );}}

The following is an example:

<? Phpecho '<pre> '; // test array 1 $ arr1 = ['A' => ['B' => ['c' => ['D' => ['E' => ['E' => [' f' => 'HTTP: // blog.csdn.net/zhouzme'{%}%%%%%%%// test array 2 $ arr2 = ['A' => ['B' => ['c' => ['H' => ['I' => ['J' => 'HTTP: // www.hihtml5.com/'dimensional elements have been read into the existing multi-dimensional element: '; echo Node: get ($ arr1, 'a. b. c. d. e. f', 'default'); // http://blog.csdn.net/zhouzmeecho '

The output result is as follows:

The above is all the PHP multi-dimensional array element operation methods provided by xiaobian. I hope you can support more help ~

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.