I just wrote a few lines of very basic code For example the code would be something along the lines of; $my_array = array("item 1","item 2"); $my_string = implode(",",$my_array); When I print $my_string it's giving me something like item 1,item 2,item 1,item 2 Any ideas what mistake I've made?
I agree... plus, it doesn't help that you've basically said "I wrote a few lines of code, which were something like this and the result was something like this". God forbid you give us the actual code and the actual output!
lol the reason I've not given you the actual code is; 1) It's on the backend of my oscommerce site and basically I'd have to paste in thousands of lines of code which wouldn't be very helpful in determining what the problem was. 2) I'm sending an error from a select menu which is then changed into a string. It is then posted through a hidden input field where it is processed for the database. 3) I can't show the actual web page because my client wouldn't be too keen about everyone under the sun being able to access the backend pages of his ecommerce website. Thanks T0PS. I've already tried var_dump on the array and it gives the 2 values as expected. It's when I use the following code to implode it into a string it seems to change into 4 values; ..... $j=0; foreach($_POST['extra_images'] as $field){ $extra_images[] = $field; print $extra_images[$j]; // prints all the values in the array $j++; } if(count($extra_images)>1){ $extra_image = implode(",",$extra_images); print "<br />" . $extra_image; // prints all the values twice } ....... PHP:
I've just inserted unset($extra_images); PHP: before the for loop and it's sorted the problem. I can't understand why this happened but it was obviously entering the values twice into the array but considering 2 lines before it had 2 values, it printed out 4 values in the array.
My apologies. I guess your idea of 'a few lines of very basic code' and my idea of same are different Doesn't osCommerce require register_globals to be on? So you have $_POST['extra_images'] (which automatically defines a variable called $extra_images with whatever is in $_POST['extra_images']) and then you're essentially looping through the POST, adding the information again, as you're adding it to a variable called $extra_images. That's the problem: the extra_images variable already exists with all the information you're about to put into it...
I agree that this was the thing that was causing the problem but why did the previous lines of code only print out 2 values?
Do you mean with this block of code? $j=0; foreach($_POST['extra_images'] as $field){ $extra_images[] = $field; print $extra_images[$j]; // prints all the values in the array $j++; } PHP: If so, it's pretty simple. Say for example you have three items in your $_POST['extra_images']. When you get to that code block, $extra_images is already an array of three items. It looks something like this: $extra_images = array( 0 => 'item1', 1 => 'item2', 2 => 'item3' ); You loop through the post, adding to the end of the array. So: $extra_images[] = $field; changes $extra_images to look like: $extra_images = array( 0 => 'item1', 1 => 'item2', 2 => 'item3', 3 => 'item1' ); Then "print $extra_images[$j];" prints the first item... the item that was added by register_globals, not by you. By the time your loop is complete, you have added all of your items to the end of the array and you have printed out all of the items that were in the array to begin with, but you never end up printing any value that you added yourself. You could 'prove' this by changing: $extra_images[] = $field; to: $extra_images[] = $field . 'added_by_me'; Your print call would never show a string with that suffix that you added. If, instead of: $extra_images[] = $field; you used: $extra_images[$j] = $field; you probably wouldn't have the problem as it would be overwriting the values in the first place.