-1

I am looking for a simple way to see if this array:

array('apples', 'bananas');

is associative or not, that is key=>value. Now PHP will give you something like:

array(2){
    [0]=>'apples'
    [1] => 'bananas'
}

if you var_dump the above array. So in theory I have my associative array. But what I am looking for is to see if array('key' => 'some value', 'some_other_key' => 'some other value'); is a "true" associative array or if its just a regular array.

I have seen a couple posts on this through out stack but a lot of their answers are really complicated or poorly written or not even OO at all.

LogicLooking
  • 916
  • 1
  • 16
  • 32
  • So why don't you tell us what you saw and how it wasn't good enough? – Jon Oct 10 '13 at 20:26
  • What is the difference between a truly associative array and regular? PHP knows only that one array, if you do not count instances of SplFixedArray. – Sven Oct 10 '13 at 20:26
  • What you have is an enumerated array, not an associative array: if the keys were strings, then it would be an associative array – Mark Baker Oct 10 '13 at 20:27
  • 2
    Have you seen [this](http://stackoverflow.com/questions/173400)? – George Brighton Oct 10 '13 at 20:27

1 Answers1

1

You mean you want to differentiate between purely numeric-keyed arrays v.s. string-keyed?

$string_keys = preg_grep('/\D/', array_keys($your_array));
if (count($string_keys) > 0) {
   echo "at least one non-numeric key - it\'s associative";
} else {
   echo "no non-numeric keys. it's a normal array";
}

in short: grab all the keys in the array, use the preg_grep function to search for NON-digits in the resulting array-of-keys. If you get a non-zero count of matching keys, you've got an associative array.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    So `array('key' => 'value')` will echo `"at least one non-numeric key - it\'s associative";` if I understand this correctly? – LogicLooking Oct 10 '13 at 20:30
  • This does not recognize "holes" in the array, see: http://codepad.org/3UYjy0Ht ([This answer](http://stackoverflow.com/a/4254008/603003) provides a better way.) – ComFreek Oct 10 '13 at 20:32
  • @comfreek: OP didn't demand that the array be consecutively indexed... just whether it's associative or not. sparse array are perfectly legal in PHP and many other languages. – Marc B Oct 10 '13 at 20:33
  • 1
    @MarcB It's a bit picky but your function also thinks `array( '0' => 'Value' )` is numeric. – Jason Oct 10 '13 at 20:39
  • 2
    `'0' == 0` is TRUE in PHP, so yes... it is a numerically keyed array – Marc B Oct 10 '13 at 20:40
  • Hmm... I see indeed. A little playing around and PHP automatically converts the string '0' to int when used as array key. I would've thought it kept the type regardless, which would be `'0' !== 0`. (This is from a quick `foreach( $array as $key => $value ){ var_dump( $key ); }` so if my observations are skewed give me a heads up.) – Jason Oct 10 '13 at 20:48
  • php has "lazy" comparisons, `==`, `!=` which only compare values after one's been type coerced to match the other, and strict comparison, `===`, `!==` which compare type AND value. `'0' == 0` is true, but `'0' === 0` is false – Marc B Oct 10 '13 at 20:54