Jump to content

for each


proud

Recommended Posts

My array contains names e.g. (clark, daniel and mike) .. I want to print all the possible combination with numbers from 1 to 100 e.g. (clark1,clark2,clark3,etc.. daniel1,daniel2 etc..) and at the end of every combination I want to add a certain character e.g. (clark1 absent,clark2 absent, daniel1 absent etc..)

 

I tried this code but it didn't work:

<?php

$names = "dave darko dalton";

 

for ($i=1, $i<101, $i++)

{

foreach( $names as $key => $k){

echo $names. $i. 'absent';

 

 

?>

Link to comment
https://forums.phpfreaks.com/topic/140903-for-each/
Share on other sites

I suggest you brush up on your PHP. This code had numerous errors.

 

1. Your $names array was not an array at all

2. Your for loop was not closed or seperated properly

3. your foreach was not closed

4. You were not referencing your array properly.

 

When you do foreach($arrak as $key => $value ), you then must reference the $key and $value vars inside the foreach. You were doing $names as $key =>$k and then referencing the variable $names inside.

 

Here is the working code, study it and then study PHP some more to get a feel for how the syntax is supposed to work.

 

<?php
   $names = array('dave','darko', 'dalton');

for($i=1; $i<101; $i++)
{
foreach( $names as $k){
	echo $k. $i. ' absent<br />';
}
}

?> 

 

 

This gives .....

dave1 absent

darko1 absent

dalton1 absent

dave2 absent

darko2 absent

dalton2 absent

dave3 absent

darko3 absent

dalton3 absent

...............

 

I assume that this is what your wanting?

 

Nate

Link to comment
https://forums.phpfreaks.com/topic/140903-for-each/#findComment-737525
Share on other sites

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.