Jump to content

[SOLVED] array question


adv

Recommended Posts

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

:|

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/139877-solved-array-question/
Share on other sites

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 :(

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>';

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>';
}
}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.