How PHP takes a single value out of an array

Source: Internet
Author: User

This article mainly share with you the method of PHP to take out an array of single values, mainly in the form of code and we share, hope to help everyone.

PHP takes out an array of single values
1. Array arr
The Var_dump (ARR) values are as follows:

Array (size=3)  ' delete ' = =     Array (size=3)      0 = String ' hbsflyrecode20170222-101501.txt ' (length=31)      1 = String ' Hbsflyrecode20170222-105502.txt ' (length=31)      2 = String ' Hbsflyrecode20170222-108803.txt ' (length=31)  ' new ' = =     Array (size=3)      0 = String ' hbsflyrecode20170223-101504.txt ' (length=31)      1 = String ' Hbsflyrecode20170223-105505.txt ' (length=31)      2 = String ' Hbsflyrecode20170223-108806.txt ' (length=31)  ' old ' = =     Array (size=3)      0 = String ' hbsflyrecode20170221-101507.txt ' (length=31) c13/>1 = String ' Hbsflyrecode20170221-105508.txt ' (length=31)      2 = String ' Hbsflyrecode20170221-108809.txt ' (length=31)
echo $arr [' old '][0]; print out: Hbsflyrecode20170221-101507.txt

However, if Arr is in the form of an object, the print results are as follows:
Var_dump (arr)

Object (StdClass) [1] public  ' delete ' = =     Array (size=3)      0 = String ' hbsflyrecode20170222-101501.txt ' (length=31)      1 = String ' Hbsflyrecode20170222-105502.txt ' (length=31)      2 = String ' Hbsflyrecode20170222-108803.txt ' (length=31) public  ' new ' =     Array (size=3)      0 = String ' Hbsflyrecode20170223-101504.txt ' (length=31)      1 = String ' Hbsflyrecode20170223-105505.txt ' (length=31)      2 = = String ' Hbsflyrecode20170223-108806.txt ' (length=31) public  ' old ' =     Array (size=3)      0 = String ' Hbsflyrecode20170221-101507.txt ' (length=31)      1 = String ' Hbsflyrecode20170221-105508.txt ' (length=      2 = String ' Hbsflyrecode20170221-108809.txt ' (length=31)

You cannot use $arr [' old '][0] to take a value, you can use ARR objects and arrays of common foreach methods:

function GetValue ($arr) {    foreach ($arr as $key = = $value) {        if (Is_array ($value)) {            getValue ($value);        } else{            echo $value. " <br> ";}}    }

If Arr is in the form of an object, you can test the object into an array form, which provides a quick way to:

1. $object _json = Json_encode ($arr), the object   $json = Json_encode ($arr, True), and the resulting pure json2. Json_decode ($object _json) and Json_decode ($json) Gets the array object    Json_decode ($object _json,true) and Json_decode ($json, true) to get an array of arrays, the way the array objects can be converted to arrays:

Arr,true), true);

This problem is found in the project, it is recommended that when you convert JSON and array in PHP, the second parameter of Json_encode () and Json_decode () is added true, namely:

Json_encode (

Json,true);
"'

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.