在php

时间:2017-10-23 10:16:32

标签: php arrays multidimensional-array

可能我的问题对你来说太常见或容易,但我真的不知道该怎么做。尝试我所知道的所有内容和大量谷歌搜索并没有帮助我。

我只需要一个嵌套数组。

这是我的PHP代码:

EDITED
更正的:$ data ['product_names'] [$ language ['language_id']] [] =数组(

$data['product_names'] = array();

foreach ($data['languages'] as $language) {
    $product_names_info = $this->model_catalog_category->getCategoryMultiLang($this->request->get['product_id'], $language['language_id']);

    if ($product_names_info) {
        $data['product_names'][$language['language_id']][] = array(
            'category_id' => $product_names_info['category_id'],
            'language_id' => $product_names_info['language_id'],
            'name'        => $product_names_info['name']
        );
    }
}

print_r($data['product_names']);

我得到的结果:

Array
(
    [5] => Array
        (
            [0] => Array
                (
                    [category_id] => 
                    [language_id] => 
                    [name] => 
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [category_id] => 
                    [language_id] => 
                    [name] => 
                )

        )

    [4] => Array
        (
            [0] => Array
                (
                    [category_id] => 
                    [language_id] => 
                    [name] => 
                )

        )

    ... 

结果应如下所示:

Array
(
    [0] => Array
        (
            [language_id] => 1
            [category_id] => 8
            [name] => book
        )

    [1] => Array
        (
            [language_id] => 5
            [category_id] => 188
            [name] => magazine
        )

...
)
Array
(
    [0] => Array
        (
            [language_id] => 1
            [category_id] => 8
            [name] => buch
        )

...

已更新
print_r($product_names_info);

中的foreach ($data['languages'] as $key => $language) {结果
Array
(
    [0] => Array
        (
            [language_id] => 5
            [category_id] => 8
            [name] => Gecelik
        )

    [1] => Array
        (
            [language_id] => 5
            [category_id] => 188
            [name] => Sabahlık
        )

    ...

)
Array
(
    [0] => Array
        (
            [language_id] => 2
            [category_id] => 8
            [name] => لباس خواب
        )

    [1] => Array
        (
            [language_id] => 2
            [category_id] => 188
            [name] => Sabahlık
        )

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

$data['product_names'] = array();

foreach ($data['languages'] as $language) {
    $product_names_info = $this->model_catalog_category->getCategoryMultiLang($this->request->get['product_id'], $language['language_id']);

    if ($product_names_info) {
        foreach ($product_names_info as $key => $value) {
            $data['product_names'][$language['language_id']][] = array(
                'category_id' => $value['category_id'],
                'language_id' => $value['language_id'],
                'name'        => $value['name'],
            );
        }

    }
}

print_r($data['product_names']);

我认为您在var $product_names_info

中获取的数据缺少foreach