How PHP extracts a single value from an array

Source: Internet
Author: User
This time for you to bring PHP how to take out a single value in the array, PHP takes the individual values in the array of considerations, the following is the actual case, together to see.

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)  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=31)  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); Get the object

$json = Json_encode ($arr, true); get pure JSON

2. Json_decode ($object _json) and Json_decode ($json) get the Array object

Json_decode ($object _json,true) and Json_decode ($json, true) get the array

In summary, you can convert an array object to an array:

Arr=jsondecode (Jsonencode (Arr=jsondecode (Jsonencode (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 (arr,true); Jsondecode (arr,true); Jsondecode (json,true);

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP reserved Key value + Merge array

How to exclude duplicate values in a two-dimensional array

Methods for distinguishing associative arrays from indexed arrays

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.