PHP implements the method of generating corresponding array according to the string, the PHP string corresponds to the array
In this paper, we describe the method that PHP implements to generate corresponding array based on string, which is a practical technique. Share to everyone for your reference. Here's how:
Let's look at the following example:
<?php $config = Array ( ' project|page|index ' = ' content ', ' project|page|nav ' = = Array ( array ( ' image ' = ' 1.jpg ', ' name ' = ' home ', Array ( ' image ' = ' 2.jpg ', ' name ' = ' = ') ' About ' ) ), ' project|page|open ' = true);?>
The following arrays are generated according to $config:
<?php $result = Array (' project ' = = Array (' page ' = = Array ( ' index ' = ' content ', ' nav ') = = Array ( ' image ' = ' 1.jpg ', ' name ' = ' Home ' ), Array ( ' image ' = ' 2.jpg ', ' name ' = ' about ' ) , ' open ' = True )
Method: Use eval to implement:
<?php $config = Array ( ' project|page|index ' = ' content ', ' project|page|nav ' = = Array ( array ( ' image ' = ' 1.jpg ', ' name ' = ' home ', Array ( ' image ' = ' 2.jpg ', ' name ' = ' = ') ' About ' ) ), ' project|page|open ' = true); $result = Array (); foreach ($config as $key = + $val) { $tmp = '; $keys = Explode (' | ', $key); For ($i =0, $len =count ($keys), $i < $len, $i + +) { $tmp. = "['". $keys [$i]. "']"; } if (Is_array ($val)) { eval (' $result '. $tmp. ' = '. Var_export ($val, true). '; '); } ElseIf (is_string ($val)) { eval (' $result '. $tmp. ' = '. $val. '; '); } else{ eval (' $result '. $tmp. ' = $val; '); } } Print_r ($result);
Output Result:
Array
(
[Project] = Array
(
[Page] = = Array
(
[Index] = content
[NAV] = = Array
(
[0] = = Array
(
[Image] = 1.jpg
[Name] = Home
)
[1] = = Array
(
[Image] = 2.jpg
[Name] = about
)
)
[Open] = 1
)
)
)
I hope this article will be helpful for you to learn PHP programming.
How PHP extracts the value of an array containing a string from an array
$map = Array (' AA ' = = ' 3,4,5,6; ',
' BB ' = ' 3,4,6,7; ',
' CC ' = ' 5,8,1,3; ',
' dd ' = ' 1,5,7,9; '
);
foreach ($map as $k = = $v) {
if (Strpos ($v, ' 3,4 ')!== false) {
echo "' $k ' = ' $v '
";
}
}
Creating an array with strings in PHP
First, the string is processed, as a PHP definition array, and then with eval:
$str = "
Array
(
[+] and array
(
[id] = > 2304
[fromtype] + item
)
[+] = Array
(
[id] = 2313
[fromtype] + item
) [+] = Array
(
[id] = 4265
[fromtype] + item
)
) ";
$str =preg_replace ('/\[([a-z]+) \]\s*=>\s* ([0-9a-z]+]/', "' \$1 ' = ' \$2 ',", $str);
$p =array (' array ', ' [', '] ', ') ');
$to =array (' array ', "'", "'", '), ');
$str =str_replace ($p, $to, $STR);
//echo $str;
eval ("\ $arr =". $str. '; ');
Print_r ($arr [15]);
http://www.bkjia.com/PHPjc/882911.html www.bkjia.com true http://www.bkjia.com/PHPjc/882911.html techarticle PHP Implementation based on the method of generating corresponding arrays of strings, PHP string corresponding to the array of examples of this article on the PHP implementation of the corresponding array based on the method, is a more practical technique. Points ...