Jump to content

using file_get_contents() to put data into an array()


xwielder

Recommended Posts

In my "skill_array.txt" file I have:

0 => '-skill-'

,1 => 'Assassin (basic)'

,2 => 'Assassin (intermediate)'

,3 => 'Assassin (advanced)'

,4 => 'Hand to Hand (basic)'

,5 => 'Hand to Hand (intermediate)'

,6 => 'Hand to Hand (advanced)'

,7 => 'Martial Arts (basic)'

,8 => 'Martial Arts (advanced)'

,9 => 'Martial Arts (intermediate)'


In my "file.php" file, I have:
<?php

     $selects = array(file_get_contents("skill_array.txt"));

     print("<pre>");

     print_r($selects);

     print("</pre>");



     /* This is what I get */



     /*

     Array

     (

         [0] => 0 => '-skill-'

     ,1 => 'Assassin (basic)'

     ,2 => 'Assassin (intermediate)'

     ,3 => 'Assassin (advanced)'

     ,4 => 'Hand to Hand (basic)'

     ,5 => 'Hand to Hand (intermediate)'

     ,6 => 'Hand to Hand (advanced)'

     ,7 => 'Martial Arts (basic)'

     ,8 => 'Martial Arts (advanced)'

     ,9 => 'Martial Arts (intermediate)'

     )

     */

?>


Why doesn't it parse it like I'm wanting?  I want the print_r to show:

/*

Array

(

    [0] => -skill-

    [1] => Assassin (basic)

    [2] => Assassin (intermediate)

    [3] => Assassin (advanced)

    [4] => Hand to Hand (basic)

    [5] => Hand to Hand (intermediate)

    [6] => Hand to Hand (advanced)

    [7] => Martial Arts (basic)

    [8] => Martial Arts (advanced)

    [9] => Martial Arts (intermediate)

)

*/

It doesn't work like that.

 

You have to save the array as a readable format for PHP, for example, use json_encode.

 

Like this:

 

<?PHP

  $skillsArray = array(0 => '-skill-',
                       1 => 'Assassin (basic)',
                       2 => 'Assassin (intermediate)',
                       3 => 'Assassin (advanced)',
                       4 => 'Hand to Hand (basic)',
                       5 => 'Hand to Hand (intermediate)',
                       6 => 'Hand to Hand (advanced)',
                       7 => 'Martial Arts (basic)',
                       8 => 'Martial Arts (advanced)',
                       9 => 'Martial Arts (intermediate)'
                      );
 
  $skillsArray = json_encode($skillsArray);

  echo '<pre>';
  print_r($skillsArray);
 
?>

 

Then use file_put_contents, to put the $skillsArray content in the "skills_array.txt" file.

 

Then when retrieving the data, you do something like:

 

<?PHP

  $content = file_get_contents('skills_array.txt');
  $content = json_decode($content, TRUE);
 
  echo '<pre>';
  print_r($content);
 
?>

Fair enough.  I've opted to do the following:

 

My "skill_array.txt" file is now "skill_array.php".

Here is that file now:

 

<?php
$selects = array(0 => '-skill-'
,1 => 'Assassin (basic)'
,2 => 'Assassin (intermediate)'
,3 => 'Assassin (advanced)'
,4 => 'Hand to Hand (basic)'
,5 => 'Hand to Hand (intermediate)'
,6 => 'Hand to Hand (advanced)'
,7 => 'Martial Arts (basic)'
,8 => 'Martial Arts (advanced)'
,9 => 'Martial Arts (intermediate)'
);
?>

 

And now in my "file.php" file:

 

<?php include("skill_array.php"); ?>

 

Everything works great.  I'm not sure why "$selects = array(file_get_contents("skill_array.txt"));" doesn't, as you say, "work like that", but if it doesn't it doesn't. 

 

Thanks for your help and advice!

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.