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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP - help with placing element in array please

  • 05-08-2004 5: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