Jump to content

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


xwielder
Go to solution Solved by PaulRyan,

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)

)

*/
Link to comment
Share on other sites

  • Solution

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);
 
?>
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.