zaphodbeeb Posted September 28, 2008 Share Posted September 28, 2008 Hi, I have the following bit of code: <?php $Colors="a90d3d[6|ea0e0e[7|f66313[8|fae500[9|fad495[10|ad1dba[11|f5729c[12|00646c[13|00c3d9[14|312c54[15|6400b0[16|9d8edf[18|070f98[17|4873b8[19|7d7f71[20|007e6d[21|a3de22[22|573222[23|a25d18[24|a77751[25|bd987b[26|99CCFF[9999|"; $colors_array = explode("|",$Colors); $len = sizeof($colors_array); $array_details=""; foreach($colors_array as $key) { $bit = explode("[",$key); $a = $bit[0]; $b = $bit[1]; if (!empty($b)) { $array_details .= "$b => \"$a\" ,"; } } $array_details = trim(substr($array_details,0,-1)); #echo $array_details; $c = array($array_details); print_r($c); ?> Now, the problem is that when I used the print_r($c) above I get: Array ( [0] => 6 => "a90d3d" ,7 => "ea0e0e" ,8 => "f66313" ,9 => "fae500" ,10 => "fad495" ,11 => "ad1dba" ,12 => "f5729c" ,13 => "00646c" ,14 => "00c3d9" ,15 => "312c54" ,16 => "6400b0" ,18 => "9d8edf" ,17 => "070f98" ,19 => "4873b8" ,20 => "7d7f71" ,21 => "007e6d" ,22 => "a3de22" ,23 => "573222" ,24 => "a25d18" ,25 => "a77751" ,26 => "bd987b" ,9999 => "99CCFF" ) Note the initial Array([0] => bit. I don't want this (and have no idea why it's getting put in there). See where in the initial bit of code I have commented out the statement that echoes the array_details string that I build: #echo $array_details; If I uncomment that, and copy and paste the resultant string to the code i.e. $c = array($array_details); changes to $c = array(6 => "a90d3d" ,7 => "ea0e0e" ,8 => "f66313" ,9 => "fae500" ,10 => "fad495" ,11 => "ad1dba" ,12 => "f5729c" ,13 => "00646c" ,14 => "00c3d9" ,15 => "312c54" ,16 => "6400b0" ,18 => "9d8edf" ,17 => "070f98" ,19 => "4873b8" ,20 => "7d7f71" ,21 => "007e6d" ,22 => "a3de22" ,23 => "573222" ,24 => "a25d18" ,25 => "a77751" ,26 => "bd987b" ,9999 => "99CCFF"); and then print out the array using print_r($c) I get exactly what I need, i.e. Array ( [6] => a90d3d [7] => ea0e0e [8] => f66313 [9] => fae500 [10] => fad495 [11] => ad1dba [12] => f5729c [13] => 00646c [14] => 00c3d9 [15] => 312c54 [16] => 6400b0 [18] => 9d8edf [17] => 070f98 [19] => 4873b8 [20] => 7d7f71 [21] => 007e6d [22] => a3de22 [23] => 573222 [24] => a25d18 [25] => a77751 [26] => bd987b [9999] => 99CCFF ) Can anyone point out what I'm doing wrong. You'd never think it to look at the code, but I've actually been using PHP for years - but this has me stumped... ??? Quote Link to comment https://forums.phpfreaks.com/topic/126121-solved-problem-with-creating-an-array/ Share on other sites More sharing options...
CroNiX Posted September 28, 2008 Share Posted September 28, 2008 What if you did this: <?php $array_details = array(); //changed this foreach($colors_array as $key) { $bit = explode("[",$key); $a = $bit[0]; $b = $bit[1]; if (!empty($b)) { $array_details[$b] = $a; // added this //$array_details .= "$b => \"$a\" ,"; } } Quote Link to comment https://forums.phpfreaks.com/topic/126121-solved-problem-with-creating-an-array/#findComment-652144 Share on other sites More sharing options...
zaphodbeeb Posted September 28, 2008 Author Share Posted September 28, 2008 You know, I really thought I'd tried that. I am an idiot [although it's 20 to 3 in the morning here so...] Thank you so so so much for this - it's working perfectly. -Z Quote Link to comment https://forums.phpfreaks.com/topic/126121-solved-problem-with-creating-an-array/#findComment-652146 Share on other sites More sharing options...
CroNiX Posted September 28, 2008 Share Posted September 28, 2008 NP Quote Link to comment https://forums.phpfreaks.com/topic/126121-solved-problem-with-creating-an-array/#findComment-652149 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.