Minzer Posted April 19, 2021 Share Posted April 19, 2021 (edited) <?php class CsvToArray { /* global $file; $file = '../Datafeeds/baseball_all.csv'; */ public function data() { $file = '../Datafeeds/baseball_all.csv'; $fileName = fopen($file,'r+'); $all_rows = array(); $header = fgetcsv($fileName); while ($row = fgetcsv($fileName)) { $games[] = array_combine($header, $row); } //print_r($games); print_r($games[64]["Home Team"]); } } $game = new CsvToArray(); $game->data(); ?> csv: https://anonymousfiles.io/vSubWgPY/ How can I get specific array values outside a class or function? About to try "File put contents" I think. That should work right? So I thought the last couple times. Or what would you suggest?. Overwritten previous attempts to share =[ Next up is seeing if I can change $file to $var to change it from other locations. Should be easy I reckon. Edited April 19, 2021 by Minzer Quote Link to comment https://forums.phpfreaks.com/topic/312505-specific-array-value-from-outside-class-or-function/ Share on other sites More sharing options...
kicken Posted April 19, 2021 Share Posted April 19, 2021 (edited) 2 hours ago, Minzer said: print_r($games[64]["Home Team"]); instead of that, make your data function return your games variable. return $games; Then you can assign it when you call your data function and access whatever you want. $game = new CsvToArray(); $games = $game->data(); echo $games[64]['Home Team']; Quote Next up is seeing if I can change $file to $var to change it from other locations. Make it a function argument. function data($file){ $fileName = fopen($file,'r+'); ... Edited April 19, 2021 by kicken 1 Quote Link to comment https://forums.phpfreaks.com/topic/312505-specific-array-value-from-outside-class-or-function/#findComment-1585955 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.