Jump to content

[SOLVED] Functions with unlimited arguments


DeanWhitehouse

Recommended Posts

You could use function_get_args(). Have a look at the example from the manual.

 

Alternatively just pass an array to your function, eg

 

$arr = array('one', 'two', 'three');

function make_list($arr)
{
    if(is_array($arr))
    {
        $list = '<ol>';
        foreach($arr as $item) $list .= '<li>'.$item.'</li>';
        $list .= '</ol>';

        return $list;
    }
    return false;
}

echo make_list($arr);

Link to comment
Share on other sites

yes that is fine. However for readability, it'll be better to construct your array before calling the function (as I have shown). Rather than construct the array when you call the function.

 

That's a bit of a waste of a variable in some cases, as long as you properly space your code:

make_list(array('foo' => 'bar',
                        'something' => 'something',
                        'test' => 'we get the point')
              );

Link to comment
Share on other sites

Last thing

If i use a multi-dimensional array how do i echo it out in a for each loop,

 

e.g.

<?php
$var = array(array('Home','index'));

foreach($var as $nav)
{
	?>
	<div style="background:#FFFFFF; border:#999999 thin solid; margin-top:2px; text-align:right; width:300px">
		<?php echo $nav; ?>
	</div>
	<?php
}

Link to comment
Share on other sites

You'll have to use a foreach loop within a foreach loop

$var = array(array('Home','index'));

   foreach($var as $nav)
   {
      ?>
      <div style="background:#FFFFFF; border:#999999 thin solid; margin-top:2px; text-align:right; width:300px">
         <?php foreach($nav as $title) echo $title; ?>
      </div>
      <?php
   }

Link to comment
Share on other sites

I tried that

<?php
function create_nav($link_name)
{
foreach($link_name as $nav)
{
	?>
	<div style="background:#FFFFFF; border:#999999 thin solid; margin-top:2px; text-align:right; width:300px">
		<a href="<?php foreach($nav as $nav_link){ echo $nav_link; }?>"><?php echo $nav; ?></a>
	</div>
	<?php
}
}
	create_nav(array(array('Home','index')));
?>

 

And that prints out

<div style="background:#FFFFFF; border:#999999 thin solid; margin-top:2px; text-align:right; width:300px">

<a href="Homeindex">Array</a>

</div>

Link to comment
Share on other sites

Adapted your code a bit.

function create_nav($nav_links)
{
   foreach($nav_links as $link)
   {
      ?>
      <div style="background:#FFFFFF; border:#999999 thin solid; margin-top:2px; text-align:right; width:300px">
         <a href="<?php echo $link['url']; ?>"><?php echo $link['title']; ?></a>
      </div>
      <?php
   }
}

      create_nav(array( array( 'title' => 'Home',
                               'url' => 'home.html'),

                        array( 'title' => 'About',
                               'url' => 'about.html'),

                        array( 'title' => 'Contact',
                               'url' => 'contact.html')

                      )
                );

Link to comment
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.