Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将php数组转换为更小的json?_Php_Arrays_Json - Fatal编程技术网

将php数组转换为更小的json?

将php数组转换为更小的json?,php,arrays,json,Php,Arrays,Json,我正在从一个Web服务(soap xml)接收一个PHP数组。 从这个数组中,我只想保留几个标记,然后将其转换为json(请参见底部的示例) 请参阅我从webservice收到的阵列示例,如下所示: (***标记我在最终Json中需要的信息) 我希望最终裁剪的json如下所示: { "Id":445946, "Name": "Movie: Film", "Image":{ "Url":"http://images.pix.se",

我正在从一个Web服务(soap xml)接收一个PHP数组。 从这个数组中,我只想保留几个标记,然后将其转换为json(请参见底部的示例)

请参阅我从webservice收到的阵列示例,如下所示:

(***标记我在最终Json中需要的信息)

我希望最终裁剪的json如下所示:

  {  
      "Id":445946,
      "Name": "Movie: Film",
      "Image":{
         "Url":"http://images.pix.se",
         "Width":595,
         "Height":709
      },
      "MovieUrl": "http://www.Movie.se",
      "LongText": "Text text info tinfo text Long text",
      "Price": "70 SEK",
      "ShortText": "Short info text text text",
      "Phone": "0410-1111111",
      "StartDate": 2014-11-12T00:00:00,
      "EndDate": 2014-11-13T00:00:00,
      "StartTime": 0001-01-01T19:00:00,
      "ValidDays": "Wednesday Thursday",
      "ArenaId": 75316,
      "ArenaName": "Moviegrafteatern Movie, Staden"
      }

如何将此数组转换为更小的JSON?

只需创建一个要显示为白名单的数组键列表,然后删除其余的

假设您的对象是$object

//this line must come before any output is made. (echo, print, etc.)
header('Content-Type: application/json');

//whitelist. the items you want to keep
$keep = array("id", "name", "image" ...);

foreach($object as $object) {

    foreach ($object as $key => $product) {

        if( !in_array($key, $keep) ) { //check if the current loop key is not in the whitelist
            unset($object[$key]);
        }

    }

}



//Then convert the array to JSon.

echo json_encode($object)

然后$object将只保存白名单中的内容

我认为解决这个问题的唯一方法是对函数中需要的项进行exakt映射,然后循环遍历不同的对象,因为使用这些不同的对象得到的结构非常糟糕

function cleanUpResult(array)
{
    $r = array();
    foreach($item in $array){
     $result = array(); 

      $result["Id"] = $item["Id"];
      $result["Name"] = $item["Name"];
      $result["Image"] = array();
      $result["Image"]["Url"] =$item["Image"]->Url;
      $result["Image"]["Width"] =$item["Id"]->Width;
      $result["Image"]["Height"] =$item["Id"]->Height;
      $result["MovieUrl"] = $item["Attributes"]->AttributeData[0]->Value->Data;
      $result["LongText"] = $item["Attributes"]->AttributeData[1]->Value->Data;
      $result["Price"] = $item["Attributes"]->AttributeData[2]->Value->Data;;
      $result["ShortText"] = $item["Attributes"]->AttributeData[4]->Value->Data;
      $result["Phone"] = $item["Attributes"]->AttributeData[6]->Value->Data;
      $result["StartDate"] = $item["StartDate"];
      $result["EndDate"] = $item["EndDate"];
      $result["StartTime"] = $item["StartTime"];
      $result["ValidDays"] = $item["ValidDays"];
      $result["ArenaId"] = $item["ArenaId"];
      $result["ArenaName"] = $item["ArenaName"];
      $r[] = $result;
    }
    return json_encode($r);
}

这并不复杂,但由于您需要嵌套数组中的各种字段,最简单的方法就是迭代数组并获取要编码的各个字段。我不确定是否值得花时间构建一个智能过滤器

例如,假设
$soapArray
包含您在上面提供的信息:

$data = array();

// iterate over the full field list
foreach ($soapArray as $soapElement) {

    // construct a partial array containing the fields you want
    $data[] = array(
        'Id' => $soapElement['Id'],
        'Name' => $soapElement['Name'],
        'Image' => array(
            'Url' => $soapElement['Image']->Url,
            'Width' => $soapElement['Image']->Width,
            'Height' => $soapElement['Image']->Height,
        ),
        'MovieUrl' => $soapElement['Attributes']->AttributeData[0]->Value->Data,
        'LongText' => $soapElement['Attributes']->AttributeData[1]->Value->Data,
    );
}

// display the encoded array
echo json_encode($data);

分享你迄今为止尝试过的代码,你得到了什么结果。。。很少有人愿意从scratchWell为您编写代码。据我所知,更大的问题是您有太多的信息。您可以控制在原始数组中返回的内容吗?我想您应该在带有对象的数组上尝试json_encode函数。那是错误的。如果你是这样做的,那么看看OP有一个对象数组,AFAIU。请参见
[0]=>stdClass对象
@mudasobwa为多维更新的
foreach($key=>$product对象)
将失败,因为
$Object
在此处不是数组。看看输入数据。
$data = array();

// iterate over the full field list
foreach ($soapArray as $soapElement) {

    // construct a partial array containing the fields you want
    $data[] = array(
        'Id' => $soapElement['Id'],
        'Name' => $soapElement['Name'],
        'Image' => array(
            'Url' => $soapElement['Image']->Url,
            'Width' => $soapElement['Image']->Width,
            'Height' => $soapElement['Image']->Height,
        ),
        'MovieUrl' => $soapElement['Attributes']->AttributeData[0]->Value->Data,
        'LongText' => $soapElement['Attributes']->AttributeData[1]->Value->Data,
    );
}

// display the encoded array
echo json_encode($data);