This time I will show you how to retrieve 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. 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
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)
foreach method common to arr objects and arrays. 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) What you get with json_decode($json) is an array objectjson_decode($object_json,true) What you get with json_decode($json,true) is an arrayTo sum up , how to convert an array object into an array: 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 php retaining key values + merging arrays
How to remove duplicate values in a two-dimensional array
How to distinguish associative arrays from index arrays
The above is the detailed content of How to remove a single value from an array in php. For more information, please follow other related articles on the PHP Chinese website!