0

i have a json array

[  
  {  
     "id":1.25,
     "name":"rose",
     "Number":15,
     "SSN":[  
         12345,
         3455
     ]
  },

  {  
     "id":1.15,
     "name":"orchid",
     "Number":7,
     "SSN":[  
        12345,
        3455
     ]
  }
]

i want to get the id value if user searched with any ssn or name or by number.the input for search may not be single value that means it may be combination of ssn and name or it may be name,ssn and number .Any combination of name number ,ssn may be inputs .i have to get id value if the inputs matches using php .i used xpath for xml but i want to search fron json.can someone suggest me with any idea..thank you...

5
  • So what you've tried so far? Commented Jun 25, 2016 at 9:48
  • suppose it's xml. Write xpath to select value you need with properly conditions Commented Jun 25, 2016 at 9:48
  • $nams = $xml->xpath ( '/users/ReportToken[name = "rose" && Number = 15]' ); i have tried with xpath for xml instead of json array.it worked well but i want to et search from json array Commented Jun 25, 2016 at 9:55
  • Do the input params (name,ssn and number) come via POST? Commented Jun 25, 2016 at 9:59
  • actually i want to use it in some api call but not in website with post method Commented Jun 25, 2016 at 10:10

3 Answers 3

1

To search user ID by analyzing JSON string use the following approach with array_filter and array_intersect_assoc functions:

$json_str = '[ { "id":1.25, "name":"rose", "Number":15, "SSN":[ 12345, 3455 ] }, { "id":1.15, "name":"orchid", "Number":7, "SSN":[ 12345, 3455 ] }]';
$decoded = json_decode($json_str, true);

// base search structure
$search_structure = ['name' => "", 'Number' => 0];

// let's say, the following inputs have come from API (some of them may be empty or omitted)
$number = 7;
$ssn = "3455";

if ($number && is_numeric($number)) $search_structure['Number'] = $number;
$search_structure = array_filter($search_structure); // filtering out empty params
$count_params = count($search_structure);
$user_id = 0;

foreach ($decoded as $item) {
    if ($count_params == count(array_intersect_assoc($search_structure, $item))) {
        if (!$ssn || ($ssn && in_array($ssn, $item['SSN']))) {
            $user_id = $item['id'];
            break;
        }
    }
}

print_r(($user_id)? "User id: $user_id" : "User with specified params not found!");

The output:

User id: 1.15
Sign up to request clarification or add additional context in comments.

7 Comments

thanks roman it worked..but if ssn given with input 12345 only it is not displaying id value. by the code we are geeting id value when ssn is given as 12345-3455.but i want the id when any of the value in ssn array given.thanks in advance
well, in reality, SSN is usually not a simple number as 12345, and contains hyphens. Is this for real system or just for the test?
it is for real system for api calling
so, I suppose, the exact search should consider the whole SSN number, but not part of it. Otherwise, the search would not be accurate, cause some users can have same name and same part of SSN number
Actually a user may have one or more ssn which are represented as array of ssn numbers .Any of the ssn input by user should return the id .
|
0

Lets $json contains an array string

$ar = json_decode($json, true);
$id = false;
foreach ($ar as $i) 
   if ($i["name"] == "rose" and  $i["Number"] == 15)
      { $id = $i["id"]; break; }
if ($id !== false) { 
   // use $id
}

Comments

0

Preferably, convert the json data to native PHP Object and then using foreach, loop over the object and extracting the ID of the Objects that match the given SSN Number. The result of the extraction would be assigned to an array. Below is a Commented procedure of 1 possible way of going about this:

    <?php

        // WE ASSUME, JUST FOR TESTING PURPOSES THAT THE SSN NUMBER WE ARE LOOKING FOR IS 4567
        $ssnNumber  = 4567; //3455;

        // CREATE A NEW ARRAY VARIABLE TO HOLD THE IDs YOU DESIRE & INITIALIZE IT TO AN EMPTY ARRAY
        $arrIDS     = array();
        $jsonData   = '[
              {
                 "id"       :1.25,
                 "name"     :"rose",
                 "Number"   :15,
                 "SSN"      :[
                        12345,
                        3455
                 ]
              },

              {
                 "id"       :1.15,
                 "name"     :"orchid",
                 "Number"   :7,
                 "SSN"      :[
                        12345,
                        3455
                 ]
              },

              {
                 "id"       :1.75,
                 "name"     :"orchid",
                 "Number"   :79,
                 "SSN"      :[
                        4567,
                        89012
                 ]
              }
            ]';
        // CONVERT $jsonData TO NATIVE PHP OBJECT
        $objJson    = json_decode($jsonData);

        // LOOP THROUGH THE PHP OBJECT & TEST IF A GIVEN SSN EXIST.
        foreach($objJson as $key=>$data){
            // CHECK IF THE $data->SSN IS SET:
            if(isset($data->SSN) && is_array($data->SSN)){
                // CHECK IF THE SSN ARRAY CONTAINS THE GIVEN SSN NUMBER.
                if(in_array($ssnNumber, $data->SSN)){
                    $arrIDS[]   = $data->id;
                }
            }
        }
        var_dump($arrIDS);  // DISPLAY: array (size=1) 0 => float 1.75

Alternatively, one could as well encapsulate the above Routine withina Function for re-usability like so:

    <?php    
        function getIDBySSN($jsonData, $ssnNumber){
            $arrIDs     = array();
            $objJson    = json_decode($jsonData);
            // LOOP THROUGH THE PHP OBJECT & TEST IF A GIVEN SSN EXIST.
            foreach($objJson as $key=>$data){
                // CHECK IF THE $data->SSN IS SET:
                if(isset($data->SSN) && is_array($data->SSN)){
                    // CHECK IF THE SSN ARRAY CONTAINS THE GIVEN SSN NUMBER.
                    if(in_array($ssnNumber, $data->SSN)){
                        $arrIDs[]   = $data->id;
                    }
                }
            }
            return $arrIDs;
        }

        var_dump(getIDBySSN($jsonData, $ssnNumber)); // PRODUCES THE SAME RESULT

Test it HERE:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.