php - How to use foreach and for to process the following array into the string below the array?
欧阳克
欧阳克 2017-06-12 09:21:57
0
4
1112
$arr = array(
                'id' => 'ID',
                'staff_name' => '昵称',
                'staff_real '=> '真实姓名',
            );

$strKey = 'id,staff_name,staff_real';
$strValue = 'ID,昵称,真实姓名';

As above, $arrHow 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 forHow to deal with it.
Want to let you know, thank you.

欧阳克
欧阳克

温故而知新,可以为师矣。 博客:www.ouyangke.com

reply all(4)
巴扎黑
$strKey = implode(',', array_keys($arr));
$strValue = implode(',', array_values($arr));
typecho
echo implode(',', array_keys($arr));
echo implode(',', $arr);
代言

It’s actually very simple. The author must not know much about PHP! !

  1. The first step is to use array_keys() to get all the keys from the array to form a new array.

  2. 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:

  1. The first step is to concatenate strings in a loop.

  2. 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;
我想大声告诉你

https://www.bytelang.com/o/s/...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template