从数组中删除数据

时间:2013-04-01 12:28:04

标签: php arrays

我有一个问题,如果“lastpost”设置为“No Posts”,我需要忽略任何数组键

这是$ sublast的数组转储:

    array(3) { [0]=> array(9) { ["forum_category_id"]=> string(1) "3" ["name"]=>  string(15) "Sub category #1" ["description"]=> string(39) "This is a short description test.... #2" ["topics"]=> string(1) "0" ["lastpost"]=> string(8) "No Posts" ["lastpostauthor"]=> string(1) " " ["lastposturl"]=> string(88) "http://test.codetrove.com/index.php?route=forum/read&forum_path=3&forum_post_id=" ["posts"]=> int(0) ["href"]=> string(81) "http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=1_3" } [1]=> array(9) { ["forum_category_id"]=> string(1) "7" ["name"]=> string(16) "Test sub Sub cat" ["description"]=> string(0) "" ["topics"]=> string(1) "2" ["lastpost"]=> string(26) "Mon Apr 1, 2013 1:00:42 pm" ["lastpostauthor"]=> string(16) "Justine Smithies" ["lastposturl"]=> string(90) "http://test.codetrove.com/index.php?route=forum/read&forum_path=7&forum_post_id=63" ["posts"]=> int(2) ["href"]=> string(81) "http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=1_7" } [2]=> array(9) { ["forum_category_id"]=> string(1) "9" ["name"]=> string(11) "Test cat #3" ["description"]=> string(20) "Short description #3" ["topics"]=> string(1) "2" ["lastpost"]=> string(26) "Mon Apr 1, 2013 1:00:15 pm" ["lastpostauthor"]=> string(16) "Justine Smithies" ["lastposturl"]=> string(90) "http://test.codetrove.com/index.php?route=forum/read&forum_path=9&forum_post_id=62" ["posts"]=> int(2) ["href"]=> string(81) "http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=1_9" } } 

这是我试图做的代码,只是因为它总是适用于最好的代码,但是我需要一种方法来删除顶部条目,如果将“lastpost”设置为“No Posts”所以它得到了正确的信息。

                        <?php
                        $sublast = $forum_category['children'];
                        foreach ($sublast as $key => $row) {
                        $dates[$key]  = $row['lastpost']; 
                        }
                        array_multisort($dates, SORT_ASC, $sublast);
                        $lastitem = $sublast[count($sublast) - 1];
                         var_dump($sublast); 
                        ?>

这是$ lastitem的转储:

    array(9) { ["forum_category_id"]=> string(1) "3" ["name"]=> string(15) "Sub category #1" ["description"]=> string(39) "This is a short description test.... #2" ["topics"]=> string(1) "0" ["lastpost"]=> string(8) "No Posts" ["lastpostauthor"]=> string(1) " " ["lastposturl"]=> string(88) "http://test.codetrove.com/index.php?route=forum/read&forum_path=3&forum_post_id=" ["posts"]=> int(0) ["href"]=> string(81) "http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=1_3" }

正如你所看到的那样,当整个数组条目被移除时它需要“No Posts”,所以需要数组[1]这是我需要的那个。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

在php中使用unset函数删除元素

答案 1 :(得分:0)

Unset将满足您的需求。

unset($array)