0

I am trying to check if I have the $subCat value in the array then don't add it. I should end up with one of each $subCat but when I dump $details I am still getting the full array. Why is this?

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']   
                        );
        }
}
2
  • In which array do you want to check if $subCat exists? Right now you're just checking if the variable is set, and if it equals itself (which will always be true) Commented Nov 24, 2012 at 3:21
  • @freejosh ahh thanks. I am trying to check the $details[] Commented Nov 24, 2012 at 3:26

1 Answer 1

1

Are you concerned with keeping the keys of $details numeric? If not You could add each item with $subCat as its key, and check if the key exists before adding it:

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']   
                        );
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Josh Thats what I wanted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.