Maison  >  Article  >  développement back-end  >  Comment obtenir une seule valeur d'un tableau en php

Comment obtenir une seule valeur d'un tableau en php

小云云
小云云original
2018-03-27 10:56:452068parcourir

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

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)

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>";
        }

    }
}

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)得到的是数组

综上 , 可以将数组对象转为数组的方式:

arr,true),true);

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

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!

Déclaration:
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