0

I have CSV text thats stored in the session array as csv. The lines are terminated by ### and fields terminated by %%.
I also have a line number of a line within that array which is CID, or the line which i want to clone.

I need to find that line within the csv array, splice it, and then update the session variable with the spliced array.

$data = $_SESSION['csv'];
$cid = $_POST['cid'];

$csvpre = explode("###", $data);

    foreach ( $csvpre AS $key => $value){

        $info = explode("%%", $value);

            if($info[0] == "$cid"){
                array_splice($csvpre, $cid, 0, $info);

                } 
    }

I dont think im doing it right, im on no sleep and this is getting confusing.

the goal is to let the user select which line to clone, and then perform this function, and have that line cloned in the csv session variable

1 Answer 1

1

From your code you have already found the correct line. I am not sure why do you need array_splice?

If you just need to clone the line, then $value is the line, just append it to your $_SESSION['csv']. If you need to modify something then reconstruct the line from $info array, append to csv afterward. If you need the lines in order then you need to reconstruct the csv in the loop.

BTW: "$cid" is no good, take the double quote off, $info[0] = $cid is just as good.

Sign up to request clarification or add additional context in comments.

1 Comment

well i need it to be placed into the csv array in the specific place, above or below the target line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.