PHP implode()用“数组”替换所有数组值

时间:2016-01-06 20:43:54

标签: php mysql arrays

作为MySQL SELECT语句的一部分,我正在尝试动态查询数据库。

当前代码:

$stmt = $pdo->prepare('SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`="assignment" AND `TABLE_NAME`=:table AND `COLUMN_NAME` != :column1 AND `COLUMN_NAME` != :column2;');
  $criteria = [
    'table' => $_GET['section'],
    'column1' => 'jobid',
    'column2' => 'catid'
  ];
  $stmt->execute($criteria);

  $arr = array();

  echo '<form method="POST">';
  foreach ($stmt as $row){
    echo '<label>'.ucwords($row['COLUMN_NAME']).':</label>
    <input type="text" name="'.$row['COLUMN_NAME'].'"/><p>';
    $arr[] = $row;
  }
  echo '<input type="submit" value="Submit" name="submit"/>
      </form>';

  if (isset($_POST['submit']))
    echo $query = implode(',', $arr);

我使用$ _POST值可以正常工作,但出于某种原因输出:

Array,Array,Array,Array,Array

即使$ arr的var_dump()是:

    0 => 
    array (size=2)
      'COLUMN_NAME' => string 'title' (length=5)
      0 => string 'title' (length=5)
  1 => 
    array (size=2)
      'COLUMN_NAME' => string 'salary' (length=6)
      0 => string 'salary' (length=6)
  2 => 
    array (size=2)
      'COLUMN_NAME' => string 'location' (length=8)
      0 => string 'location' (length=8)
  3 => 
    array (size=2)
      'COLUMN_NAME' => string 'description' (length=11)
      0 => string 'description' (length=11)
  4 => 
    array (size=2)
      'COLUMN_NAME' => string 'category' (length=8)
      0 => string 'category' (length=8)

1 个答案:

答案 0 :(得分:3)

implode函数无法按照您希望的方式使用数组数组。它接受$arr中的数组并将它们变成字符串'Array'。您可以循环遍历数组并以此方式内爆,例如,

$list = '';
foreach ($arr as $inner) {
    $list .= $inner['COLUMN_NAME'].',';
}
$list = rtrim($list,',');