PHP method to extract a single value from an array

php中世界最好的语言
Release: 2023-03-26 14:36:01
Original
1413 people have browsed it

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)
Copy after login
echo $arr['old'][0];
打印出: HBSFlyRecode20170221-101507.txt
Copy after login
But 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)
Copy after login
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>";
  }
 }
}
Copy after login

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 array

To 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);
Copy after login
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);
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!