shan2batman Posted May 29, 2016 Share Posted May 29, 2016 up votedown votefavorite I've been scraching my head for hours over this problem. I'm basically trying to fetch values from a loop inside a function to pass it to another foreach loop to get the desired results. And it is not working as I intend to. please, point me in the right direction. Here is the code: function ff($s) { $project=""; foreach ($s as $i=> $r ){ $r["friend_one"] == $_SESSION['uname'] ? $friends[]= $r["friend_two"] : $friends[] = $r["friend_one"]; $friend=$friends[$i]; $totalids=$project->totalids($_SESSION['uname'],$friend); } return $totalids; } $totalid= ff($f); print_r($totalid); foreach ($totalid as $v){ $id=$v['user_id']; //other logic to get desired result } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 29, 2016 Share Posted May 29, 2016 You didn't tell us what the problem is, but why don't you look at this line: $friend = $friends[$i] You will always get the last key value in your loop. Is that what you want? Quote Link to comment Share on other sites More sharing options...
maxxd Posted May 30, 2016 Share Posted May 30, 2016 You should be getting a good number of errors using this code, because quite honestly it doesn't make any sense at all. Add the error reporting code in ginerjm's signature to the top of your file. First and foremost, that's not how you use a ternary statement. You want something more along the lines of this: $friend[] = ($r['friend_one'] == $_SESSION['uname']) ? $r['friend_two'] : $r['friend_one']; Second, you're creating an empty string called $project, then attempting to access the totalids() method of that empty string, which obviously doesn't exist. If $project is supposed to be an object with the valid method totalids(), you'll need to pass that object in to the function in order to use it. Even if you do pass in the correct object, you're overwriting the value of $totalids on every loop iteration, which may not be what you intend to do. $project = ""; ... $totalids = $project->totalids($_SESSION['uname'], $friend); Third, you're assigning a value ($r['friend_one'] or $r['friend_two']) to the next numeric index in the $friend array, then immediately trying to access that data at a specific numeric index, which may or may not exist. If the specified index does exist, it looks like it would be completely accidental. The print_r() statement shouldn't be printing anything at all right now. Quote Link to comment 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.