Jump to content

Using an Array


goodespeler

Recommended Posts

Hi,

 

I'm trying to use an array for something. Here's what my array looks like:

 

	$sidebar = array( array( 'section'  => 'overview',
						 'showVideo' => true,
						 'showContact' => true,
						 'showTestimonial' => true
						),	
				  array( 'section'  => 'solutions',
						 'showVideo' => true,
						 'showContact' => true,
						 'showTestimonial' => false
						)
				);

 

There's a global var called $section.

 

I want to do this:

 

Look for $section in $sidebar array. Once it finds it, use the showParameters in the array to include various files or do some sort of other action.

 

 

Any help doing this would be appreciated.

 

 

Link to comment
https://forums.phpfreaks.com/topic/97854-using-an-array/
Share on other sites

Try this...

 

$sidebars = array( array( 'section'  => 'overview',
						'showVideo' => true,
						'showContact' => true,
						'showTestimonial' => true
						),	
				  array( 'section'  => 'solutions',
						'showVideo' => true,
						'showContact' => true,
						'showTestimonial' => false
						)
				);


foreach ($sidebars as $sidebar)
{
	if ($sidebar['section'] == $section) {// do what you want it to do here}
}

 

NOTE: I made your original $sidebar variable plural: $sidebars. Don't forget to make that change!

Link to comment
https://forums.phpfreaks.com/topic/97854-using-an-array/#findComment-500688
Share on other sites

here's a start

<?php
$sidebar = array( array( 'section'  => 'overview',
						 'showVideo' => true,
						 'showContact' => true,
						 'showTestimonial' => true
						),	
				  array( 'section'  => 'solutions',
						 'showVideo' => true,
						 'showContact' => true,
						 'showTestimonial' => false
						)
				);

function findSection ($section, &$sidebar)
{
    foreach ($sidebar as $k => $data)
    {
        if ($data['section'] == $section)
            return $sidebar[$k];
    }
    return array();
}

$section = 'solutions';

if ($a = findSection ($section, $sidebar) )
{
    echo '<pre>', print_r($a, true), '</pre>';
}
else {
    echo "not found";
}                    
?>                    
?>

Link to comment
https://forums.phpfreaks.com/topic/97854-using-an-array/#findComment-500698
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.