PHP数组遍历有序键后缀

时间:2011-10-07 22:15:38

标签: php json

我必须在PHP中解析这个JSON,以便对于每个地址,我获取地址,行和城市信息并将其存储在数据库中。我计划实施的方式是这样的:

For each key in the json string, check if it begins with address,
If yes, split the string based on '_' and get the index count. 
Get line1, line2, city for this index count. 

有更好的方法吗?

(注意索引计数可以是随机的)

{
  "route": "A",

  "address_0": "A0",
  "line1_0": "L1_0",
  "line2_0": "L2_0",
  "city_0": "city_0",

  "address_1": "A1",
  "line1_1": "L1_1",
  "line2_1": "L2_1",
  "city_1": "city_1",

  "address_2": "A2",
  "line1_2": "L1_2",
  "line2_2": "L2_2",
  "city_2": "city_2",

}

4 个答案:

答案 0 :(得分:0)

是。请改用此格式

{
  "route": "A",
  data : [{
      "address": "A0",
      "line1": "L1_0",
      "line2": "L2_0",
      "city": "city_0"
  },{
      "address": "A1",
      "line1": "L1_1",
      "line2": "L2_1",
      "city": "city_1"
  },{
      "address": "A2",
      "line1": "L1_2",
      "line2": "L2_2",
      "city": "city_2"
  }]
}

并且像这样得到它

$json = '    {
      "route": "A",
      data : [{
          "address": "A0",
          "line1": "L1_0",
          "line2": "L2_0",
          "city": "city_0"
      },{
          "address": "A1",
          "line1": "L1_1",
          "line2": "L2_1",
          "city": "city_1"
      },{
          "address": "A2",
          "line1": "L1_2",
          "line2": "L2_2",
          "city": "city_2"
      }]
    }';
$object = json_decode($json);
echo $object->data[0]->address; // A0
echo $object->data[1]->address; // A1
//and so on...

答案 1 :(得分:0)

这是一个棘手的问题,因为即使是JSON也不是很好(address_0 vs address_1)而不是

{
    "route" :
        { "address" : "something", "line1": "something else" },
        { ... }
}

我会做一个json_decode把你拥有的东西变成一个数组,然后适当地解析这些键并将值传递给一个DB对象(或循环并保存)。

答案 2 :(得分:0)

您可以随时执行此操作:

$json = {
  "route": "A",

  "address_0": "A0",
  "line1_0": "L1_0",
  "line2_0": "L2_0",
  "city_0": "city_0",

  "address_1": "A1",
  "line1_1": "L1_1",
  "line2_1": "L2_1",
  "city_1": "city_1",

  "address_2": "A2",
  "line1_2": "L1_2",
  "line2_2": "L2_2",
  "city_2": "city_2",

}

$object = json_decode($json);

$route = array();
$currentRoute = "";
$addressCounter = 0;

foreach($object as $key => $value)
{
    if($key == "route"){
        $currentRoute = $value;
    } else {
        $explode = explode("_",$key);
        $route[$currentRoute][$addressCounter][$explode[0]] = $value;
        if($explode[0] == "city"){
            $addressCounter++;
        }
    }
}

print_r($route) 

// Should return something like
// Array (
//    ['A'] => Array (
//                [0] => Array (
//                        ['address'] => 'A0',
//                        ['line1'] => 'L1_0'
// ...

这很讨厌,但它确实有效。再说一次......如果你能控制原始表格,我会重新考虑这种方法,以便在这里更好地处理。

祝你好运!

答案 3 :(得分:0)

只需迭代数据并分配给数组:

$route = array();
foreach(json_decode($json) as $p => $v)
{
   list($k, $i) = explode('_', $p, 2) + array(NULL, NULL);
   if (NULL === $i) {
       $rc =& $route["$k $v"];
       continue;
   }
   $rc[$i][$k] = $v;
}
unset($rc);

给出:

array(1) {
  ["route A"]=>
  array(3) {
    [0]=>
    array(4) {
      ["address"]=>
      string(2) "A0"
      ["line1"]=>
      string(4) "L1_0"
      ["line2"]=>
      string(4) "L2_0"
      ["city"]=>
      string(6) "city_0"
    }
    [1]=>
    array(4) {
      ["address"]=>
      string(2) "A1"
      ["line1"]=>
      string(4) "L1_1"
      ["line2"]=>
      string(4) "L2_1"
      ["city"]=>
      string(6) "city_1"
    }
    [2]=>
    array(4) {
      ["address"]=>
      string(2) "A2"
      ["line1"]=>
      string(4) "L1_2"
      ["line2"]=>
      string(4) "L2_2"
      ["city"]=>
      string(6) "city_2"
    }
  }
}

Demo