0

我正在尝试检查我是否$subCat在数组中有值,然后不要添加它。我应该最终得到一个,$subCat但是当我转储时,$details我仍然得到完整的数组。为什么是这样?

foreach ($detailsFunction as $main) 
{
        $mainlisting = $main['listingId'];
        $mainCat     = strtolower($main['mainCat']);
        $subCatO     = strtolower($main['subCat']);
        $subCat      = str_replace(" ", "-", $subCatO);

        if(isset($subCat) && $subCat == $subCat)
        {

          $details[] = array('url'     => base_url().'listings/'.$mainCat.'/'.$subCat,
                         'mainCat' => $main['mainCat'],
                         'subCat'  => $main['subCat']   
                        );
        }
}
4

1 回答 1

1

您是否关心保留数字键$details?如果不是,您可以添加每个项目$subCat作为其键,并在添加之前检查该键是否存在:

foreach ($detailsFunction as $main) 
{
        $mainlisting = $main['listingId'];
        $mainCat     = strtolower($main['mainCat']);
        $subCatO     = strtolower($main['subCat']);
        $subCat      = str_replace(" ", "-", $subCatO);

        if(!array_key_exists($subCat, $details))
        {

          $details[$subCat] = array('url'     => base_url().'listings/'.$mainCat.'/'.$subCat,
                         'mainCat' => $main['mainCat'],
                         'subCat'  => $main['subCat']   
                        );
        }
}
于 2012-11-24T03:35:57.697 回答