Home  >  Article  >  Backend Development  >  How to generate corresponding array based on string through php

How to generate corresponding array based on string through php

jacklove
jackloveOriginal
2018-06-09 11:37:181788browse

php generates the corresponding array method based on the string

For example:

<?php
$config = array(
    &#39;project|page|index&#39; => &#39;content&#39;,
    &#39;project|page|nav&#39; => array(
            array(
                &#39;image&#39; => &#39;1.jpg&#39;,
                &#39;name&#39; => &#39;home&#39;
            ),
            array(
                &#39;image&#39; => &#39;2.jpg&#39;,
                &#39;name&#39; => &#39;about&#39;
            )
    ),
    &#39;project|page|open&#39; => true
);
?>


Generate the following array according to $config

<?php
$result = array(
    &#39;project&#39; => array(
        &#39;page&#39; => array(
            &#39;index&#39; => &#39;content&#39;,
            &#39;nav&#39; => array(
                    array(
                        &#39;image&#39; => &#39;1.jpg&#39;,
                        &#39;name&#39; => &#39;home&#39;
                    ),
                    array(
                        &#39;image&#39; => &#39;2.jpg&#39;,
                        &#39;name&#39; => &#39;about&#39;
                    )
            ),
            &#39;open&#39; => true
        )    
    )
);
?>


Method: Use eval to implement

<?php
$config = array(
    &#39;project|page|index&#39; => &#39;content&#39;,
    &#39;project|page|nav&#39; => array(
            array(
                &#39;image&#39; => &#39;1.jpg&#39;,
                &#39;name&#39; => &#39;home&#39;
            ),
            array(
                &#39;image&#39; => &#39;2.jpg&#39;,
                &#39;name&#39; => &#39;about&#39;
            )
    ),
    &#39;project|page|open&#39; => true
);
$result = array();
foreach($config as $key=>$val){
    
    $tmp = &#39;&#39;;
    $keys = explode(&#39;|&#39;, $key);
    
    for($i=0,$len=count($keys); $i<$len; $i++){
        $tmp .= "[&#39;".$keys[$i]."&#39;]";
    }
    
    if(is_array($val)){
        eval(&#39;$result&#39;.$tmp.&#39;=&#39;.var_export($val,true).&#39;;&#39;);
    }elseif(is_string($val)){
        eval(&#39;$result&#39;.$tmp.&#39;=&#39;.$val.&#39;;&#39;);
    }else{
        eval(&#39;$result&#39;.$tmp.&#39;=$val;&#39;);
    }
}
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
                )
        )
)

This article explains how to generate corresponding arrays based on strings through PHP. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

Explanation on the solution to the lack of double quotes in the JSON string key

How Obtain https request method through curl

Explanation on uploading pictures and saving them to the database through php


The above is the detailed content of How to generate corresponding array based on string through php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn