0

我有这种数组,我希望用 MySQL db 中的值填充它。例如,如果数据库中有 5 行,它应该将数组与其中的值一起循环 5 次。

这就是我当前的数组(用 JSON 解析)的样子:

array(
    1 => array(
        "id" => 1,
        "category" => "For men",
        "items" => array(
            array(
                "id" => 1,
                "name" => "Baju",
                "price" => "5:52"
            ),
            array(
                "id" => 2,
                "name" => "The Canyon",
                "price" => "3:01"
            )
        )
    ),
    2 => array(
        "id" => 2,
        "category" => "Adele 21",
        "items" => array(
            array(
                "id" => 1, 
                "name" => "Rolling In The Deep", 
                "price" => "03:48"
            ),
            array(
                "id" => 2,
                "name" => "Rumour Has It",
                "price" => "03:43"
            )
        )
    )
),
.
.
.
. //more arrays here;

所以我做了一个while循环来print_r()根据我的数据库打印出()数组。它看起来像这样:

Array (
    [0] => Array (
        [id] => 1
        [item_photo] => gambar1
        [item_name] => Kotak
        [item_description] => Desc 1 - Lorem ipsum blablabla
        [user_id] => 6
        [category_id] => 2
    )
    [1] => Array (
        [id] => 2
        [item_photo] => gambar2
        [item_name] => Botol
        [item_description] => Desc 2 - Lorem ipsum blablabla
        [user_id] => 6
        [category_id] => 3
    )
    [n] . . .
    .
    .
    .
)

我想要实现的是用数组二中的值替换第一个数组中的值(要在 JSON 中解析)。像这样,

array(
    $category_id => array(
        "id" => $i++,
        "category" => $category_name,
        "items" => array(
            array(
                "id" => $j++,
                "user_name" => $user_name,
                "name" => $item_name,
                "price" => $price,
                "item_description" => $item_description
                )
            )
        ),
); //notice the variables

这只是我想要实现的想法的草图。但是我无法解决这个问题以使其发挥作用。

知道如何根据第二个数组的值填充第一个数组吗?

4

0 回答 0