![](/img/trans.png)
[英]Filter Empty/Null value from associative array in php (multidimensional array)
[英]Remove key from nested/multidimensional array if value is empty ("") or (null)
我正在閱讀這是如何完成的,但是所有這些嵌套讓我有點困惑......
這是我當前的代碼:
function if_exists($a){
if(isset($a) && !empty($a)){
return $a;
} else {
return "";
}
}
$my_array = array(
array(
"@context" => "https://schema.org",
"@type" => "WebSite",
"@id" => get_site_url()."#website",
"name:" => ssd_fallback($myvar['name'],get_bloginfo('name')),
"headline" => get_bloginfo('description'),
"description" => $myvar['description'],
"url" => get_site_url()
),
array(
"@context" => "https://schema.org",
"@type" => $myvar['subtype'],
"@id" => $myvar['org_type'],
"url" => get_site_url(),
"name" => get_bloginfo('name'),
"logo" => $myvar['logo-url'],
"image" => $myvar['image-url'],
"description" => $myvar['description'],
"address" => array(
"@type" => "PostalAddress",
"streetAddress" => $myvar['address-street'],
"addressLocality" => $myvar['address-locality'],
"addressRegion" => $myvar['address-region'],
"postalCode" => $myvar['postal-code']
),
"telephone" => $myvar['phone'],
"email" => $myvar['email'],
"openingHours" => [$opening_hours],
"priceRange" => $myvar['price-range'],
"areaServed" => array(
array(
"@type" => "GeoCircle",
"geoMidpoint" => array(
"@type" => "GeoCoordinates",
"latitude" => $myvar['geo-lat'],
"longitude" => $myvar['geo-lat']
),
"geoRadius" => $myvar['geo-radius']
)
),
"sameAs" => array(
if_exists($myvar['same-as-1']),
if_exists($myvar['same-as-2']),
if_exists($myvar['same-as-3']),
if_exists($myvar['same-as-4']),
if_exists($myvar['same-as-5']),
),
"hasMap" => $myvar['has-map-url'],
"geo" => array(
"@type" => "GeoCoordinates",
"latitude" => $myvar['geo-lat'],
"longitude" => $myvar['geo-long']
),
"aggregateRating" => array(
"@type" => "AggregateRating",
"ratingValue" => ssd_fallback($gp_avg_star_rating,$myvar['star-rating-manual']),
"reviewCount" => ssd_fallback($gp_total_reviews,$myvar['nbr-reviews-manual'])
)
)
);
$my_json = json_encode($my_array,JSON_PRETTY_PRINT);
echo $my_json;
查看"sameAs"
-您可以看到我正在使用我的 function if_exists
。
但是,當使用它時,如果沒有值,我仍然會得到空字符串(如預期的那樣)
例如 JSON 結果:
"sameAs": [
"https://www.facebook.com/myco/",
"",
"",
"https://www.linkedin.com/company/myco/",
""
],
而我希望 output 看起來像這樣:
"sameAs": [
"https://www.facebook.com/myco/",
"https://www.linkedin.com/company/myco/"
],
通過閱讀,我嘗試array_filter
但這沒有用(即使我將if_exists
function 更改為返回null
); 進一步閱讀后,它看起來像是因為我手上有一個多維數組)。
我發現了一個正則表達式解決方案,它在 json object 的條帶 null 值上似乎很有吸引力(參見 Jim S 的回答),但這對我沒有任何作用。 然而,也許它只需要一個調整?
到目前為止,我從閱讀中獲得了一些其他選項(超出了我目前的實施能力):
A)專門“定位”元素,例如通過使用“路徑”(即,“sameAs”元素中的子項),例如使用unset()
按照從 PHP 中的數組中刪除元素(似乎有不止一種方法實際刪除密鑰)。
B)某種 function 或循環,此時我無法理解 PHP。
C) 或者可能是上述的組合。 ☺️
其他想法:
與其在定義數組后清理它,不如在數組定義中使用array_filter
以刪除空值。 如果您想在 JSON 中定義沒有索引號的數組,請使用array_values
:
"sameAs" => array_values(array_filter(array(
$myvar['same-as-1'],
$myvar['same-as-2'],
$myvar['same-as-3'],
$myvar['same-as-4'],
$myvar['same-as-5'])
)),
或者,如果$myvar
中只有 5 個值,或者您想使用所有這些值,則更簡單:
"sameAs" => array_values(array_filter($myvar)),
請記住,這將過濾掉空字符串""
、 null
、 false
和0
integer 或字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.