Home >Backend Development >PHP Tutorial >How to remove a single value from an array in php
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.txtBut if arr is a
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. 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);project. It is recommended that you use json_encode() and json_decode() when converting json and array in php. Add true to the second parameter, 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 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!