Thursday 30 August 2018

PHP - How to correctly use implode () with associative arrays

 I have a question regarding the implode() in php, i have this array()

$user_data = array(
    'user_id_num' => $_POST['userid'],
    'fullname' => $_POST['userfname'],
    'username' => $_POST['useruname'],
    'password' => $password_hash
);

what i want to achieve is like this for example,
for the fields
`user_id_num`,`fullname`,`username`,`password`

and for the values
'2159','Sample Name','example','mypassword' <- hash password

what i have tried so far is this
$user_fields = '`' . implode('`, `', $user_data) . '`';
$user_data   = '\'' . implode('\', \', $user_data) . '\'';

but i can't get what i want to achieve can someone help me with this? thanks in advance

Try
$user_fields = '`' . implode('`, `', array_keys($user_data)) . '`';
$user_data   = "'" . implode("', '", array_values($user_data)) . "'";

0 comments:

Post a Comment