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

PurpleFoxy
  • 1,107
  • 3
  • 12
  • 17

4 Answers4

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];
Freelancer
  • 4,459
  • 2
  • 22
  • 28
  • 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... – PurpleFoxy Sep 04 '13 at 22:19
  • 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 – brbcoding Sep 04 '13 at 22:21
  • the ":" was a typo on stack, by the way I was using 'as &$value' but even using your suggestion I get '$key = 0' – PurpleFoxy Sep 04 '13 at 22:28
  • @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 – Freelancer Sep 04 '13 at 22:33
  • sorry I was not clear, I iterate a 2nd array and I use $i as index incrementing it each time by 1, then I use $i as key value in the 1st array. I guess this is the misunderstanding – PurpleFoxy Sep 04 '13 at 22:36
  • 2
    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? – brbcoding Sep 04 '13 at 22:36
2

You may either use array_keys:

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

or reset and current:

reset($arr);
echo current($arr);
kirilloid
  • 14,011
  • 6
  • 38
  • 52
  • I like the first solution but doesn't works :( 'Parse error: syntax error, unexpected '['' – PurpleFoxy Sep 04 '13 at 22:59
  • I needed to split it in two lines and now works! perfect, exactly what I needed! – PurpleFoxy Sep 04 '13 at 23:03
  • Hmm, actualy first solution [works in php 5.4+](http://stackoverflow.com/questions/1459377/access-array-returned-by-a-function-in-php). And I get used to javascript, where this always were possible. – kirilloid Sep 05 '13 at 13:31
  • I know, in JS I use it too, but in my machine with PHP I need to split it as I said. – PurpleFoxy Sep 05 '13 at 17:05
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
}
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]];
}    
gontrollez
  • 6,372
  • 2
  • 28
  • 36