I'm making use of foreach($field as $fieldkey => $fieldval) to iterate through an array of fields and I'm wondering if the order of $fieldkey is guaranteed to be the same as when $field was populated? That is, if I know that $fields = array( 'one' => '1', 'two' => '2'); can I count on foreach giving me $field->one followed by $field->two? (This is a generic question applying to all arrays).

While I'm sure there are better ways to do this, what I have built is a content type that has a dynamic number of fields that come in triplets and in a specific order (eg: ['name:1'], ['time:1'], ['count:1']) and while I can easily parse the names to keep all "1"s together I would think it would be much less expensive to simply know that the order doesn't change ... then I can just look for a start and an end.

I know that it does seem to work this way but I can't find anything that says it is guaranteed to do so and I don't want to depend on that behaviour unless I am expected / allowed to.

Comments

jaypan’s picture

That's too generic of a question to really be able to answer. Sorry.

Contact me to contract me for D7 -> D10/11 migrations.

justageek’s picture

You are saying you built a dynamic content type in Drupal? With CCK? If not, can you be more specific as to what you built? Also, if you can describe the problem you are trying to solve, someone might suggest an easier solution using Drupal.

To answer your question about arrays, yes, foreach iterates through the in the order in which you built the array, if you define


$a = array('one' => 1, 'two' => 2);

then foreach will iterate through them in that order, the only reason the order of keys changes is if you tell it to change somehow, by using a sort function, or slicing apart pieces and rebuilding it or something.

Dave Kinchlea’s picture

Thanks much, you have answered my question ... FYI it is a custom module, not CCK and it was specifically the $form array that I am building out dynamically depending upon what results are available ... the {usage_profile} content type builds out based on multiple records within {usage_profile_gen}, each record holds three related fields $form that are displayed horizontally in a grid fashion. In the form I created a fieldset called ['transactions']['transaction'] and the three fields that are a part of that fieldset are called ['name:%N'], ['count:%N'], and ['time:%N'] . I was using preg_match on each $field and then strncmp to parse out the name but that is somewhat expensive if I could simply rely on the order. I did implement the simpler algorithm and it works of course but I was wondering if that was happenstance rather than by design.

Anyway while writing the question I realized it had little to do with $form and much to do with how PHP stores arrays hence the more generic question (getting hung up on the associative key ... just making life more complicated for myself, not for the first time I might add :-)).

@laybunz: thanks for the extra info

laybunz’s picture

Query results are not guaranteed to be in any specific order, so if you require that coming out of your DB be sure to use the ORDER BY clause.