Php String Array

Source: Internet
Author: User

This article is not about the use of split and explode, but about a special situation.

Example.

 $ Array = array (2, 'A' => array ('B' => 'C '));
// List of deep keys
$ Keys = array ('A', 'B ');
// Value
$ Vals = 'X ';

// The problem is how to change the value of the corresponding array $ array according to the $ keys array (dynamic)
// For example, you must change the value of $ array ['a'] ['B'] from 'C' to 'x'
// Note that $ keys is dynamic or array ('A', 'C', 'x', 'D ').
// You must set $ array ['a'] ['C'] ['X'] ['D'] To 'x ';

 

 

At first glance, it seems very simple. Can eval solve it? Indeed, but eval is strictly prohibited on many servers,

Except for eval, is there a preg_replace + e modifier? Yes, but there are always security concerns,

Isn't it as dangerous to inject system/exec/shel_exec Function + system commands as eval?

Finally, we thought of parse_str, which is relatively safe.

 

 $ Array = array (2, 'A' => array ('B' => 'C '));
// List of deep keys
$ Keys = array ('A', 'B ');
// Value
$ Vals = 'X ';
// Convert string to array
Parse_str ('array ['. implode ('] [', $ keys).'] = '. $ vals );

Print_r ($ array );

 

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.