Jump to content

Recommended Posts

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... ???

Link to comment
https://forums.phpfreaks.com/topic/126121-solved-problem-with-creating-an-array/
Share on other sites

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\" ,";
	}
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.