1

I have an array that contains the following:

$code_ids
 [138]=>
  string(0) "asdsad"
  [126]=>
  string(0) ""
  [163]=>
  string(0) "asdasd"
  [162]=>
  string(0) ""
  [135]=>
  string(0) "awawawaw"
  [59]=>
  string(0) ""
  [63]=>
  string(0) ""
  [70]=>
  string(0) ""
  [146]=>
  string(0) ""
  [155]=>
  string(0) ""
  [66]=>
  string(0) ""

I want to get a hold of all indexes if it contains anything. How do you do this? I tried

foreach($code_ids as $code_id) {
  if(!empty($code_id)) {
     $index[] = $code_id;
  }
}

The problem in this is that it gets its value. I just want the index.

1 Answer 1

5

Try it

array_keys(         // 2. returns keys as new array
    array_filter(   // 1. removes all empty items
        $code_ids));

array_filter, array_keys

Sign up to request clarification or add additional context in comments.

1 Comment

+1, but be aware that array_filter() will remove items with the value of 0 or false as well, even '0' as a string.

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.