Reputation: 345
I'm pretty new to PHP and want to write a "for" statement based on IDs of a page. The problem is that the IDs are not sequential (by that I mean they are not increasing by 1 each time ie. 1,2,3,4,5). Is there a way to use "for" to use specific numbers? For example (pseudocode)
for IDnumbers i = (1,5,7,23,28,34)
echo "function1(i)"
echo "function2(i)"
end
I hope that makes sense. I basically want functions associated with each post ID to be returned, but I want the IDnumbers to be specific. Sorry if this is a basic question!
edit: wow, that really was basic. Thanks guys!
Upvotes: 2
Views: 139
Reputation: 805
IF you MUST use FOR, then you can do this:
$IDNumbers = (1,5,7,23,28,34);
for($i = 0; $i < count($IDNumbers); $i++):
echo function1($IDNumbers[$i]);
endfor;
This is because even though you stored a number as the value in a numeric array, it still orders it sequentially in reference to the identifiers and where they were added. The value may be 7, but its pointer is 2 (0, 1, 2, 3, 4, 5 in this case)
Upvotes: 2
Reputation: 196
try something like this:
IDnumbers = array(1,5,7,23,28,34)
foreach IDnumbers as id
echo "function1(id)"
echo "function2(id)"
end
Upvotes: 2
Reputation: 440
$IDNumbers = array(1,5,7,23,28,34);
foreach($nums as $num)
{
echo function1($num);
echo function2($num);
}
Read more about php foreach here.
Upvotes: 2
Reputation: 91857
foreach (array(1,5,7,23,28,34) as $n) {
echo "function1($n)";
echo "function2($n)";
}
Upvotes: 3
Reputation: 45589
// create an array
$numbers = array(1,5,7,23,28,34);
// loop over it
foreach($numbers as $number){
echo function1($number);
echo function2($number);
}
Upvotes: 4
Reputation: 324620
foreach( Array(1,5,7,23,28,34) as $i) {
// do stuff with $i
}
Upvotes: 2