Wednesday, 3 May 2017

Implode array key and value


I want to print like

'id'='1', 'name'='Ravi', 'email'='test@gmail.com'

And I have array like

$input = array(
    'id'  => 1,
    'name'  => 'Ravi',
    'email'  => 'test@gmail.com',
);


$output = implode(', ', array_map(
    function ($v, $k) { return sprintf("'%s'='%s'", $k, $v); },
    $input,
    array_keys($input)
));
 
print_r($output);         // it will result as 'id'='1', 'name'='Ravi', 'email'='test@gmail.com'
 
 
 
banner
Previous Post
Next Post

0 comments: