1

Is it ok to build an array with numerical keys but assign items to arbitrary key numbers?

i.e.

$test = array(23=>'first thing', 245=>'second thing'); 
CharlesB
  • 86,532
  • 28
  • 194
  • 218
dibs
  • 1,018
  • 2
  • 23
  • 35

3 Answers3

6

There's nothing inherently wrong with it, especially if you think of constructing one like you are to assign an ID to a value. If you're doing something like looping from for($i=0;$i<1000000;$i++) to find the values, then yes, you might want to rethink how you're writing it.

jprofitt
  • 10,874
  • 4
  • 36
  • 46
  • `for` doesn't make any sense for non-arrays. Irrelevant to the question. You see, you want to add more words to fit the "guidelines of good answer" but you added **irrelevant** piece of information. Any reason to do so? – zerkms Mar 01 '12 at 01:51
  • @zerkms How is this not an array? – jprofitt Mar 01 '12 at 01:53
  • it is hash, not an array. And, yes, there is a difference. And, again, yes, I know there are no "true" arrays in php. And for traversing hashes there is a `foreach` in php – zerkms Mar 01 '12 at 01:53
  • @zerkms Also, `for($i=0;$i – jprofitt Mar 01 '12 at 01:54
  • it modifies the original array. And gives no any profit in comparison to `foreach`. Still just a words to have volume (without any valid reason) – zerkms Mar 01 '12 at 01:55
3

Yep

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 4
    I know there's not much to say about this topic, but you can probably do a little better than "Yep". – Wesley Murch Mar 01 '12 at 01:47
  • 1
    @Madmartigan: there is a **specific** question that assumes the same specific answer. It makes no sense to explain the quantum physics to person who asked about alphabet – zerkms Mar 01 '12 at 01:48
  • 3
    Not the downvoter, but probably because you didn't offer any explanation with your answer. It *is* the exact answer the poster was looking for, but at 42.4k you know that there are guidelines for answers like there are questions – jprofitt Mar 01 '12 at 01:48
  • @jprofitt: let's see if other answers has just any useful information rather than the same "yes" word. For now I see 2 other answers and they have nothing useful to OP. Even though they have more words - they don't have more useful information – zerkms Mar 01 '12 at 01:49
  • 2
    @zerkms I gave some clarification about what would make it *not* ok, thereby adding value to my answer. And Ed S. explained what he was making. Both add useful information to the answer – jprofitt Mar 01 '12 at 01:51
  • @jprofitt: commented your clarification - it makes no any sense for *arrays*. Don't see any additional value – zerkms Mar 01 '12 at 01:53
  • @zerkms Like I said, I wasn't the downvoter so I'm not sure where this hostility toward me is coming. I was simply offering an explanation. – jprofitt Mar 01 '12 at 01:55
  • @jprofitt: it's not a hostility, it's just a demonstration that even though your answer has more words - the other words rather than "There's nothing inherently wrong with it" are **irrelevant** to the question. – zerkms Mar 01 '12 at 01:57
  • @zerkms What I added may not have been all-encompassing of every situation, but it was most certainly relevant to his. I gave him a broad example of a way to abuse using numeric values as keys, which would in turn make what he was doing wrong. – jprofitt Mar 01 '12 at 01:58
  • @jprofitt: I can repeat it once again - there is a `foreach` to traverse hashes. And there are billion things you could do wrong using hashes and arrays. And there is no reason to explain all those wrong things when the question was the only "is it ok or not". If some one asks you about a time on the street - do you just say it is 12pm now or trying to explain person that time depends on gravity and there is no absolute time, and if we were living near to the blackhole the time would go slower? I bet you just go with first case. – zerkms Mar 01 '12 at 02:03
  • @zerkms I think we both have valid points. As regular members of this site, we should just leave this be, especially since neither of us are being relevant to this question. Truce? – jprofitt Mar 01 '12 at 02:06
  • @jprofitt: truce, I didn't fight actually yet ;-) Just wanted to express my point of view - that simple questions mean simple answers. – zerkms Mar 01 '12 at 02:08
  • 2
    And to anyone who stumbles on this in the future, this is how it *is* possible to have a disagreement on the Internet and **not** be uncivil jerks to one another. – jprofitt Mar 01 '12 at 02:09
1

Yes, that is an associative array and that is what makes it useful.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • "that is an associative array and that is what makes it useful" --- how being an associative makes an array useful? Probably I just cannot get the basic idea, but the phrase itself confuses me a lot :-S – zerkms Mar 01 '12 at 02:04
  • @zerkms: If you could only add elements 0-N then it wouldn't be a very useful associative array... it would just be a "normal" array. – Ed S. Mar 01 '12 at 17:36
  • yes, that is how hashes work. Still don't see any extra usefullness, because it is the feature "by design" – zerkms Mar 01 '12 at 20:43
  • @zerkms: ...yes, obviously it is by design, and that is the point. Did you read the question? Also interesting that you would question this when you answered "Yep". – Ed S. Mar 01 '12 at 20:54
  • yes I did. And I don't see anything about usefullness by design. It is about "is it ok or not". – zerkms Mar 01 '12 at 20:55
  • @zerkms: You answered the question literally, and while technically correct, it gives the OP (who is obviously a beginner) zero insight into what is actually happening here. Sometimes, especially with beginners, you need to think for more than 2 seconds in order to understand what they are really having trouble understanding. – Ed S. Mar 01 '12 at 21:57
  • uhm, the note about usefulness of associative arrays also doesn't give any additional value. They are useful by definition, otherwise they wouldn't be used or implemented. Every array in php is a hash. So according to your answer - every array is useful. Oh, yes, this info makes sooooo much sense! – zerkms Mar 01 '12 at 22:01
  • @zerkms: Of course it does; it tells the OP that *this is how they are designed to be used*. It's certainly more useful than "Yep", but honestly, I don't care to argue with you anymore as your comments are just dull and pedantic. – Ed S. Mar 01 '12 at 22:18
  • programming is a science around being pedantic and precise. Saying that something is useful just because it has that behaviour by design doesn't give any value at all. – zerkms Mar 01 '12 at 22:40
  • @zerkms: Ok well let's not fool ourselves here; neither of us are practicing any sort of "science" here. I write hardware interfaces and image processing routines (using methods developed by "real" scientists), you write web apps, maybe some back end stuff from what I can tell. This is all getting very boring. I am still amazed that you feel you have a foot to stand on considering your answer is at least as useless as mine, but whatever; let's stop wasting our time here. – Ed S. Mar 01 '12 at 22:51
  • @zerkms: I was over the line in that comment, I apologize. – Ed S. Mar 01 '12 at 22:51
  • yep... Sorry for being a jerk - I'm in a very bad mood the last days :-( For now in even worse mood because of the discussion in which I abuse the person I don't even know. Sorry again :-( – zerkms Mar 01 '12 at 22:52
  • @zerkms: :-) Me too. Hope you find yourself in a better place soon. So easy to get into little spats on sites like these. – Ed S. Mar 01 '12 at 22:53
  • btw, yeah need to find some time to get my CV better - indeed now it looks like yet-another-webdev resume, but I definitely have some interesting things I could specify there ;-) – zerkms Mar 01 '12 at 22:55
  • @zerkms: I'm sure. Like I said... I was essentially trying to be a jerk and I wrote something stupid. We all have our interests, and I would wager that a "typical web-dev" is probably not spending much time on SO helping people. – Ed S. Mar 01 '12 at 22:57