1

My array looks like this:

Array
    ( [myarr] => Array (
                [504] => 2
                [508] => 25
        )
    ) 

Is it possible to echo a certain position of this array? I have tried:

echo $_SESSION['myarr'][0][0];

I can't seem to get anything to echo back.

EDIT: to be more specific.. Is it possible to echo it based on numeric index?

DavChana
  • 1,944
  • 17
  • 34
Norse
  • 5,674
  • 16
  • 50
  • 86

5 Answers5

5

Use array_keys() to get the keys into an array. Then access the 2D array using indexes in the keys array. Not that this is the best way to do this but it is a way to use numeric indexes to solve your problem.

$keys = array_keys($_SESSION["myarr"]);
$zero = $_SESSION["myarr"][$keys[0]];
DRiFTy
  • 11,269
  • 11
  • 61
  • 77
2

It's just a regular nested array. You use the index keys just as you normally would:

echo $_SESSION['myarr'][504];  //2

echo $_SESSION['myarr'][508];  //25
John Conde
  • 217,595
  • 99
  • 455
  • 496
2

Have a look at Get the first element of an array.

The following should work (untested, so no guaranties):

echo array_shift(array_values($_SESSION))[0][0];
Community
  • 1
  • 1
mariusnn
  • 1,847
  • 18
  • 30
1

Yes it is possible

print $array['myarr'][508]; // 25
Nazariy
  • 6,028
  • 5
  • 37
  • 61
-2

This is getting ugly.

$i = 1;
foreach ($myarr as $array) {
    if ($i == 2) echo $array;
    $i++;
}
Dave Kiss
  • 10,289
  • 11
  • 53
  • 75