Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

PHP - help with placing element in array please

  • 05-08-2004 05:06PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 6,315 ✭✭✭ballooba


    Edited to make the problem more clear.


  • Registered Users, Registered Users 2 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
Advertisement