0

this answer maybe on google but i couldnt find it because i dont know the exact keywords for this. The situation is as follows. I have an array called $value that contains 2 types of arrays:

[12] => Array ( 
        [id] => phone 
        [class] => phone 
        [config] => Array ( 
                            [disabled] => 1 
                            [value] => 1
                )
) 

[46] => Array ( 
        [id] => email 
        [class] => email 
        [config] => Array ( 
                            [display_type] => disabled 
                            [value] => 1
                    )
)

As you may see, they are almost the same except for the fact that one contains an array called "disabled" and the other "display_type". What im trying to do is to count how many of each type i have. I tried with this:

foreach ( $value as $col ) {
    if ( $col->config == 'disabled' ) {
        $total_default++;
    } elseif ( $col->config == 'display_type' ) {
        $total_custom++;
    }
}

but didnt work. I know it must be very simple but i just cant figure it out.

Thank you.

1
  • When comparing strings, use ===, instead of ==, or you might see some odd behaviour once in a while. PHP will evaluate '1e3' == '1000' to true, for instance. Just a good tip :) Commented Dec 26, 2013 at 6:25

1 Answer 1

1

$col is an array, not object.

foreach ($value as $col) {
  if (array_key_exists('disabled', $col['config'])) {
      $total_default++;
  } else if (array_key_exists('display_type', $col['config'])) {
      $total_custom++;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. so those things (disabled and display_type) are called array keys?
@Nuker Yes, they are.

Your Answer

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