1 pièce(s) jointe(s)
JSONPATH ou recherche recursive
J’ai un fichier JSON que je veux manipuler avec PHP il retourne un tableau multi dimension voici le code
Code:
1 2 3
| $jsonString = file_get_contents('tree.json');
$data = json_decode($jsonString, true); |
Quand je fais un dump sur $data j'obtiens cette résultat
Pièce jointe 241562
Je cherche une manière pour rechercher un élément par sa valeur j'ai trouvé une solution pour faire une recherche par attribut on utilisant Library JSONPATH mais je veux filtrer par valeur chose qui ne fonctionne pas j'ai testé 2 solution qui ne fonctionne pas
1) Utilisation de Jsonpath voici le code
Code:
1 2
| $o = $parser->decode($json);
jsonPath($o, "$..[?(@.text == MA1 Processus Pilotage)]"); |
2) Fonction recursive
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| function srch($array,$target){
$path= "";
foreach ($array as $key => $value) {
if(is_array($value)){
$path = srch($value,$target);
if($path != ""){
$path = "[".$key."]".$path;
}
}else{
if($value == $target){
$path = "[".$key."]";
}
}
}
return $path; |
merci de m'aider