Home  >  Article  >  Backend Development  >  How to get a single value from an array in php

How to get a single value from an array in php

小云云
小云云Original
2018-03-27 10:56:452160browse

This article mainly shares with you the method of extracting a single value from an array in PHP. It is mainly shared with you in the form of code. I hope it can help you.

php takes out a single value from the array
1. Array arr
var_dump(arr) The value is 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];
打印出: HBSFlyRecode20170221-101507.txt

But if arr is in object form, the print result is 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 get the value. You can use the foreach method common to arr objects and arrays to get the value:

function getValue($arr){

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

    }
}

If arr is Object form, you can convert the object into array form. Here is a shortcut:

1. $object_json = json_encode($arr);得到的是对象   $json = json_encode($arr,true);得到的是纯json2. json_decode($object_json) 和 json_decode($json)得到的是数组对象
    json_decode($object_json,true) 和 json_decode($json,true)得到的是数组

综上 , 可以将数组对象转为数组的方式:

arr,true), true);

项目中发现此问题 , 建议大家在php中将json和array转换时 , json_encode() 和 json_decode()的第二个参数要加 true , 即:

json_encode(

##json,true);

“`

Related recommendations:

How to remove a single value from an array in php

The above is the detailed content of How to get a single value from an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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