goodespeler Posted March 25, 2008 Share Posted March 25, 2008 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 More sharing options...
Laxidasical Posted March 25, 2008 Share Posted March 25, 2008 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 More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 So, I'm guessing you want to find $section in $sidebar's keys? I'd also suggest print_r($sidebar); I don't think the array is structured the way you want it to be. Link to comment https://forums.phpfreaks.com/topic/97854-using-an-array/#findComment-500692 Share on other sites More sharing options...
Barand Posted March 25, 2008 Share Posted March 25, 2008 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 More sharing options...
goodespeler Posted March 26, 2008 Author Share Posted March 26, 2008 Thanks Barand. What you did was enough to help me get things going. Link to comment https://forums.phpfreaks.com/topic/97854-using-an-array/#findComment-501309 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.