Ejemplo n.º 1
0
prettyp($arr);
?>
    <hr />
    <p>Array pluck </p>
    <?php 
$people = array(array('name' => 'nev', 'age' => '41', 'occupation' => 'web dev'), array('name' => 'clare', 'age' => '36', 'occupation' => 'events'), array('name' => 'fred', 'age' => '0', 'occupation' => 'being cute'));
function arrPluck($arr, $topluck = 'name')
{
    $ret = array();
    foreach ($arr as $item) {
        array_push($ret, $item[$topluck]);
    }
    return $ret;
}
var_dump(arrPluck($people, "age"));
?>
    <hr />
    <p>Array pluck using array map</p>
    <?php 
$people = array(array('name' => 'nev', 'age' => '41', 'occupation' => 'web dev'), array('name' => 'clare', 'age' => '36', 'occupation' => 'events'), array('name' => 'fred', 'age' => '0', 'occupation' => 'being cute'));
function arrPluckMap($arr, $topluck)
{
    return array_map(function ($item) use($topluck) {
        return $item[$topluck];
    }, $arr);
}
var_dump(arrPluckMap($people, "occupation"));
?>
</body>
</html>
Ejemplo n.º 2
0
}
pp($idxfamily);
?>
    <hr />
    <p>Array pluck </p>
    <?php 
$people = array(array('name' => 'nev', 'age' => '41', 'job' => 'web dev'), array('name' => 'clare', 'age' => '36', 'job' => 'events'), array('name' => 'fred', 'age' => '0', 'job' => 'being cute'));
function arrPluck($arr, $topluck = 'name')
{
    $out = array();
    foreach ($arr as $item) {
        array_push($out, $item[$topluck]);
    }
    return $out;
}
var_dump(arrPluck($people, 'name'));
?>
    <hr />
    <p>Array pluck using array map</p>
    <?php 
function arrPluckMap($arr, $topluck = 'name')
{
    return array_map(function ($item) use($topluck) {
        return $item[$topluck];
    }, $arr);
}
var_dump(arrPluckMap($people, 'job'));
?>
</body>
</html>