implode problem

Discussion in 'PHP' started by Weirfire, May 11, 2007.

  1. #1
    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?
     
    Weirfire, May 11, 2007 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    print_r $my_array before the impode; sounds like it doesn't contain what you think it does.
     
    T0PS3O, May 11, 2007 IP
  3. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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!
     
    TwistMyArm, May 11, 2007 IP
  4. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #4
    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:
     
    Weirfire, May 11, 2007 IP
  5. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #5
    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.
     
    Weirfire, May 11, 2007 IP
  6. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try unsetting it first.
     
    T0PS3O, May 11, 2007 IP
    Weirfire likes this.
  7. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #7
    lol don't pretend you knew that all along! :p
     
    Weirfire, May 11, 2007 IP
  8. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I did. Was just testing you. It's the best to learn.
     
    T0PS3O, May 11, 2007 IP
  9. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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...
     
    TwistMyArm, May 11, 2007 IP
  10. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #10
    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?
     
    Weirfire, May 11, 2007 IP
  11. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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.
     
    TwistMyArm, May 11, 2007 IP
    Weirfire likes this.