PHP implements the method of generating corresponding array based on string, PHP string corresponding to array _php tutorial

Source: Internet
Author: User
Tags learn php learn php programming php definition

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 ...

  • 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.