How do I get a data with condition in json? [closed]

0

Good!,

Several time ago I am looking for a solution to my problem, I will give you an example:

I have the following JSON file:

{
"datos": [
        {
            "user_r": "22543341",
            "user_n": "15678798"
        },
        {
            "user_r": "06183254",
            "user_n": "15674578"
        },
        {
            "user_r": "04088869",
            "user_n": "15674567"
        },
        {
            "user_r": "01215181",
            "user_n": "15679876"
        },
        {
            "user_r": "01311283",
            "user_n": "15674557"
        },
        {
            "user_r": "05302433",
            "user_n": "15673465"
        },
        {
            "user_r": "04485424",
            "user_n": "15670756"
        },
        {
            "user_r": "04088872",
            "user_n": "15677653"
        },
        {
            "user_r": "00892195",
            "user_n": "15675623"
        }
    ]
}

And I need to get the value of user_n where user_r is the one that I specify, if they did not understand me, what better to do it in SQL: SELECT user_n WHERE user_r = 22543341 .

I await your response, thank you! : D

    
asked by Alejandro Montoya 24.10.2016 в 23:47
source

1 answer

1

One solution would be to use json_decode to convert to Json the String given and then go through for until the value of user_r whatever you want.

<?php 

$values = '{
   "datos": [
    {
        "user_r": "22543341",
        "user_n": "15678798"
    },
    {
        "user_r": "06183254",
        "user_n": "15674578"
    }

  ]
 }';
  $json = json_decode($values);
  foreach($json->datos as $item)
  {
    if($item->user_r  == "22543341")
    {
     echo $item->user_n;
    }
 }

?>
    
answered by 24.10.2016 / 23:55
source