Table of Contents
Reply content:
Home Backend Development PHP Tutorial laravel - php variable parsing

laravel - php variable parsing

Aug 18, 2016 am 09:15 AM
laravel php thinkphp yii

For example, now I have the variable $arr, which is an array

<code>$arr = [
    'news' =&gt; [
        'data' =&gt; [
            0 =&gt; [
                'title' =&gt; '名字',
                'content' =&gt; '内容'
            ],
        ],
    ],
];
</code>
Copy after login
Copy after login

Some frameworks or template engines have parsing functions. The value of title can be obtained through arr.news.data[0].title, and the value can be modified.

Then I want to know what its principle is, and how to use this expression efficiently, safely and simply to get and set the values ​​in the array?

What I can think of is to use text processing, but it should not be very safe and efficient. Ask the teacher for guidance.

Reply content:

For example, now I have the variable $arr, which is an array

<code>$arr = [
    'news' =&gt; [
        'data' =&gt; [
            0 =&gt; [
                'title' =&gt; '名字',
                'content' =&gt; '内容'
            ],
        ],
    ],
];
</code>
Copy after login
Copy after login

Some frameworks or template engines have parsing functions. The value of title can be obtained through arr.news.data[0].title, and the value can be modified.

Then I want to know what its principle is, and how to use this expression efficiently, safely and simply to get and set the values ​​in the array?

What I can think of is to use text processing, but it should not be very safe and efficient. Ask the teacher for guidance.

Most template engines are processed in a pre-compiled manner, that is, the input template data will be converted into standard PHP statements such as variables, loops, conditions and other symbols, and then executed.

In addition, these frameworks or template engines are all open source. If you have time to ask people here, you will already understand if you look at the code yourself.

Let me find you a code.

<code> public static function getValue($array, $key, $default = null)
    {
        if ($key instanceof \Closure) {
            return $key($array, $default);
        }

        if (is_array($key)) {
            $lastKey = array_pop($key);
            foreach ($key as $keyPart) {
                $array = static::getValue($array, $keyPart);
            }
            $key = $lastKey;
        }

        if (is_array($array) &amp;&amp; (isset($array[$key]) || array_key_exists($key, $array)) ) {
            return $array[$key];
        }

        if (($pos = strrpos($key, '.')) !== false) {
            $array = static::getValue($array, substr($key, 0, $pos), $default);
            $key = substr($key, $pos + 1);
        }

        if (is_object($array)) {
            // this is expected to fail if the property does not exist, or __get() is not implemented
            // it is not reliably possible to check whether a property is accessable beforehand
            return $array-&gt;$key;
        } elseif (is_array($array)) {
            return (isset($array[$key]) || array_key_exists($key, $array)) ? $array[$key] : $default;
        } else {
            return $default;
        }
    }</code>
Copy after login
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles