2016-11-30 34 views
0

我在我的问题的底部有一个主PHP数组。它的工作原理与编码完全一致。我的问题局限于数组中显示的“内容”对象。对多维数组内存在子数组的条件变化

'content'对象应该包含最多10个子数组($ newSubArray0-9),每个子数组根据下面的这个简单循环填充图像数据(数据全部可从先前的数据库查询中获得 - 所以没有问题)。现在

//get data for the 'content' object sub arrays.... 
for($i = 0; $i<9; $i++) { 
$newSubArray.$i = 
[ 
'url' => $uploadspath.$media_url.$i,//main image 
"type" =>$type.$i,//media type 
"caption"=>$caption.$i 
]; 
} 

...主阵列在下面我只是需要能够排除或删除任何$ newSubArray的(0-9)情况下,如果相应的媒体条件下产生的空介质的结果,即( !media_url。$ i)或($ media_url。$ i =“”)。

我已经试过......

If (media-conditon) 
...{$newSubArray.$i=UNSET($newSubArray.$i);} 
...{$newSubArray.$i=array_filter($newSubArray.$i);} 
...{$newSubArray.$i=array_values($newSubArray.$i);} 

所有这些努力的影响,我想完全删除子数组元素(基于条件),但留下的子阵列本身如“无”的痕迹。该跟踪会导致后续数据验证失败。虚拟化正在通过json数据发送到的第三方API完成 - 所以我无法更改验证。我需要在那里没有任何关联$ media_url值的子数组没有记录。

以下是主阵列的一部分,其中包含需要有条件地包含或排除的子阵列元素,具体取决于关联图像的可用性。这一切工作正常...只需要解决上述问题。

$data = json_encode 
(
array 
    (

      "location" =>array 
      (
       "property_number_or_name" => $house_name_number, 
       "street_name" => $propaddress1, 
       "locality" => $propaddress2, 
       "town_or_city" => $town, 
       "postal_code" => $postcode, 
       "country_code" => "GB", 
       "coordinates"=>array 
            (

             "latitude"=>(float)$latitude, 
             "longitude"=>(float)$longitude 

            ) 
      ), 

      "pricing" =>array 
      (
      "rent_frequency" => $rent_frequency, 
      "currency_code" => "GBP", 
      "price" => (int)$price, 
      "transaction_type" => "rent" 
      ), 


      "detailed_description" =>array 
             (
             array 
              (
              "text" => $description 
              ) 
             ), 

      "content" => array 
         (
          $newSubArray0,//remove this sub array completely if no media_url0 
          $newSubArray1,//remove this sub array completely if no media_url1 
          $newSubArray2,//remove this sub array completely if no media_url2 
          $newSubArray3,//remove this sub array completely if no media_url3 
          $newSubArray4,//remove this sub array completely if no media_url4 
          $newSubArray5,//remove this sub array completely if no media_url5 
          $newSubArray6,//remove this sub array completely if no media_url6 
          $newSubArray7,//remove this sub array completely if no media_url7 
          $newSubArray8,//remove this sub array completely if no media_url8 
          $newSubArray9 //remove this sub array completely  if no media_url9        
         ), 

      "google_street_view"=>array 
          (
          "heading"=>(float)$pov_heading, 
          "pitch"=> (float)$pov_pitch, 
          "coordinates"=> array        
             (
             "latitude"=>(float)$pov_latitude, 
             "longitude"=>(float)$pov_longitude 
             ) 
          ), 


      "display_address"=> $display_address, 
      "summary_description"=> $summary 

,JSON_UNESCAPED_SLASHES); 

回答

0

你可能想unset但不存储在同一个地方调用的结果。

此致:

If (media-conditon) 
    ...{$newSubArray.$i=UNSET($newSubArray.$i);} 

更新:

If (media-conditon) 
    ...{UNSET($newSubArray.$i);} 

您还可以通过你的内容数据组的元素之前验证您的媒体条件下做到这一点更有效。

+0

嗨凯文。这次真是万分感谢。我很困惑为什么unset不起作用。请你给我一点解释你的新建议。 [很高兴首先设置内容数组]。只是尽量保持这个尽可能简单,因为有这个代码的一大堆实例。 – user2755309

+0

如果有大量代码实例,则应该考虑将其抽象化。为了帮助进一步了解为什么unset不适合你,我们需要更多的真实代码,而不是像你的问题那样使用伪代码。 –

+0

所以我只是在测试脚本之前,整个阵列如下.. – user2755309