简体   繁体   English

PHP json string:获取关联数组对象

[英]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): 我有一个像这样的数据的json文件(这是显示结构的文件的部分视图):

{
"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. 我想要做的是搜索它以在ZOLD字段= $ OLD中找到一个值,然后返回相应的ZNEW数组的值。 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. 我尝试过使用foreach并且可以让阵列回响,但我不确定它的价值。 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 (ie "Alaska") but I may want to return this information as well based on the found value. 附加信息:我可能知道也可能不知道“州”字符串(即“阿拉斯加”),但我可能希望根据找到的值返回此信息。

Thanks for the help. 谢谢您的帮助。

Instead of trying to loop through ZOLD, you could use array_search (assuming that the $OLD value can only appear once in the data). 您可以使用array_search (假设$OLD值只能在数据中出现一次),而不是尝试遍历ZOLD。 This function will either return a number for the index of the value you search for or false if it cannot find it: 此函数将为您搜索的值的索引返回一个数字,如果找不到它,则返回false

$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. 这将替换你的两个最里面的for循环(因为你需要其他循环来达到这个级别)并迭代每个状态。 At this point as well, $key would be defined to your state name. 此时, $key将被定义为您的州名。

Here is another way to complete your task, this one with array_map function: 这是另一种完成任务的方法,这个方法有array_map函数:

$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 如果找到相应的ZNEW值, $result dump将包含带有“state”和“ZNEW”键的列表或assoc数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM