PHP Multidimensional array element Action class method _php instance

Source: Internet
Author: User
Tags arrays explode mixed

My framework inside a multidimensional array element action class, mainly used to read the configuration data in the array, through the string node way: A.B.C to get and set the elements, as well as multidimensional array coverage, there is a need to refer to the next bar!

<?php/** * Created by Phpstorm.

* User:zhouz * date:14-5-25 * Time: Afternoon 5:32 * * namespace snail\component; /** * Multidimensional Array node read-write * class node * @package Snail\component/class node extends Acomponent {/** * Multidimensional array node separator * @v

  AR String */const SP = '. '; /** * Generate string-type data Strings node * <code> * node::create (Array (' A ', ' B ', 1, true)) => ' a.b.1.1 ' * node::create ( ' A ', ' B ', NULL, false) => ' a.b ' * </code> * @param mixed $nodes supports arrays, any number of parameters * @return string/PU
    Blic static function Create ($nodes) {! Is_array ($nodes) && $nodes = Func_get_args ();
  Return join (STATIC::SP, Array_filter (Array_map (' Strval ', $nodes));
    /** * Gets the multidimensional array node to convert the array key name * @param string $node * @return Array */public static function explode ($node) {
  Return explode (STATIC::SP, $node); /** * Set multidimensional array node value * NOTE: * Because the PHP array numeric key length only supports the maximum length of int, * in order to avoid the confusion of overlay, all nodes will be cast * to the string type when converting to the key name * @param array & $aThe RR references the memory address * @param string $node node path of the manipulated array, such as: A.B.C => $arr [' a '] [' B '] [' C ']; * @param mixed $value * @return void */public static function set (array & $arr, $node, $value = null) {$
    arr = $arr 2 = (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 multidimensional array specified node value * @param array & $arr reference the memory address of the manipulated array * @param string $node node path, such as: A.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; /** * Overlay or add a second multidimensional array to the first multidimensional array merge * @param array & $ARRAYSRC reference the updated array * @param array & $arrayReplace Primer  With data that needs to be merged * @return Void address Operation no return 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::mer
      GE ($ARRAYSRC [$k], $v);
      else {$ARRAYSRC [$k] = $v;
   }}/** * To determine if a specified node exists in a multidimensional array, * results with the Isset keyword (false if there is a key but null) * @param array & $arr reference the original array * @param string $node * @return bool/public static function has (array & $arr, $node) {if Empty ($ar
    R)) 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; /** * To determine if the specified key * @param array & $arr reference in the multidimensional array * @param string $node * @return bool/Public
    The 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; /** * Unregister the specified node element * @param array $arr * @param string $node * @return void */public static Functio
    N 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 to be successful}} $arr = null;
  Unset ($arr);  }
}

Here's a chestnut:

<?php
echo ' <pre> ';
Test array 1
$arr 1 = [' A ' =>[' B ' =>[' C ' =>[' d ' =>[' e ' =>[' F ' => ' http://blog.csdn.net/zhouzme ']]];
Test Array 2
$arr 2 = [' A ' =>[' B ' =>[' C ' =>[' h ' =>[' I ' =>[' j ' => ' http://www.hihtml5.com/']]]];

Echo reads an existing multidimensional element: ';
Echo Node::get ($arr 1, ' a.b.c.d.e.f ', ' default value '); Http://blog.csdn.net/zhouzme
echo '  
 

The corresponding output is shown below:

The above is a small series for everyone to bring the PHP multidimensional array elements of the method of operation of all the content, I hope that we support the cloud Habitat Community ~

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.