-2
$array = array(
array(
    'id' => 1,
    'name' => 'John Doe',
    'upline' => 0
),
array(
    'id' => 2,
    'name' => 'Jerry Maxwell',
    'upline' => 1
),
array(
    'id' => 3,
    'name' => 'Roseann Solano',
    'upline' => 1
),
array(
    'id' => 4,
    'name' => 'Joshua Doe',
    'upline' => 1
),
array(
    'id' => 5,
    'name' => 'Ford Maxwell',
    'upline' => 1
),
array(
    'id' => 6,
    'name' => 'Ryan Solano',
    'upline' => 1
),
array(
    'id' =>7,
    'name' => 'John Mayer',
    'upline' => 3
),

); I want to make a function like:

function get_downline($userid,$users_array){
}

Then i want to return an array of all the user's upline key with the value as $userid. I hope anyone can help. Please please...

2
  • 4
    You have no dupes in your sample? What makes for a dupe? Commented Apr 9, 2012 at 0:35
  • I want retrieve an array of users with the same upline values Commented Apr 9, 2012 at 0:52

3 Answers 3

3

You could do it with a simple loop, but let's use this opportunity to demonstrate PHP 5.3 anonymous functions:

function get_downline($id, array $array) {
    return array_filter($array, function ($i) use ($id) { return $i['upline'] == $id; });
}

BTW, I have no idea if this is what you want, since your question isn't very clear.

Sign up to request clarification or add additional context in comments.

2 Comments

basicall i just want to retrieve an array of all users with the value '1' for their upline key.
If that's based on the $id parameter for this function, then the above does exactly that.
2

If you need do search thru yours array by $id:

foreach($array as $value)
{
   $user_id = $value["id"];
   $userName = $value["name"];
   $some_key++;

   $users_array[$user_id] = array("name" => $userName, "upline" => '1');
}

function get_downline($user_id, $users_array){
     foreach($users_array as $key => $value)
     {
         if($key == $user_id)
         {
              echo $value["name"];
              ...do something else.....
         }
     }
}

or to search by 'upline':

function get_downline($search_upline, $users_array){
         foreach($users_array as $key => $value)
         {
             $user_upline = $value["upline"];
             if($user_upline == $search_upline)
             {
                  echo $value["name"];
                  ...do something else.....
             }
         }
    }

Comments

1

Code :

function get_downline($userid,$users_array) 
{
    $result = array();

    foreach ($users_array as $user)
    {
        if ($user['id']==$userid)
            $result[] = $user['upline'];
    }
    return result;
}
?>

Example Usage :

get_downline(4,$array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.