This time I will bring you the analysis of the method of retrieving a single value from an array in PHP. What are theprecautions 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.txt
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)
foreachmethod 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."
"; } } }
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 json 2. json_decode($object_json) and json_decode($json) What is obtained is an array object json_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);
json_encode(arr,true);jsondecode(arr,true);jsondecode(json,true);
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!