在foreach中重新格式化数组

时间:2016-05-18 05:57:31

标签: php arrays json facebook-graph-api

我现在正在facebook的graph api上工作,他们给我一个json /数组响应。我需要做的是重新格式化json,因为我不习惯使用它们的格式。 这是我的样本json响应:

"ids": [
    {
      "id": "id1",
      "format": [
        {
          "filter": "130x130",
          "picture": "sample1.jpg",
        },
        {
          "filter": "130x130",
          "picture": "sample1.jpg",
        }
      ]
   },
      {
      "id": "id2",
      "format": [
        {
          "filter": "130x130",
          "picture": "sample2.jpg",
        },
        {
          "filter": "130x130",
          "picture": "sample2.jpg",
        }
      ]
     }
]

PHP代码:

$sampleArray = //let say this is the array sample above
$dataArray = array();
$dataArrayNested = array();
foreach($sampleArray['ids'] as $key => $value){
   $dataArrayNested[$key] = $value['id'];
   $dataArrayNested[$key] = $value['format'][0]['picture']
}
$dataArray['ids'] =  $dataArrayNested;

但是当我打印出结果时:

{
  "ids": [
    "id1",
    "sample1.jpg",
    "id2",
    "sample2.jpg"
  ]
}

这个结果并不是我想要的。它应该是这样的:

{
      "ids": [
        {
        "id": "id1",
        "format" : "sample1.jpg"
        },
        {
        "id": "id1",
        "format" : "sample1.jpg"
        }
      ]
    }

我知道我的代码有问题。

1 个答案:

答案 0 :(得分:1)

第4行中的PHP代码中,您引用的io.hpp/cpp数字不相关。

我认为你正在寻找的东西是:

    name: "xxxNet"
    layer {
        name: "xxx"
        type: "MultiTaskData"
        top: "data"
        top: "exp_label"
        data_param { 
            source: "expression_ld_train_leveldb"   
            batch_size: 60 
            task_num: 1
            label_dimension: 8
        }
        transform_param {
            scale: 0.00390625
            crop_size: 60
            mirror: true
        }
        include:{ phase: TRAIN }
    }
    layer { 
        name: "exp_prob" 
        type: "InnerProduct"
        bottom: "data"  
        top: "exp_prob" 
        param {
            lr_mult: 1
            decay_mult: 1
        }
        param {
            lr_mult: 2
            decay_mult: 0
        }
        inner_product_param {
            num_output: 8
            weight_filler {
            type: "xavier"
            }    
        bias_filler {      
            type: "constant"
            }  
        }
    }
    layer {  
        name: "exp_loss"  
        type: "EuclideanLoss"  
        bottom: "exp_prob" 
        bottom: "exp_label"
        top: "exp_loss"
        include:{ phase: TRAIN }
    }