$arr = array(
'id' => 'ID',
'staff_name' => '昵称',
'staff_real '=> '真实姓名',
);
$strKey = 'id,staff_name,staff_real';
$strValue = 'ID,昵称,真实姓名';
As above, $arr
How to output the key name into a string and the key value into a string?
I heard that it can be achieved using the arraykeys
and arraryvalue
functions, but I don’t know how to write them. I also want to know how to use foreach
and for
How to deal with it.
Want to let you know, thank you.
巴扎黑2017-06-12 09:23:57
$strKey = implode(',', array_keys($arr));
$strValue = implode(',', array_values($arr));
代言2017-06-12 09:23:57
It’s actually very simple. The author must not know much about PHP! !
The first step is to use array_keys() to get all the keys from the array to form a new array.
The second step is to convert the two arrays into strings through implode().
Next, let’s talk about the solution to the foreach loop that the poster wants to know:
The first step is to concatenate strings in a loop.
The second step is to remove the comma at the end of the string.
The code is as follows:
$arr = array(
'id' => 'ID',
'staff_name' => '昵称',
'staff_real '=> '真实姓名'
);
$keystr = '';
$valstr = '';
foreach($arr as $key => $val) {
$keystr .= $key.',';
$valstr .= $val.',';
}
$keystr = trim($keystr, ',');
$valstr = trim($valstr, ',');
echo $keystr;