1

I am trying to loop over certain array keys in drupal, but this is more of a generic php array question.

The array looks something like this...

$form['items'] = array(
  #title  =>  'hello',
  0       =>  array(
               #subtitle => 'hello2';
              ),
  1       =>  array(
               #subtitle => 'hello2';
              ),
  #prefix =>  '<div>hello</div>',
);

As you can see, the keys are a mix of numeric keys and #meta keys.

I am using this...

foreach($form['items'] as $x) {
  unset($form['items'][$x]['column1']); 
}

But i only want to target the numeric keys, I have tried is_numeric but it returned false.

Can someone tell me how to ignore the other keys? (Ignore #title and #prefix etc)

2
  • The # symbol in php starts a line comment. If your array declaration looks literally like the one that you pasted, $form['items'] contains 2 empty arrays and nothing more. Unless Drupal does some weird magic. Commented Oct 17, 2016 at 14:05
  • Yeah i think its a Drupal thing, the answer below sorted it! Commented Oct 17, 2016 at 14:15

2 Answers 2

2

You want to check the keys, but you are using the value in your foreach. Do the following:

foreach($form['items'] as $key => $value) {
    if (is_numeric($key))
        unset($form['items'][$key]); 
}

Hope I was helpful

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

Comments

2

Use is_int() rather than is_numberic()

foreach ($input_array as $key => $val) {
  if (is_int($key)) {
    // do stuff
  }
}

Important to note that is_int only works on things that are type integer, meaning string representations are not allowed.

Comments

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.