Jump to content

i need array help :(


jamesxg1

Recommended Posts

Ok the code is short very! so i dont need to really explain what i am trying to do,

 

test.php

 

<?php

$textfile = 'datafiles.rtf';

$handle = fopen("$textfile", "r");

$contents = fread($handle, filesize($textfile));

fclose($handle);

echo nl2br($contents);
echo "<BR>";

$det = preg_split("/[\s,]+/", "$contents");



print_r($det);

?>

 

 

write.php

 

<?php

  foreach(glob("*.php") as $filename) {

    $contents .= $filename . '-' . filesize($filename) . "\r" ;

  }

  file_put_contents("datafiles.rtf", $contents);

?>

 

datafiles.rtf

 

connect.php-7722
db.php-1482
test.php-281
write.php-180

 

Screen Print

 

connect.php-7722

db.php-1482

test.php-281

write.php-180

 

Array ( [0] => connect.php-7722 [1] => db.php-1482 [2] => test.php-281 [3] => write.php-180 [4] => )

 

 

 

i need to turn $det into and array like so array($FILENAME => $SIZE) with all the details on the files in one array basically i need to remove the " - " from the .rtf file (implode, explode, split, str_replace) anything i am just really stuck here :(

 

James.

Link to comment
https://forums.phpfreaks.com/topic/166432-i-need-array-help/
Share on other sites

test.php

 

<?php

$textfile = 'datafiles.rtf';

$handle = fopen("$textfile", "r");

$contents = fread($handle, filesize($textfile));

fclose($handle);

echo nl2br($contents);
echo "<BR>";

$det = preg_split("/[\s,]+/", "$contents");



print_r($det);

?>

That can be written as

$contents = file('datafiles.rtf');
$contents = array_map('trim', $contents);

foreach($contents as $line)
{
     list($filename, $size) = explode('-', $line);

     $files[$filename] = $size;
}

echo '<pre>'.print_r($files, true).'</pre>';

Link to comment
https://forums.phpfreaks.com/topic/166432-i-need-array-help/#findComment-877673
Share on other sites

test.php

 

<?php

$textfile = 'datafiles.rtf';

$handle = fopen("$textfile", "r");

$contents = fread($handle, filesize($textfile));

fclose($handle);

echo nl2br($contents);
echo "<BR>";

$det = preg_split("/[\s,]+/", "$contents");



print_r($det);

?>

That can be written as

$contents = file('datafiles.rtf');
$contents = array_map('trim', $contents);

foreach($contents as $line)
{
     list($filename, $size) = explode('-', $line);

     $files[$filename] = $size;
}

echo '<pre>'.print_r($files, true).'</pre>';

 

hiya mate,

 

this seems to be very effective :),

 

but the only problem is, is that it is showing of the details of one file (one line)

 

this is the screen print

 

 

Array

(

    [connect.php] => 7722

db.php

)

Link to comment
https://forums.phpfreaks.com/topic/166432-i-need-array-help/#findComment-877680
Share on other sites

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.