array_combine字符串中的两个以上的值

时间:2017-10-24 12:24:19

标签: php mysql

我试图在我的array_combine函数中添加一个额外的数组,当添加了两个以上的值时,我得到以下错误:

  

[24-Oct-2017 12:18:18 UTC] PHP警告:在第61行的/manage/add-users-to-courses.php中为foreach()提供的参数无效

我的工作代码:

$stmtcd = "SELECT id, course_title FROM distributor_course_settings WHERE distributor = ? AND active = 'Y' AND price NOT LIKE '0.00'";
$stmtcd = $conn->prepare($stmtcd);
$stmtcd->bind_param('s', $distributor);
$stmtcd->execute();
$stmtcd->bind_result($id, $course_title);

while($stmtcd->fetch()) {
    $course_id_array[] = $id;
    $course_title_array[] = $course_title;
}

<?php
    foreach (array_combine($course_id_array, $course_title_array) as $course_id => $course_title) {
        echo '<tr style="background: #dddddd;">';
        echo '<td><h2 style="text-align: center; margin: 0;">' . $course_title . ' - ' . $price . '</h2></td>';
        echo '</tr>';

        foreach (array_combine($user_id_array, $username_array) as $user_id => $username) {
            echo '<tr>';
            echo '<td><input type="checkbox" id="" name="" value="' . $course_id . ',' . $course_title . ',' . $price . ',' . $user_id . ',' . $username . '"> ' . $username . '</td>';
            echo '</tr>';
        }
    }
?>

每当我尝试将'price'添加到SELECT语句以及for each语句时,我都会收到上述错误。

这是为什么? foreach / array_combine中允许的值是否有限制?

2 个答案:

答案 0 :(得分:1)

遵循良好的编码标准和best practices

虽然“最佳实践”范围广泛,绝大多数,并且充满了不同的意见,但它们很重要且通常都同意。在您的情况下,您应该initialize variables(参见示例1)。

PHP array_combine需要数组

超出预期,array_combine 需要所有参数都是数组。因此,将这些变量初始化为空数组。

$course_id_array = [];
$course_title_array = [];
$user_id_array = [];
$username_array = [];

while($stmtcd->fetch()) {
    $course_id_array[] = $id;
    $course_title_array[] = $course_title;
}

不要破坏您的数据。

在您的代码中,您将数据的ID和数据分成不同的数组,只是再次组合它们以循环它们。正确创建数组,您的代码将很多更清洁。

$courses = [];
while($stmtcd->fetch()) {
    $courses[$id] = $course_title;
}

foreach($courses as $course_id => $course_title) {
    // your code here
}

使用对象让生活更轻松

虽然一开始构建一切程序都比较简单,但OOP会让生活更轻松。

// build an object for your Course
Class Course
{
    public $id, $title, $price;

    public function __construct($title, $id = 0)
    {
        $this->id = $id;
        $this->title = $title;
    }
}

// Build an object for your db conn and queries
// Note: in a larger app, DB conn, and specific SQL commands, should be built in different objects, Like:
//     $dbo = new DBO($conn);
//     $courseTools = new CourseDBTools($dbo);
//     $course = $courseTools->getOneMatchingCourse([args]);
Class DBO
{
    private $conn;

    public function __construct($conn)
    {
        $this->conn = $conn;
    }

    public function getCoursesByDistrubutor($distributor)
    {
        $title = '';
        $id = 0;
        $price = 99;
        $stmtcd = "SELECT id, course_title, price FROM distributor_course_settings WHERE distributor = ? AND active = 'Y' AND price NOT LIKE '0.00'";
        $stmtcd = $this->conn->prepare($stmtcd);
        $stmtcd->bind_param('s', $distributor);
        $stmtcd->execute();
        $stmtcd->bind_result($id, $title, $price);

        while($stmtcd->fetch()) {
            $course = new Course($title, $id);
            $course->price = $price;
            yield $course;
        }
    }
}

// Then your actual code becomes clean and simple
$distributor = 'ABC';
$conn = null; //set it, 
$dbo = new DBO($conn);

foreach ($dbo->getCoursesByDistrubutor($distributor) as $course) {
    echo $course->title . ' - ' . $course->price;
}

答案 1 :(得分:-1)

保持简单。将两个循环更改为这两个:

while($stmtcd->fetch()) {
    $titles[$id] = $course_title;
}
foreach ($titles as $course_id => $course_title) { ... }

如果您以后只需要ID或只有标题(没有ID),请参阅array_keys($title)array_values($titles)