clone line in array with array_splice and then store new array
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
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.
精彩评论