PHP多维数组+ MySQLi $行数组数据

时间:2022-10-08 21:21:54
$category = array(
    "Alpha",
    "Beta",
    "Gamma",
    "Delta",
    "Epsilon",
    "Zeta"
);
for ($count = 0; $count < 5; $count++) {

    $query = "SELECT * FROM random_walk WHERE category = '$category[$count]'";
    $result = $mysqli->query($query) or die($mysqli->error . __LINE__);
    $row_cnt = $result->num_rows;

    if ($result->num_rows > 0) {
        $row_counter = 0;

        while ($row = $result->fetch_assoc()) {
            $category[$count][$row_counter] = $row['image_filename'];
            $row_counter++;

            echo $category[$count][$row_counter];
        }
    }
}

I am trying to store MySQLi $row data into a PHP 2 dimensional array $category[][].

我正在尝试将MySQLi $row数据存储到PHP 2维数组$category[][][]。

I have initialized an array named $category which contains contains the category names I wish to use. I now want to retrieve records from my database and store the contents of the record field image_file (eg. poly_interpolated.jpg) into the second dimension and loop until there are no more images files for that category in the database.

我初始化了一个名为$category的数组,其中包含我希望使用的类别名。现在我想从数据库中检索记录并存储记录字段image_file的内容(例如)。进入第二个维度并循环,直到数据库中不再有该类别的图像文件。

However, when I echo the array I only see a single character which is not what I was expecting to happen at all as $row['image_file'] returns a filename of multiple characters in length. I would have thought that $category[$count][$row_counter] = $row['image_filename']; would store the name of the file but it appears I'm some way off in that assumption.

然而,当我返回数组时,我只看到一个字符,这不是我期望发生的,因为$row['image_file']会返回一个长度为多个字符的文件名。我认为$category[$count][$row_counter] = $row['image_filename'];会存储文件的名称,但在这个假设中,我似乎离它不远了。

Can someone please take a look at the above code and point me in the right direction?

谁能看看上面的代码,给我指出正确的方向吗?

1 个解决方案

#1


4  

This is what you're looking for.

这就是你要找的。

Some insight - you're only seeing one character because you don't have a multidimensional array. You have a single-dimensional array of strings, and when you access an index on those strings, you get a character in the string.

一些见解-你只看到一个字符,因为你没有多维数组。你有一个单维字符串数组,当你访问这些字符串上的索引时,你会得到字符串中的一个字符。

You want an associative array of keys representing arrays.

您需要一个表示数组的键的关联数组。

I added other code to concatenate your searches into a single search with an IN clause, so it'll do something like WHERE category IN ('Alpha', 'Beta', etc.). This will be much faster.

我添加了其他代码,将搜索连接到一个带有IN子句的单个搜索中,因此它将执行一些类似WHERE category IN ('Alpha', 'Beta',等等)的操作。这会快得多。

Finally, for each record, you want to add it to the category array with the matching index. This will sort your data into the category collection.

最后,对于每个记录,您希望将其添加到具有匹配索引的类别数组中。这将把数据分类到类别集合中。

Oh, also no need for a row counter. Just adding the row to the end of its category will index it properly in the array.

哦,也不需要行计数器。只要将行添加到它的类别末尾,就会在数组中正确地索引它。

$category = array(
    "Alpha" => array(),
    "Beta" => array(),
    "Gamma" => array(),
    "Delta" => array(),
    "Epsilon" => array(),
    "Zeta" => array()
);

// Concatenate the categories for searching
$categories = array_keys($category);
for ($i = 0; $i < count($categories); $i++) {
    $categories[$i] = "'{$categories[$i]}'";
}
$categories = implode(",", $categories);

// Query on an IN clause
$query = "SELECT * FROM random_walk WHERE category IN ({$categories})";
$result = $mysqli->query($query) or die($mysqli->error . __LINE__);
$row_cnt = $result->num_rows;

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $category[$row['category']][] = $row['image_filename'];
        echo $category[$row['category']][count($category[$row['category']]) - 1];
    }
}

#1


4  

This is what you're looking for.

这就是你要找的。

Some insight - you're only seeing one character because you don't have a multidimensional array. You have a single-dimensional array of strings, and when you access an index on those strings, you get a character in the string.

一些见解-你只看到一个字符,因为你没有多维数组。你有一个单维字符串数组,当你访问这些字符串上的索引时,你会得到字符串中的一个字符。

You want an associative array of keys representing arrays.

您需要一个表示数组的键的关联数组。

I added other code to concatenate your searches into a single search with an IN clause, so it'll do something like WHERE category IN ('Alpha', 'Beta', etc.). This will be much faster.

我添加了其他代码,将搜索连接到一个带有IN子句的单个搜索中,因此它将执行一些类似WHERE category IN ('Alpha', 'Beta',等等)的操作。这会快得多。

Finally, for each record, you want to add it to the category array with the matching index. This will sort your data into the category collection.

最后,对于每个记录,您希望将其添加到具有匹配索引的类别数组中。这将把数据分类到类别集合中。

Oh, also no need for a row counter. Just adding the row to the end of its category will index it properly in the array.

哦,也不需要行计数器。只要将行添加到它的类别末尾,就会在数组中正确地索引它。

$category = array(
    "Alpha" => array(),
    "Beta" => array(),
    "Gamma" => array(),
    "Delta" => array(),
    "Epsilon" => array(),
    "Zeta" => array()
);

// Concatenate the categories for searching
$categories = array_keys($category);
for ($i = 0; $i < count($categories); $i++) {
    $categories[$i] = "'{$categories[$i]}'";
}
$categories = implode(",", $categories);

// Query on an IN clause
$query = "SELECT * FROM random_walk WHERE category IN ({$categories})";
$result = $mysqli->query($query) or die($mysqli->error . __LINE__);
$row_cnt = $result->num_rows;

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $category[$row['category']][] = $row['image_filename'];
        echo $category[$row['category']][count($category[$row['category']]) - 1];
    }
}