john_doemoor Posted June 12, 2012 Share Posted June 12, 2012 Hi guys, My problem is, when I use array i created in previous function, it "forgets" the values I already inserted. How can i make array "remember" the values and not to delete them? THANKS! Quote Link to comment https://forums.phpfreaks.com/topic/264046-using-array-from-previous-function/ Share on other sites More sharing options...
Nyuszer Posted June 12, 2012 Share Posted June 12, 2012 Use global $your_array;[/cods] at the beginning of the function Quote Link to comment https://forums.phpfreaks.com/topic/264046-using-array-from-previous-function/#findComment-1353166 Share on other sites More sharing options...
KevinM1 Posted June 12, 2012 Share Posted June 12, 2012 Use global $your_array;[/cods] at the beginning of the function No, don't do this. In fact, never use the 'global' keyword at all, for anything. OP, why don't you show us what you're trying to do (in code)? Quote Link to comment https://forums.phpfreaks.com/topic/264046-using-array-from-previous-function/#findComment-1353167 Share on other sites More sharing options...
twistedvengeance Posted June 12, 2012 Share Posted June 12, 2012 perhaps trying doing public $yourarray = array($_POST); Not sure if that would work or not. Quote Link to comment https://forums.phpfreaks.com/topic/264046-using-array-from-previous-function/#findComment-1353171 Share on other sites More sharing options...
marcus Posted June 12, 2012 Share Posted June 12, 2012 perhaps trying doing public $yourarray = array($_POST); Not sure if that would work or not. $_POST is global by default. Quote Link to comment https://forums.phpfreaks.com/topic/264046-using-array-from-previous-function/#findComment-1353184 Share on other sites More sharing options...
Mahngiel Posted June 12, 2012 Share Posted June 12, 2012 You could do two things, depending on use. Either return a value, or pass the value to another function. function one() { $array = array('one', 'two'); return $array; } $test = one(); // OR function one() { $array = array('one', 'two'); two($array); } function two( $data = array() ) { empty( $data ) return FALSE; ... some code ... } Quote Link to comment https://forums.phpfreaks.com/topic/264046-using-array-from-previous-function/#findComment-1353223 Share on other sites More sharing options...
twistedvengeance Posted June 12, 2012 Share Posted June 12, 2012 You could also use classes, I think. <?php class Beginning{ function Opening(){ ['your code'] } } class End extends Beginning{ function End(){ ['ending code'] } } Quote Link to comment https://forums.phpfreaks.com/topic/264046-using-array-from-previous-function/#findComment-1353250 Share on other sites More sharing options...
KevinM1 Posted June 12, 2012 Share Posted June 12, 2012 You could also use classes, I think. <?php class Beginning{ function Opening(){ ['your code'] } } class End extends Beginning{ function End(){ ['ending code'] } } That would be a horrible use of classes. Again, john_doemoor, show the code you're having issues with. Quote Link to comment https://forums.phpfreaks.com/topic/264046-using-array-from-previous-function/#findComment-1353262 Share on other sites More sharing options...
john_doemoor Posted June 13, 2012 Author Share Posted June 13, 2012 So my issue is, i need to use: public function parseLogLines($logLines, $householdId) { $logLinesArray = array(); foreach(preg_split("/\n|\r/", $logLines) as $logLine) { if (preg_match("/$householdId/", $logLine) > 0) { $logLineArray = preg_split("/,/",$logLine, 2, PREG_SPLIT_NO_EMPTY); $index = $logLineArray[0]; $content = $logLineArray[1]; $logLinesArray[$index] = $content; } } return $logLinesArray; } at next function, and at next function i parse more loglines with this function, and i need all of the loglines stay in an array. Quote Link to comment https://forums.phpfreaks.com/topic/264046-using-array-from-previous-function/#findComment-1353400 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.