访问多维数组时未定义的索引

时间:2014-09-10 15:27:42

标签: php multidimensional-array

我有一个多维数组。当我访问它时,我得到了未定义的索引,但我无法看到我做错了什么。如果有人有胶水,我非常感激,因为我现在已经有很长一段时间了。谢谢!

转储:

Array
(
[922] => Array
    (
        [products] => Array
            (
                [169] => http://www.golf-safari.com/golf_regions/cape_town.asp
            )

        [company] => stdClass Object
            (
                [id] => 922
                [category_id] => 42
                [language_id] => 1
                [shipping_from_id] => 0
                [name] => Golf-Safari.com
                [logo] => 
                [company_id] => 922
                [area_id] => 414
                [country_id] => 
                [city_id] => 260
                [product_id] => 169
                [url] => http://www.golf-safari.com/golf_regions/cape_town.asp
                [likes] => 0
                [dislikes] => 0
                [views] => 0
            )

    )
)

我的访问代码:

<?php foreach($items as $cmp_id => $company):
      $item = $company['company']; // throws undefined index company
      $item = $company['products']; // throws undefined index product
?>
<div class="title" title="<?= $item->company ?>"> 

我甚至没有进入div,因为错误已经在$ company [&#39;公司&#39;]

上出现了

感谢所有答案!我真的不知道什么是错的。到目前为止,我一直认为当产生转储时,可以访问此转储,显然不是。我在下面添加了我的准备代码。

我的准备代码:

foreach($rows as $row)
    {                   
        if($row->product_id && !in_array($row->product_id, $products)){
            array_push($products, $row->product_id);                
        }           
        $products_count[$row->product_id][] = '1';      

        $ids[$row->id] = $row->id; //store every company id, in order to avoid repetitions count variables  

        $items[$row->id]['products'][$row->product_id] = $row->url; //cities products               
        $items[$row->id]['company'] = $row; 
    }
            if($products && is_array($products)){
                $query = DB::table('products');
                $query->select('id', "name_$this->language AS product");
                $query->whereIn('id', $products);
                $product_names = $query->get();
    }


            $sub['items'] = array(
                'items' => $items,
                'product_names'=> $product_names
            );

4 个答案:

答案 0 :(得分:2)

  1. $ item = $ company [&#39; product&#39;]; //抛出未定义的索引产品
  2. 在这里你传递错误的密钥使用$ item = $ company [&#39; products&#39;];

    的print_r($公司[&#39;产品&#39;]);

    1. 在$ company [&#39; company&#39;]中它是一个对象,因此您可以通过以下方式访问这些值 echo $ company [&#39; company&#39;] - &gt; name;

答案 1 :(得分:1)

阵列大约有3层。我将继续并假设$ items是outtermost层。根据您的结构,您的代码应如下所示:

foreach($item[922] as $cmp_id => $company){
       $$cmp_id = $company    //converts the index of the item[922] into a variable and assign them the values
      $item = $$cmp_id['company']; 
      $item = $$cmp_id['product']; 
    }

第一个$项目将保留此值

 [company] => stdClass Object
        (
            [id] => 922
            [category_id] => 42
            [language_id] => 1
            [shipping_from_id] => 0
            [name] => Golf-Safari.com
            [logo] => 
            [company_id] => 922
            [area_id] => 414
            [country_id] => 
            [city_id] => 260
            [product_id] => 169
            [url] => http://www.golf-safari.com/golf_regions/cape_town.asp
            [likes] => 0
            [dislikes] => 0
            [views] => 0
        )

第二个将举行

(
    [169] => http://www.golf-safari.com/golf_regions/cape_town.asp
 )

它们都包含数组,我不认为你可以在div中使用它们,因为没有新的$ item数组有索引[company]。获取最里面的数组,将代码更改为

foreach($items[922][company] as $key => $value){   //or $items[922][products]
  $$key = $value;
}

您将获得所有带有索引的值作为变量名称,例如

$category_id =42;

答案 2 :(得分:1)

在您的示例中,每次声明时都会覆盖$ item。以下是如何遍历数组的示例 - http://phpfiddle.org/lite/code/tr0c-u9vh

foreach($items as $comp_id => $company){
    $item = $company['products'];
    $company_name = $company['company']['name'];
    print_r($item); // because this is still an array
    echo $company_name; // this is a specific item
}

你也可以使用PHP的递归迭代器类 - http://php.net/manual/en/class.recursiveiteratoriterator.php

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($items)); 
foreach($iterator as $key=>$value){
    echo $key. "\t" . $value . "\n";
}

本质上通过这样调用它可以展平数组。 http://phpfiddle.org/lite/code/5p3h-fb21

答案 3 :(得分:0)

foreach(['variable_by_which_get_dump']['922'] as $values)

{
    foreach($values as $get_subvalue)
    {
        foreach($get_subvalue as $get_final_subvalue)
        {
            echo $get_final_subvalue;
            echo '<br>';        
        }
    }
}
相关问题