After converting PHP array to object, how to access data using object?
给我你的怀抱
给我你的怀抱 2017-05-16 13:15:05
0
3
473
    $fields = M('fields') -> select();
    $aa = json_encode($fields);
    $bb = json_decode($aa);
    
    $bb -> 0;//获取不到任何数据,应该怎么写呢
    
    print_r($bb);
    结果为:

Array
(

[0] => stdClass Object
    (
        [fields_id] => 1
        [name] => 勘探
    )

[1] => stdClass Object
    (
        [fields_id] => 2
        [name] => 钻完井
    )

[2] => stdClass Object
    (
        [fields_id] => 3
        [name] => 采油采气
    )

[3] => stdClass Object
    (
        [fields_id] => 4
        [name] => 油气储运
    )

[4] => stdClass Object
    (
        [fields_id] => 5
        [name] => 地面工程
    )

[5] => stdClass Object
    (
        [fields_id] => 6
        [name] => 销售炼化
    )

[6] => stdClass Object
    (
        [fields_id] => 7
        [name] => 新能源
    )

[7] => stdClass Object
    (
        [fields_id] => 8
        [name] => 综合
    )

)

How to get the data inside? Ask for a way to write. For example, how to get all the data under [0]? What if we take [name] below [0]
?

给我你的怀抱
给我你的怀抱

reply all(3)
phpcn_u1582

There is a slight problem upstairs, let me do this: $bb[0]->

You may want to use it like this: json_decode($aa, true);

Please check the documentation when programming:

API

json_decode

(PHP 5 >= 5.2.0, PECL json >= 1.2.0, PHP 7)

json_decode — Decode JSON formatted strings

Instructions

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
Accepts a JSON encoded string and converts it to a PHP variable

Parameters

json
The string in json string format to be decoded.

This function can only handle UTF-8 encoded data.

Note:

PHP implements a superset of JSON as specified in the original » RFC 7159.

assoc
When this parameter is TRUE, an array will be returned instead of an object.

漂亮男人
$bb ->[0]['name'];一样,跟普通数组类似,就是多了->
洪涛

$bb[0]->name

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template