Comment obtenir une seule valeur d'un tableau en php

小云云
Libérer: 2023-03-22 11:54:02
original
2066 Les gens l'ont consulté

Cet article partage principalement avec vous la méthode d'extraction d'une valeur unique d'un tableau en PHP. Il est principalement partagé avec vous sous forme de code.

php retire une seule valeur du tableau
1. La valeur du tableau arr
var_dump(arr) est la suivante :

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)
Copier après la connexion
echo $arr['old'][0];
打印出: HBSFlyRecode20170221-101507.txt
Copier après la connexion

Mais si arr est sous forme d'objet, le résultat de l'impression est le suivant :
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)
Copier après la connexion

ne peut pas utiliser $arr['old'][0] pour obtenir la valeur. Vous pouvez utiliser le. méthode foreach commune aux objets et aux tableaux arr pour obtenir la valeur :

function getValue($arr){

    foreach($arr as $key => $value){        if(is_array($value)){
            getValue($value);
        }else{            echo $value."<br>";
        }

    }
}
Copier après la connexion

Si arr est sous forme d'objet, vous pouvez convertir l'objet sous forme de tableau :

1. $object_json = json_encode($arr);得到的是对象   $json = json_encode($arr,true);得到的是纯json2. json_decode($object_json) 和 json_decode($json)得到的是数组对象
    json_decode($object_json,true) 和 json_decode($json,true)得到的是数组

综上 , 可以将数组对象转为数组的方式:
Copier après la connexion

arr,true),true);

项目中发现此问题 , 建议大家在php中将json和array转换时 , json_encode() 和 json_decode()的第二个参数要加 true , 即:
Copier après la connexion

json_encode(

json,true);
"`

Recommandations associées :

Comment supprimer une seule valeur d'un tableau en php

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!