Jump to content

[SOLVED] Basic Multidimensional Array Query


rhyspaterson

Recommended Posts

Hey guys,

 

Quick question. Say i have two different arrays:

 

$basic = array('a','b');

// and

$multi = array(
  'a' => array(
      'apple',
      'banana',
  ),
  'b' => array(
      'cherry',
      'pineapple',
  )
)

 

Now, if i wanted to make a variable the same as a piece of data in the $basic array i would go:

 

$x = 0;

$variable = $basic[$x];

// $variable now equals 'a'.

 

If i wanted to make a variable the same as a piece of data in the $multi array i would go:

 

$x = 0;

$variable = $multi['a'][$x];

// $variable now equals 'apple'.

 

However! If i wanted to make $variable equal to the name of the multidimensional array (that is - a), how would i do it? I can't go:

 

$x = 0;

$variable = $multi[];

// breaks

$variable = $multi[][];

// breaks

$variable = $multi[$x];

// breaks

 

I would like to be able to use a loop:

 

for ($y=0; $y < 10; $y++)
{
$array_one[$y]=$multi[NAME OF the $Y $MULTI];
}

 

Suggestions..?

Try this:

<?php
$multi = array(
  'a' => array(
      'apple',
      'banana',
  ),
  'b' => array(
      'cherry',
      'pineapple',
  )
);

foreach ($multi as $ary => $dmy) 
    echo $ary . '<br>';
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.