2012-04-09 91 views
-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 
), 

); 我要让这样的函数:如何使用PHP检索特定数组重复值

function get_downline($userid,$users_array){ 
} 

然后我想返回与值$用户ID用户的所有上线键的数组。我希望任何人都可以帮忙。请请...

+4

你有你的样品中没有受骗者?是什么让一个愚蠢的? – 2012-04-09 00:35:32

+0

我想要检索具有相同上线值的用户数组 – johndavedecano 2012-04-09 00:52:21

回答

2

如果你需要做的的$ 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..... 
     } 
    } 
} 

或 '上线' 搜索:

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..... 
      } 
     } 
    } 
3

你可以用一个简单的循环做到这一点,但让我们利用这个机会来证明PHP 5.3匿名函数:

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

顺便说一句,我不知道如果这是你想要的东西,因为你的问题不是很清楚。

+0

basicall我只想检索值为'1'的上线键的所有用户的数组。 – johndavedecano 2012-04-09 01:11:33

+0

如果这是基于这个函数的'$ id'参数,那么上面就是这样。 – deceze 2012-04-09 01:17:33

1

代码:

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

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

用法示例:

get_downline(4,$array);