Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

PHP functions

<?php

function say_hello($name = 'buddy'){
    return "Hi, there $name!";
}

//echo say_hello('Joe');

//functions.php
function pp($value){ //object or array to be provided
    echo '<pre>';
    print_r($value);
    echo '</pre>';
}

$arr = [
        'name' => 'Joe',
        'age' => 40,
        'occupation' => 'teacher'
    ];
    
//print_r($arr);

/*function array_pluck($toPluck, $arr){
    $ret = []; //new array to be returned
    
    foreach($arr as $item){
        $ret[] = $item[$toPluck]; //remplissage
    }
    
    return $ret;
}*/

//every function has his own local scope.
function array_pluck($toPluck, $arr){
    return array_map(function($item) use($toPluck){
        return $item[$toPluck];
    }, $arr);
}

$people = [
    ['name' => 'Jean', 'age' => 43, 'occupation' => 'Web developer'],
    ['name' => 'Joe', 'age' => 50, 'occupation' => 'Teacher'],
    ['name' => 'Jane', 'age' => 30, 'occupation' => 'Marketing']
];

//1ere étape 
$names = array_pluck('name', $people); // retourner ['Jean', 'Joe', 'Jane']
//print_r($plucked);

foreach($names as $name){
    echo $name; 
}










Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.