我有一個大類陣列結構PHP array_search - 正確的方式來獲得價值
[0] => WP_Term Object ([term_id] => 1028
[name] => Sale woman
[slug] => sale-woman
[term_group] => 0
[term_taxonomy_id] => 1028)
[1] => WP_Term Object ([term_id] => 1029
[name] => Sale man
[slug] => sale-man
[term_group] => 0
[term_taxonomy_id] => 1029)...
我不需要那麼多的價值,我只顯示該陣列的一部分()。所以,我從這個數組
$ids = array();
$i = 0;
foreach ($cats as $key=>$cat) {
$ids[$i]['id'] = $cat->term_id;
$ids[$i]['catname'] = $cat->name;
$i++;
}
它運作良好,並返回我
Array ([0] => Array ([id] => 1028 [catname] => Sale woman)
[1] => Array ([id] => 1029 [catname] => Sale man))...
比我需要找到當前類別該數組中,並得到下一個和類別分組的ID創建自己的數組。我嘗試
$thisindex = array_search($this_category_id, $ids['id']);
$previd = $ids[ $thisindex - 1 ];
$nextid = $ids[ $thisindex + 1 ];
但是,當我嘗試ECHO $ previd或$ nextid它什麼也沒有返回。 $this_category_id
效果很好,我認爲問題出在array_search($this_category_id, $ids['id']);
我嘗試也$編號[] [ '身份證'] - 導致錯誤
$ IDS [$ i] [ '身份證'] - 沒有返回
如何找到$previd
和$nextid
?需要幫助
您將獲得'$ ids ['id']的索引您需要一個ID數組 – ArtisticPhoenix
我沒有看到通過將原始數組轉換爲您的'$ ids'數組實際實現了什麼除了轉動一個完美將可用的對象數組轉換爲數組數組並浪費時間和內存塊的過程 – RiggsFolly