有条件地改变多维数组内子数组的存在

时间:2016-11-30 20:34:19

标签: php arrays multidimensional-array

我的问题底部有一个Master PHP数组。这一切都应该工作,并完美地编码到json。我的问题仅限于数组中显示的“内容”对象。

'content'对象应该包含最多十个子数组($ 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值的子阵列的记录。

以下是Master Array的一部分,其中包含子数组元素,我需要根据相关图像的可用性来有条件地包含或排除这些元素。一切正常......只需要解决上面的问题。

$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);

1 个答案:

答案 0 :(得分:0)

您可能想要unset但不要将通话结果存储在同一个地方。

此致:

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

更新

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

在内容数据中设置元素之前,您还可以通过验证媒体条件来更有效地执行此操作。