0

I need to assign in an associative array a number as key name, but if I do:

// Places (generated by mysql)
$places = array (
    0 => '1234',
    1 => '2345'
 );

// Week stats (generated by mysql)
$week = array (
  1234 => 
  array (
    0 => 
    array (
      'iid' => '1234',
      'mid' => 'xxxxxxxx',
      'name' => 'Name1',
    ),
    1 => 
    array (
      'iid' => '1234',
      'mid' => 'xxxxxxxx',
      'name' => 'Name3',
    )
  ),
  2345 => 
  array (
    0 => 
    array (
      'iid' => '2345',
      'mid' => 'xxxxxxxx',
      'name' => 'Name2',
    ),
    2 => 
    array (
      'iid' => '2345',
      'mid' => 'xxxxxxxx',
      'name' => 'Name4',
    )
   )
  );

  foreach($places as &$place) {

     echo $place;

     $i = 0;

     foreach($week[$i] as &$value) {

       echo $value["name"];
       $i++;
     }

  }

it doesn't work: http://codepad.viper-7.com/Y1g37t

because seems I should call it with:

echo $arr[<specific index>];

Instead I need to set "1234" and "2345" as strings, like this array:

$arr = Array("foo" => "bar");

So I can call it with

$arr[0] // bar

How can I do?

Solution

Thanks to @kirilloid

i use this code:

$vararr = array_keys($week);
$key =  $vararr[$i];

To get the key

4 Answers 4

3

It because it's an map and map associates values to keys so you have to do this :

<?php
$myNumber = 1234;
$myValue = "foo";
$arr = Array( $myNumber => $myValue );
echo $arr[1234];
?>

And don't forget to replace the ":" at your first line !

To iterate on a "map" you can use the foreach function :

foreach($arr as $key=>$value) {
    echo $key;
    echo $value;
}

This should display your key and the value associated :

1234
foo

Here is the difference with a simple array:

$array = array("foo", "bar", "hallo", "world");
echo $array[0];
Sign up to request clarification or add additional context in comments.

6 Comments

ok but what if I need to iterate it in a for loop? I need to assign numbers as key names and not as index, like they was trings...
You iterate over the whole array... like foreach($arr as $k=>$v){ .. echo $k; }. Instead of $k[0], it would just be $k inside the loop
the ":" was a typo on stack, by the way I was using 'as &$value' but even using your suggestion I get '$key = 0'
@user2070518 if this is the only code you have i don't see where is your problem. My code works well on my computer :s
Also, regarding your comment "by the way I was using 'as &$value'"... Why are you passing by reference? Are you modifying the array in any way?
|
2

You may either use array_keys:

echo $arr[array_keys($arr)[0]];

or reset and current:

reset($arr);
echo current($arr);

4 Comments

I like the first solution but doesn't works :( 'Parse error: syntax error, unexpected '[''
I needed to split it in two lines and now works! perfect, exactly what I needed!
Hmm, actualy first solution works in php 5.4+. And I get used to javascript, where this always were possible.
I know, in JS I use it too, but in my machine with PHP I need to split it as I said.
2

There is no problem here - this is how it is supposed to work.

If you create an array like this:

$myNumber = 1234;
$myValue = "foo";
$arr = Array( $myNumber => $myValue );

then the index of the element is 1234, not 0.

You can retrieve it with echo $arr[1234]

If you need to loop over the array you can do so with

foreach($arr as $key=>$value) {
  // do something with $value
}

Comments

0

There is no problem. As the value of $myNumber is 1234, you should access the array element like this:

echo $arr[1234];

If you need to access them in a foor loop you can get the keys as an array:

$keys = array_keys($arr);
$keys_count = count($keys);
for ($i=0; i<$keys_count; $i++) {
    echo $arr[$keys[$i]];
}    

3 Comments

yes but I need to call it using 0,1,2,3 ($i) etc in some for loop, so I can't use $myNumber
@user2070518: foreach($arr as $key => $value) ...
I've edited my answer so you can loop the values by using 0, 1, 2, etc. Anyway I'm curious about why you can't iterate as suggested.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.