2017-08-26 36 views
-4

我會盡我所能解釋這一點,並保持簡短。我正在解析一個JSON文件。我已經根據JSON中的鍵創建了變量,並通過它循環。我已經有一個array_filter過濾了我想要允許的循環中的值。我不確定的是如何更進一步,並通過不同的鍵/變量的值進行另一次檢查和禁止。PHP - 按值過濾並省略循環

在我的JSON我有這個鍵/對象...

"geocode": { 
        "UGC": [ 
         "TXZ179", 
        ], 

現在我已經創造了這個得到這個值是像這樣的varible ...

$geocode = $currFeature['properties']['geocode']['UGC'][0]; 

現在,如果你注意到的價值是

TXZ179

我只需要從該值的前兩個字母,所以我已經剝離下來,只顯示前兩個字母是這樣的...

$state = substr($geocode, 0, -4); 

這給我留下了TX的從上面的值。

我解釋說明了這一點。我想獲取$ state變量並過濾掉,並在我的循環中忽略來自該變量的TX值。

前通過我的解析JSON我環路我通過我想告訴像這樣的值過濾它...

//Lets filter the response to get only the values we want 
$alerts = array(
    'Tornado Warning', 
    'Severe Thunderstorm Warning', 
    'Hurricane Warning', 
    'Tropical Storm Warning', 
    'Flash Flood Warning', 
    'Flood Warning', 
); 

// filter features, remove those which are not of any of the desired event types 
    $alertFeatures = array_filter($features, function(array $feature) use ($alerts) { 
    $eventType = $feature['properties']['event']; 

    return in_array($eventType, $alerts); 
}); 

// flip alerts, mapping names of alerts to index in array (we'll use it to order) 
$order = array_flip($alerts); 

// sort elements by order of event type as desired 
usort($alertFeatures, function (array $a, array $b) use ($order) { 
    $eventTypeA = $a['properties']['event']; 
    $eventTypeB = $b['properties']['event']; 

    return $order[$eventTypeA] - $order[$eventTypeB]; 
}); 

這個問題有,而已經產生了與過濾掉某一值做多現有的array_filter允許某些值以及它們如何共存。

所以我的問題是由於缺乏知識。我不確定使用什麼函數或者如何通過$ state變量中的TX值來禁止它。從我的知識array_filter是爲了過濾出你想保留的值。那麼我怎麼能在循環拋出它之前用TX刪除值,並且它傳遞了第一個array_filter。

+0

您尚未嘗試執行此任務。您正在要求我們爲您編寫代碼。請先嚐試並告訴我們你卡在哪裏。使用'substr($ geocode,0,2)'來獲得前兩個。你的問題顯示沒有研究或試圖自我解決,並可能被低估。請更新您的問題。 – mickmackusa

+0

以前曾詢問過濾深度值的multidim數組。 – mickmackusa

+0

雖然你的問題提供了針的值('$ state'),但它不提供乾草堆值('$ features')。我希望當你回到一個星期後,你會在你的問題中包含這個,以便它們都包含一個[MCVE](https://stackoverflow.com/help/mcve)。如果你編輯了關於這個項目的其他最近的問題,我會很高興爲所有問題提供全部的幫助。我不是在攻擊你,我是在支持你,所以。 (當你離開時,我不會編輯你的問題 - 這對我希望能夠合作完成的東西會產生反作用)。 – mickmackusa

回答

1

您可以增強您現有的過濾功能,以根據其地理信息排除功能。

// this is the TX You don't want 
$ignoreState = 'TX'; 

// filter features, remove those which are not of any of the desired event types 
// and also remove those from $ignoreState 
$alertFeatures = array_filter($features, function (array $feature) use ($alerts, $ignoreState) { 
    $eventType = $feature['properties']['event']; 
    $isValidEvent = in_array($eventType, $alerts); 

    $geocode = $feature['properties']['geocode']['UGC'][0]; 
    $isValidState = substr($geocode, 0, 2) != $ignoreState; 

    return $isValidState && $isValidEvent; 
}); 

,如果你想從產生排除的陣列項目的過濾功能僅僅是應該返回true如果你想數組項($feature)是導致陣列中的功能,或false陣列。

對於您的情況,如果其事件是列入白名單的事件之一($alerts)並且州代碼($state)不是TX,您希望數組項目位於結果數組中。

+0

「$ isValidState」是幹什麼用的?我已經在使用substr()和$ state變量來修剪$ geocode變量,以返回地理編碼值的前兩個字符。 $ isValidState做同樣的事情還是執行不同的事情。 – Texan78

+0

PHP substr創建新的字符串,然後比較,與strncmp代碼更快,使用更少的內存。這可能會更具可讀性,但爲什麼會妨礙性能並使用更多堆棧? :) –

+0

我有$ state = substr($ geocode,0,-4);在我的循環中做同樣的事情。我是否應該將!= $ ignoreState添加到我的循環中? – Texan78