Home >Backend Development >PHP Tutorial >PHP method to extract a single value from an array
This time I will bring you the analysis of the method of retrieving a single value from an array in PHP. What are the precautions for retrieving a single value from an array in PHP? The following is a practical case, let's take a look.
1. The value of array arr
var_dump(arr) 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.txtBut if arr is in the form of
object, print The 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 in object form, you can convert the object into array form. Here is a shortcut:
1. $object_json = json_encode ($arr); What you get is an object$json = json_encode($arr,true); What you get is pure json2. json_decode($object_json) and json_decode($json) What is obtained is an array objectjson_decode($object_json,true) and json_decode($json,true) is obtained is an arrayTo sum up, the array object can be converted For arrays: This problem was found in the
arr=jsondecode(jsonencode(arr=jsondecode(jsonencode(arr,true),true);project. It is recommended that when converting json and array in php, the second parameter of json_encode() and json_decode() should be added. true , that is:
json_encode(arr,true);jsondecode(arr,true);jsondecode(json,true);I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website! Recommended reading:
Detailed explanation of the steps to implement routing and class automatic loading in PHP
php splits the operation string into an array
The above is the detailed content of PHP method to extract a single value from an array. For more information, please follow other related articles on the PHP Chinese website!