while循环到Array转换

时间:2017-09-20 11:39:16

标签: php mysql arrays

尝试从while循环中拉出数组时遇到一段代码问题。代码如下

export default class ImageUpload extends Component {
  handleImageChange(e) {
    console.log('change');
    this.props.onChange(e);
  }

  removeImage = (e) => {
    e.preventDefault();
    // Setting the value to null doesn't trigger the onChange event
    this.inputElement.value = null;
    // Below is an attempt to manually trigger the onChange event using the input ref
    const changeEvent = new Event('change');
    this.inputElement.dispatchEvent(changeEvent);
  }

  render() {
    const { name, label, value, children, error } = this.props;

    return (
      <div>
        {label &&
          <label htmlFor={name}>{label}</label>
        }

        {children}

        <div className="image-upload-preview">
          <Button onClick={e => this.removeImage(e)}>Click to remove/change</Button>
          <img alt="" src={value.data.src} />
        </div>

        <button className="form-control image-upload-btn" onClick={this.fakeButtonClick}>
          <IconWithText text="Click to add an image from your device (JPEG, PNG, GIF)" iconName="insert-image" iconSize="M" iconFill="dark" />
          <input
            id="eugh"
            type="file"
            name={name}
            ref={(input) => { this.inputElement = input; }}
            accept="image/jpeg, image/png, image/gif"
            onChange={e => this.handleImageChange(e)}
            onClick={e => e.stopPropagation()}
          />
        </button>
      </div>
    );
  }
}

问题是当$query = "SELECT ID,Assigned_user FROM work_orders"; $result = mysqli_query($connection, $query); while ($row = mysqli_fetch_assoc($result)) { $Assigned[] = $row['Assigned_user']; $ID[] = $row['ID']; var_dump($ID); 返回其结果时,var_dump()被拆分为两个数组,如下所示

$ID[]

我需要它是一个由array (size=1) 0 => string '2' (length=1) D:\wamp64\www\MYCMS\Admin\test.php:31: array (size=2) 0 => string '2' (length=1) 1 => string '3' (length=1)

这两个值组成的数组

然后我需要像

一样爆炸它

Array ( [0] => 2 [1] => 3 )

因此它变为字符串“2,3”,因此可以在IN语句中使用

从我的数据库中插入一个select语句

$IDS = explode(",", $ID);

如果有人能指导我如何做到这一点我会非常感激。

P.S我不能str_split它,因为这需要适用于数百个数字的数量,因此将它分成一个字符不起作用             `

4 个答案:

答案 0 :(得分:2)

这是因为你在循环中将数组转储到了内部。在while循环之后<?php // download.php $path = '/file/'; $fileName = $_GET['file']; $filePath = $path . $fileName; // verify that the file exists if (file_exists($filePath) ) { // force the download header("Content-Disposition: attachment; filename=\"" . basename($fileName) . "\""); header("Content-Length: " . filesize($filePath)); header("Content-Type: application/octet-stream;"); readfile($filePath); } ?> 然后检查

var_dump

答案 1 :(得分:1)

我想你还需要:

implode(',', $ID);

获得&#34; 2,3&#34;字符串,爆炸是对开。

答案 2 :(得分:0)

while ($row = mysqli_fetch_assoc($result)) {

    $Assigned[] = $row['Assigned_user'];
    $ID[] = $row['ID'];
}

implode("','",$ID);

$query = sprintf("SELECT * from work_orders Where ID IN ('".$ID."')");

答案 3 :(得分:0)

如果仅设置$ID数组,以便它可以implode成为字符串,则可以使用字符串连接获得相同的结果。

$ID = ''; // Define $ID as a blank string so it can be added to in the loop
while ($row = mysqli_fetch_assoc($result)) {

$Assigned[] = $row['Assigned_user'];
$ID .= $row['ID'] . ','; // Append current ID and a comma to the ID string
}
rtrim($ID, ','); // Trim off the last comma