Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP - help with placing element in array please

Options
  • 05-08-2004 6:06pm
    #1
    Registered Users Posts: 6,315 ✭✭✭


    This code is not doing what i want it to do.

    I have a CSV file with 10 rows.

    I explode this into an array with 10 elements.

    I want to place a new element at the position '$index' (integer).

    The item is actually put at the end of the array.

    What's the Jack 'n' Nory?

    [PHP]function writeEvent($title,$bannerName,$splashName,$link)
    {
    extract($GLOBALS);
    $index = addEvent();
    $filename = 'events.csv';
    $fp = fopen($filename, "r");
    $eventsData = fread($fp, filesize($filename));
    fclose($fp);

    $eventsArray = explode("/n", $eventsData);
    $tempArray = $eventsArray;
    $insert = $title.",".$bannerName.",".$splashName.",".$link.",0";
    $tempArray[$index] = $insert;

    $arrayString = implode("\n",$tempArray);
    $filename = 'events.csv';
    $fd = fopen($filename, "w");
    fwrite($fd, $arrayString);
    fclose($fd);
    print_r($tempArray);
    }[/PHP]
    Array
    (
    [0] => Test2,ger_banner.jpg,torture.jpg,http://entslink.com,0
    Test2,ger_banner.jpg,torture.jpg,http://entslink.com,1
    Test2,ger_banner.jpg,torture.jpg,http://entslink.com,2
    Test2,ger_banner.jpg,torture.jpg,http://entslink.com,1
    Test2,ger_banner.jpg,torture.jpg,http://entslink.com,0
    Test2,ger_banner.jpg,torture.jpg,http://entslink.com,0
    Test2,ger_banner.jpg,torture.jpg,http://entslink.com,0
    Look for this,ger_banner.jpg,torture.jpg,http://entslink.com,0
    Test 3,ger_banner.jpg,torture.jpg,http://entslink.com,0
    [1] => ,ger_banner.jpg,torture.jpg,http://,0
    )


Comments

  • Registered Users Posts: 6,315 ✭✭✭ballooba


    Edited to make the problem more clear.


  • Registered Users Posts: 6,315 ✭✭✭ballooba


    Surprised no one spotted what was wrong.

    Had a "/n" for new line character instead of "\n".

    Array actually only had one element in it because explode had wrong parameter.

    P.s. It feels so stupid answering your own question.


Advertisement