How to generate corresponding array based on string through php

jacklove
Release: 2023-03-31 08:28:01
Original
1852 people have browsed it

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
);
?>
Copy after login


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
        )    
    )
);
?>
Copy after login


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);
?>
Copy after login


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
                )
        )
)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template