xwielder Posted March 25, 2013 Share Posted March 25, 2013 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) ) */ Quote Link to comment https://forums.phpfreaks.com/topic/276140-using-file_get_contents-to-put-data-into-an-array/ Share on other sites More sharing options...
Solution PaulRyan Posted March 25, 2013 Solution Share Posted March 25, 2013 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/276140-using-file_get_contents-to-put-data-into-an-array/#findComment-1420980 Share on other sites More sharing options...
xwielder Posted March 25, 2013 Author Share Posted March 25, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/276140-using-file_get_contents-to-put-data-into-an-array/#findComment-1420981 Share on other sites More sharing options...
PaulRyan Posted March 25, 2013 Share Posted March 25, 2013 It's because it isn't interpreted as an array when it's in plain text. PHP assumes it just it's plain text and doesn't convert it to an array as you want it too. Glad you fixed your problem Quote Link to comment https://forums.phpfreaks.com/topic/276140-using-file_get_contents-to-put-data-into-an-array/#findComment-1420983 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.