themistral Posted January 20, 2011 Share Posted January 20, 2011 Hi guys, I have a multidimensional array stored as a string in a database. How can I retrieve this information and make it act as a string? $query = mysql_query("SELECT string FROM database"); $result = mysql_fetch_array($query); $arr = array(); $arr[] = $result[string]; Would this have the required effect to make a string act as a multidimensional array? Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 20, 2011 Share Posted January 20, 2011 The answer looks like it would be - No. If you posted an actual example of what your data is and what you are trying to produce, I'm sure someone could probably help you. Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/#findComment-1162518 Share on other sites More sharing options...
shlumph Posted January 20, 2011 Share Posted January 20, 2011 I have a multidimensional array stored as a string in a database. You should either normalize your database, or serialize the array before pushing it into the database, and unserialize the array after pulling it from the database. Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/#findComment-1162519 Share on other sites More sharing options...
themistral Posted January 20, 2011 Author Share Posted January 20, 2011 This is an example of the string in the database a:1:{s:7:"options";a:3:{s:11:"Yellow/Pink";s:11:"Yellow/Pink";s:12:"Yellow/Green";s:12:"Yellow/Green";s:12:"Yellow/Multi";s:12:"Yellow/Multi";}} I need to access the values of "Yellow/Green", "Yellow/Pink" and "Yellow/Multi" to populate a drop down list. Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/#findComment-1162520 Share on other sites More sharing options...
themistral Posted January 20, 2011 Author Share Posted January 20, 2011 Hi shlumph, I can't do anything about the way the data is input into the database - it's a CMS... Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/#findComment-1162521 Share on other sites More sharing options...
shlumph Posted January 20, 2011 Share Posted January 20, 2011 It looks like it's already serialized, you just need to unserialize it now http://us2.php.net/unserialize Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/#findComment-1162527 Share on other sites More sharing options...
themistral Posted January 20, 2011 Author Share Posted January 20, 2011 Hi shlumph, Doh! I'll take a look - thanks for the link Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/#findComment-1162529 Share on other sites More sharing options...
themistral Posted January 21, 2011 Author Share Posted January 21, 2011 OK, I've got my multidimensional array: Array ( [options] => Array ( [Animal Print] => Animal Print [Animal Print/Multi] => Animal Print/Multi [Animal Print/Pink] => Animal Print/Pink [black] => Black ) ) I can print my array fine. $data = unserialize($result['colours']); echo '<pre>'; print_r($data); echo '</pre>'; I can reference a single item using echo $data['options']['Animal Print']; But I can't seem to reference a single item using echo $data[0][0]; Can anyone help me out? Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/#findComment-1162901 Share on other sites More sharing options...
themistral Posted January 21, 2011 Author Share Posted January 21, 2011 Ok, I'm understanding that it's because it's an associative array that I can't reference as echo $data[0][0]; Is there a way to turn this into a non-associative array? Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/#findComment-1162908 Share on other sites More sharing options...
salathe Posted January 21, 2011 Share Posted January 21, 2011 themistral, you could use a function like the one below to turn the associative nested array into a plain nested array. function unassoc(&$array) { // Re-index the array $array = array_values($array); // Call unassoc recursively for sub-arrays foreach ($array as &$val) { if (is_array($val)) { unassoc($val); } } } // Use it like this unassoc($data); echo $data[0][0]; Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/#findComment-1162933 Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2011 Share Posted January 21, 2011 I've got to ask why you want a numerically indexed array? I suspect is it because you want to iterate over the data, in which case you would just iterate over the associative array. Given that the number of data elements could vary, the meaning of each element looks like it can be anything, and even the order of the elements could change, attempting to access a specific numerical element wouldn't necessarily give you the result you expect. Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/#findComment-1162935 Share on other sites More sharing options...
themistral Posted January 21, 2011 Author Share Posted January 21, 2011 I need to loop through the items to create a dropdown box. I have done it now - phew! $data = unserialize($result['colour']); $data2 = $data['options']; // options is the name of the first array foreach ($data2 as $value) { echo '<option value="'.$value.'">'.$value.'</option>'; } Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/#findComment-1163017 Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2011 Share Posted January 21, 2011 You can actually do the foreach() directly on the data array - foreach($data['options'] as $value){ Quote Link to comment https://forums.phpfreaks.com/topic/225082-multidimensional-arrays/#findComment-1163033 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.