Welcome to WebmasterWorld Guest from 54.211.86.24

Forum Moderators: coopster & jatar k

php problem

shifting a array

   
2:32 am on Nov 12, 2002 (gmt 0)

10+ Year Member



say i have a array:

$lines[0]="info1";
$lines[1]="info2";
$lines[2]="info3";
$lines[3]="info4";

And i need to insert 3 (*) empty arrays on location $lines[1] (**) and everything under $lines[1] (**) it must also shift 3 (*) arrays down.

So the result would be:

$lines[0]="info1";
$lines[1]="";
$lines[2]="";
$lines[3]="";
$lines[4]="info2";
$lines[5]="info3";
$lines[6]="info4";

Is there a PHP command for this?

i couldn't find one that does exactly this. And i got a really weird error message with all weird characters, when i tried to make my own code to do this, and run it on the server.

The code i come up with was:(please don't laugh, i'm new to php, so my code is probably bad)

<?PHP
$filename = 'bestand.txt';
$lines = file($filename);

$startnummer="0";
$ruimte="1";
$teller="10";
while ($teller!=$startnummer)
{
$lines[$startnummer+$teller]=$lines[$startnummer+$teller+$ruimte];
$teller=$teller-"1";
}?>

But i got this really weird message.

Can anybody help me out?

(*) i need 3 lines and sometimes just 1 line.
(**) $lines[1] is just an example, it could also be $lines[100] where the 'blank' arrays need to be inserted.

5:37 am on Nov 12, 2002 (gmt 0)

10+ Year Member



You can achieve this using array_slice() [php.net] to split the original array in two, and array_merge() [php.net] to insert a third array containing the blank elements.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month