Tsalagi Posted November 30, 2009 Share Posted November 30, 2009 Howdy! I've racked my brain over this for 24 hours now. I think it's time I asked for help. I'm learning PHP and believe my I've manipulated many code blocks trying to get this to work with for and foreach and while statements. There must be something I'm missing in my studies about arrays but anyway here is the issue. I have a multidimensional array. From that array I would like to extract only the values of a specific key['name'] so that I can then output it into html form with a special identifier. I was able to do this with nested if statements but that seemed a bit overkill to me. Here is the code I came up. I know there has to be a more efficient way of getting these results. Here is the Array code: $options = array( array( "name" => "General Administrative Settings", "type" => "title"), array( "type" => "open"), array( "name" => "Colour Scheme", "desc" => "Which colour scheme would you like?", "id" => $shortname."_colourscheme", "type" => "select", "std" => "Choose a colour scheme:", "options" => $styles), array( "name" => "Portfolio Category", "desc" => "Select the category portfolio items are being posted in.", "id" => $shortname. "_portfolio_cat", "type" => "select", "std" => "Choose a category:", "options" => $getcat), array( "name" => "Definition List", "desc" => "Select the page used as a definition list page.", "id" => $shortname."_definition_list", "type" => "select", "std" => "Select a page:", "options" => $getpag), array( "name" => "Blog page", "desc" => "Select the page to be used as a blog (post) page.", "id" => $shortname."_blogpage", "type" => "select", "std" => "Select a page:", "options" => $getpag), array( "type" => "close") ); Here is my output code: <?php if($options['name'] = "Colour Scheme"){ echo '<div id="col_schm">Colour Scheme</div>'; if($options['name'] = "Definition List") { echo '<div id="def_lst">Definition List</div>'; } } ?> If you can help I would appreciate it. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/183471-extract-specific-information-from-a-php-multidimensional-array/ Share on other sites More sharing options...
premiso Posted November 30, 2009 Share Posted November 30, 2009 The real question is what is the goal of displaying these items...is there an order to them that they have to be displayed in a certain way? If so, one option of doing so is putting the display order in an array of it's own then loop through the original array putting those items into that created array to display them: $display_options = array($shortname."_colourscheme" => array(), $shortname . "_definition_list" => array()); foreach ($options as $option) { if (in_array($option['id'], $display_options)) { $display_options[$option['id']] = $option; } } foreach ($option as $opt) { echo '<div id="' . $opt['id'] . '">' . $opt['name'] . '</div>'; } Now that is a rough example, but it would probably be easier than the nested if's. You can set the display_options different for other pages etc. There is probably a better way to do it than that, such as storing that information in a database and being able to query and only pull the data you want/need so there will only be one loop to do. Quote Link to comment https://forums.phpfreaks.com/topic/183471-extract-specific-information-from-a-php-multidimensional-array/#findComment-968471 Share on other sites More sharing options...
Tsalagi Posted December 2, 2009 Author Share Posted December 2, 2009 Thanks Premiso for the reply. The code was output into a table and styled there. I decided to break to table down into divs and style them. I can't believe people are still using tables for styling. Oh well it was good practice and it looks terrific! Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/183471-extract-specific-information-from-a-php-multidimensional-array/#findComment-969472 Share on other sites More sharing options...
premiso Posted December 2, 2009 Share Posted December 2, 2009 Putting data in tables is still relevant, especially when you are say trying to display a full database table for manipulation. You just have to use the right tools for the job, if the data coming out is not tabular data, than way style it and make it look pretty with divs Quote Link to comment https://forums.phpfreaks.com/topic/183471-extract-specific-information-from-a-php-multidimensional-array/#findComment-969813 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.