Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

How retrieve specific duplicate array values with PHP

$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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...