Jump to content

Recommended Posts

Hi guys .

 

suppose we have an array like this :

$MyArray=array("a1"=>5,"a2"=>6,"a3"=>7,"a4"=>8,"a5"=>9);

how can I generate a1,a2,a3... keys in a "For" loop instead of printing all the elements seprately , I'm looking for some method like this ( I know about "foreach" but I wanna use this method for some  reasons ) :

 

for ($i=0;$i<5;$i++)
{
        echo $MyArray[...];  // How can I get the key (...) ?
}

 

instead of :

 

echo $MyArray['a1'];
echo $MyArray['a12];
echo $MyArray['a3'];
.
.
.

 

thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/69880-array-question/
Share on other sites

Hello doctor_james,

 

Use foreach loop to get the array keys.

$MyArray=array("a1"=>5,"a2"=>6,"a3"=>7,"a4"=>8,"a5"=>9);
foreach($MyArray as $key => $value)
{
   // $key will be a1, a2,... and corresponing values will be 5,6,.... 
}

 

Hope this helps you.

Regards :)

Link to comment
https://forums.phpfreaks.com/topic/69880-array-question/#findComment-351005
Share on other sites

You cannot catch the key from an array in a for loop, unless you know what the keys are in the array. If the keys are defined as a1, a2 ... a12 etc. Then you could do:

$MyArray = array("a1"=>5,"a2"=>6,"a3"=>7,"a4"=>8,"a5"=>9);

for ($i = 1; $i < count($MyArray); $i++)
{
    $key = 'a' . $i;

    echo $key . ' = ' . $MyArray[$key] . '<br />';
}

 

However foreach would be better as its sole purpose is to loop through arrays.

$MyArray=array("a1"=>5,"a2"=>6,"a3"=>7,"a4"=>8,"a5"=>9);
foreach($MyArray as $key => $value)
{
   echo $key . ' = ' . $value . '<br />';
}

Link to comment
https://forums.phpfreaks.com/topic/69880-array-question/#findComment-351301
Share on other sites

Hello,

 

You cannot catch the key from an array in a for loop,

 

You can get the array keys in forloop using following code

 

// loop for array count
for ($i=0; $i<count($MyArray); $i++)
{
// get the current key and associated value for test array
list($var, $val) = each($MyArray);
// diplay key-value for array
echo 'Key :: ' . $var . ' value:: ' . $val .'<br>';
}

 

Hope this could solve your problem. :)

 

Regards,

Link to comment
https://forums.phpfreaks.com/topic/69880-array-question/#findComment-353885
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.