Jump to content

[SOLVED] array newbie -- limit number of outputs


simon551

Recommended Posts

Hi,

I'm trying to use an array to build a dynamic menu. I use array_unshift to build up an array but I want to limit the number of items that can be returned to the first 4 in the array:

 

$options=array();

if ($condition)
{
array_push($options,'a');
}	
if ($condition2)
{ 
array_unshift($options,'b');
}
if ($condition3)
{
array_unshift($options,'c');
}
if ($condition4)
{ 
array_unshift($options,'d');
}
if ($condition5)
{ 
array_unshift($options,'e');
}

foreach ($options as $key => $check) 
{

	echo 
	$check.'<br />';
}

<?php
$options=array();

if ($condition)
{
array_push($options,'a');
}	
if ($condition2)
{ 
array_unshift($options,'b');
}
if ($condition3)
{
array_unshift($options,'c');
}
if ($condition4)
{ 
array_unshift($options,'d');
}
if ($condition5)
{ 
array_unshift($options,'e');
}
$i = 0;
foreach ($options as $key => $check) 
{

	echo 
	$check.'<br />';
                if($i > 3){
                               break;
                }
                $i++;
}
?>

that didn't quite work. but this did  ;D

$i = 0;
foreach ($options as $key => $check) 
{
        $i++;

	echo 
	$check.'<br />';
                if($i > 3){
                               break;
                		  }
}

 

Thanks very much for your help

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.