Jump to content

Specific Array Value from outside Class or Function?


Recommended Posts

<?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 by Minzer
Link to post
Share on other sites
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 by kicken
  • Like 1
Link to post
Share on other sites

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.