discusster Posted November 10, 2006 Share Posted November 10, 2006 Can someone help me out please?I'm needing some advice on how to take a multidimensional array (which you can view here: http://discusster.textdriven.com/sandbox/index.php) and break it down into a single array with the current array values joined into strings?In case this isn't clear, I'm trying to create a drop-down menu with every possible combination of the following terms: [i]sad[/i], [i]tearful [/i]and [i]desperate[/i]. So at the moment I've got my multidimensional array displaying each array with the various combinations, I just need to create the options to go into my <select> form field, like so:<select> <option>sad,</option> <option>tearful,</option> <option>sad,tearful,</option> <option>desperate,</option> <option>sad,desperate,</option> <option>tearful,desperate,</option> <option>sad,tearful,desperate,</option></select>Anyone help? Link to comment https://forums.phpfreaks.com/topic/26826-linearise-a-multidimensional-array-into-single-array-of-strings/ Share on other sites More sharing options...
Jenk Posted November 10, 2006 Share Posted November 10, 2006 So you want, for example:[code]<?php$array = array( 'happy', array( 'sad', 'unhappy'), 'angry', 'amused');?>[/code]To become:[code]<?php$array = array( 'happy', 'sad,unhappy', 'angry', 'amused');?>[/code]? Link to comment https://forums.phpfreaks.com/topic/26826-linearise-a-multidimensional-array-into-single-array-of-strings/#findComment-122669 Share on other sites More sharing options...
discusster Posted November 10, 2006 Author Share Posted November 10, 2006 Hi Jenk,Many thanks for the reply. Are you suggesting using [i]implode[/i]? I was messing about with that but couldn't get what I was looking for. If that's the way to do it then am happy with that if someone can show me the way how.Basically I've got six arrays, each one holds a combination of values which I'd like to join together as a string; so with reference to the example at http://discusster.textdriven.com/sandbox/index.php the ideal outcome would be an array like this:[code]<?php$array = array{ "sad,", "tearful,", "sad,tearful,", "desperate,", "sad,desperate,", "tearful,desperate,", "sad,tearful,desperate,",};?>[/code]NB. To avoid any confusion, the values already all have a comma after each term - it's coming from a database like this) Link to comment https://forums.phpfreaks.com/topic/26826-linearise-a-multidimensional-array-into-single-array-of-strings/#findComment-122676 Share on other sites More sharing options...
Jenk Posted November 10, 2006 Share Posted November 10, 2006 Yes, implode:[code]<?phpforeach ($array as $key => $val){ if (is_array($val)) $array[$key] = implode('', $val);}?>[/code] Link to comment https://forums.phpfreaks.com/topic/26826-linearise-a-multidimensional-array-into-single-array-of-strings/#findComment-122694 Share on other sites More sharing options...
discusster Posted November 10, 2006 Author Share Posted November 10, 2006 Jenk,Thank you very much, it works a treat! :) Link to comment https://forums.phpfreaks.com/topic/26826-linearise-a-multidimensional-array-into-single-array-of-strings/#findComment-122703 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.