Jump to content

how to fetch values from a loop in a function?


shan2batman

Recommended Posts

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 
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.