adv Posted January 7, 2009 Share Posted January 7, 2009 hello i`m trying to grab 2 kinds of texts from a file in the file grab.txt i have firstfile:secondfile thirdfile:fourfile each one separated by a new line and in php i`m trying this <?php $file=file('aa.txt'); foreach($file as $files){ $split=explode("\n",$files); $split=explode(':',$files); /* echo '<pre>'; print_r($split); echo '</pre>'; */ } ?> how do i access $split[0] and $split[1] to rise automaticaly? i mean when i use them $split[0]=firstfile $split[1]=secondfile $split[0]=thirdfile $split[1]=fourfile something like this :| Quote Link to comment https://forums.phpfreaks.com/topic/139877-solved-array-question/ Share on other sites More sharing options...
rhodesa Posted January 7, 2009 Share Posted January 7, 2009 <?php $lines=file('aa.txt'); for($n=0;$n < count($lines);$n+=2){ list($firstfile,$secondfile) = explode(':',$lines[$n]); $n++; list($thirdfile,$fourthfile) = explode(':',$lines[$n+1]) } ?> Quote Link to comment https://forums.phpfreaks.com/topic/139877-solved-array-question/#findComment-731810 Share on other sites More sharing options...
Boo-urns Posted January 7, 2009 Share Posted January 7, 2009 If that file is large, I'd take out the "count($lines)" in the for loop and create it as a variable prior to the loop as the for loop runs that every time. Quote Link to comment https://forums.phpfreaks.com/topic/139877-solved-array-question/#findComment-731891 Share on other sites More sharing options...
adv Posted January 7, 2009 Author Share Posted January 7, 2009 thanks Aaron but it get an error here list($thirdfile ,$fourfile) = explode(':',$lines[$n+1]); Notice: Undefined offset: 2 in C:\wamp\www\tst\aa.php on line 8 Notice: Undefined offset: 1 in C:\wamp\www\tst\aa.php on line 8 i`ve tried something like this and it works,but is it good? for($n=0;$n<count($lines);$n=10){ $res=list($a,$b)=explode(':',$lines[$n]); echo '<pre>'; print_r($res); echo '</pre>'; $n++; $res1=list($c,$d)=explode(':',$lines[$n]); echo '<pre>'; print_r($res1); echo '</pre>'; } this is what is shows Array ( [0] => firstfile [1] => secondfile ) Array ( [0] => thirdfile [1] => fourfile ) if works just for 2 lines it must work if i have even 10 lines in the aa.txt Quote Link to comment https://forums.phpfreaks.com/topic/139877-solved-array-question/#findComment-731896 Share on other sites More sharing options...
rhodesa Posted January 7, 2009 Share Posted January 7, 2009 the list() syntax is just a way of assigning an array to individual variables. if you don't want to use it, just get rid of it: echo '<pre>'; for($n=0;$n<count($lines);$n++){ $res=explode(':',$lines[$n]); $n++; $res1=explode(':',$lines[$n]); print_r($res); print_r($res1); } echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/139877-solved-array-question/#findComment-731905 Share on other sites More sharing options...
premiso Posted January 7, 2009 Share Posted January 7, 2009 If you want the list but do not want the notices, this would solve that problem. for($n=0;$n<count($lines);$n=10){ if (stristr($lines[$n], ':') !== false) { $res=list($a,$b)=explode(':',$lines[$n]); echo '<pre>'; print_r($res); echo '</pre>'; } $n++; if (stristr($lines[$n], ':') !== false) { $res1=list($c,$d)=explode(':',$lines[$n]); echo '<pre>'; print_r($res1); echo '</pre>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/139877-solved-array-question/#findComment-731907 Share on other sites More sharing options...
adv Posted January 7, 2009 Author Share Posted January 7, 2009 thanks alot both Quote Link to comment https://forums.phpfreaks.com/topic/139877-solved-array-question/#findComment-732012 Share on other sites More sharing options...
adv Posted January 10, 2009 Author Share Posted January 10, 2009 another quick question in aa.txt i have spring:notcool summer:cool winter:bad and the php file: <?php echo '<pre>'; $aa1=file('aa.txt'); foreach($aa1 as $aaaa){ $expl=explode(chr(58),$aaaa); echo $expl[0].'<br /><br />'; // shows first values /* spring summer winter */ echo $expl[1]; // shows second values /* notcool cool bad */ } echo '</pre>'; ?> there is not problem with it my question is.. can it be done with for() ? to be able to access it just the same and if it can can u please show me an example ... i just want to see how its done Quote Link to comment https://forums.phpfreaks.com/topic/139877-solved-array-question/#findComment-734363 Share on other sites More sharing options...
adv Posted January 11, 2009 Author Share Posted January 11, 2009 anybody? :| Quote Link to comment https://forums.phpfreaks.com/topic/139877-solved-array-question/#findComment-734627 Share on other sites More sharing options...
premiso Posted January 11, 2009 Share Posted January 11, 2009 <?php $cnt = count($aa1); for ($i=0;$i<$cnt;$i++){ $expl=explode(chr(58),$aa1[$i]); You would just access the array element index with $i Quote Link to comment https://forums.phpfreaks.com/topic/139877-solved-array-question/#findComment-734689 Share on other sites More sharing options...
adv Posted January 11, 2009 Author Share Posted January 11, 2009 wow thanks alot premiso Quote Link to comment https://forums.phpfreaks.com/topic/139877-solved-array-question/#findComment-734704 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.