mudrock
mudrock

Reputation: 105

PHP json string: getting an associative array object

I have a json file with data like this (this is a partial view of the file to show structure):

{
"state": {
    "ALABAMA": {
        "ZOLD": [ "101", "102" ],
        "ZNEW": [ "11",  "12"  ]
    },
    "ALASKA": { 
        "ZOLD": [ "5001", "5002", "5003", "5004", "5005", "5006", "5007", "5008", "5009", "5010"],
        "ZNEW": [   "21",   "22",   "23",   "24",   "25",   "26",   "27",   "28",   "29",   "20"]
    }
}

}

What I want to do is search through it to find a value in the ZOLD field = $OLD, then return the value of the corresponding ZNEW array. I've tried going in with foreach and can get the arrays to echo out, but I'm not sure of what the value will be. Here's the code I have:

function translateZone($OLD)
{
$OLD = "5010";  //for testing purposes a constant but will be variable
$zStr  = file_get_contents('getnew.json');
$zJson = json_decode($zStr,true);
    foreach($zJson as $key=> $jsons)
    {
        foreach($jsons as $key=>$values)
        {
            foreach($values as $key=>$vals)
            {
                $counter=0;
                foreach($vals as $key=>$vls)
                {    
                    $counter ++;
                    echo var_dump($vls);//I can see the values,but now what?

                    if ($vls == $OLD)
                    {
                        $zTemp = Help here -some array value of counter??
                    }
                }
            }
            return $zTemp;
        }
}

I've searched through a bunch of other questions but haven't found something close enough to help with the problem.

Additional information: I may or may not know the "state" string (i.e. "Alaska") but I may want to return this information as well based on the found value.

Thanks for the help.

Upvotes: 1

Views: 831

Answers (2)

denied
denied

Reputation: 608

Here is another way to complete your task, this one with array_map function:

$jsonArray = json_decode($your_file_content, true);
$oldVal = "5005";
$result = [];


foreach( $jsonArray["state"] as $k => $v ) {

/**
 * Here we're building an array or pairs ZOLD => ZNEW values
 */
$pairs = array_map(function($o, $n) {
    return [ $o => $n ];
}, $v["ZOLD"],  $v["ZNEW"]);

/**
 * Filling up the result
 */
foreach( $pairs as $p )
    if( isset($p[$oldVal]) )
        $result[] = [
            "state" => $k,
            "ZNEW" => $p[$oldVal]
        ];
}

var_dump($result);

$result dump will contains a list or assoc arrays with "state" and "ZNEW" keys, if the corresponding ZNEW values will be found

Upvotes: 1

MasterOdin
MasterOdin

Reputation: 7896

Instead of trying to loop through ZOLD, you could use array_search (assuming that the $OLD value can only appear once in the data). This function will either return a number for the index of the value you search for or false if it cannot find it:

$index = array_search($OLD,$values['ZOLD']);
if ($index !== FALSE) {
    $zTemp = $values['ZNEW'][$index];
}

This would replace your two innermost for loop (as you need the other loops to get down to this level) and iterate through each state. At this point as well, $key would be defined to your state name.

Upvotes: 2

Related Questions