1

Lets say I have an multidimensional string array:

.food = array(
            'vegetable' => array(
                               'carrot' => 'blablue', 
                               'potato' => 'bluebla', 
                               'cauliflower' => 'blabla'
                           ), 
            'Fruit' => array(
                               'apple' => 'chicken65', 
                               'orange' => 'salsafood', 
                               'pear' => 'lokkulakka'
            )
);

is it possible to access the array by using index as numbers, instead of using the name of the key? So for accessing chicken65 , I will type echo $food[1][0]; I don't want to use numbers as key, because its a big array and its more user-friendly if I use string as key and it will let me do for-loops on advanced level.

2
  • 2
    If you don't want to use the number as the key, why are you asking how to do that? Commented Jun 23, 2011 at 13:54
  • Why not use foreach, instead of a for loop. Commented Jun 23, 2011 at 13:55

6 Answers 6

6

You can do foreach loops to achieve much the same thing as a typical for-loop.

foreach ($foods as $key => $value) {
    //For the first iteration, $key will equal 'vegetable' and $value will be the array
}
Sign up to request clarification or add additional context in comments.

Comments

4

$food[1][0] != $food[1]['apple'], so you cannot use numeric keys in this case.

Comments

1

try

$new_array = array_values($food);

however, variable can't start with .. It should start with $

2 Comments

array_values thats what i need :D
English isn't my native language
0

you may want to try the function array_values but since you are dealing with multidemsional arrays, here is a solution posted by a php programmer

http://www.php.net/manual/en/function.array-values.php#103905

but it would be easier to use foreach instead of for.

1 Comment

array_values thats what i need :D
0

You can use array_keys() on the array. The resulting array can be traversed via a for-loop and gives you the associative key.

I will show it to you for the first dimension:

$aTest = array('lol' => 'lolval', 'rofl' => 'roflval');
$aKeys = array_keys($aTest);
$iCnt = count($aKeys);

for($i = 0; $i < $iCnt; ++$i)
{
  var_dump($aTest[$aKeys[$i]]);
}

You would need to do this for two dimensions but I would not recommend this. It is actually more obstrusive and slower than most other solutions.

2 Comments

Why would you do this when you can simply use a foreach without it?
I would not do this, I would use foreach. I think this solution comes closer to what the OP asked for (no pun intended).
0

I don't think there is something native to go this route.

And it does seem like you are trying to stretch array use a bit.

You should go OOP on this one.

Create a FoodFamilly object and a Food object in which you can store arrays if necessary and you'll be able to write a nice user-friendly code and add indices if needed.

OOP is almost always the answer :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.