复制json编码的关联数组?

时间:2010-12-20 17:59:39

标签: php json

我有一个json字符串

{
   "to": {
      "data": [
         {
            "name": "f8",
            "id": "11204705797"
         },
         {
            "name": "f9",
            "id": "11205705797"
         }
      ]
   }
}

我需要在php中复制它,我所拥有的是一个名字和类似的数组

Array
(
    [friends] => Array
        (
            [0] => 939394934-Steve
            [1] => 939394934-Steve
            [2] => 939394934-Steve
            [3] => 939394934-Steve
        )

)

我会爆炸 - 获取id-name

我该怎么做?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你需要这样的东西:

$data = array();
foreach ($array['friends'] as $friend) {
    list($id, $name) = explode('-', $friend, 2);
    $data[] = array(
        'name' => $name,
        'id' => $id,
    );
}

$to = new stdClass; // 'to' appears to be an object... because of {}
$to->data = $data;
echo json_encode(array('to'=>$to));