I am having a multi-dimensional array. When I access it I get undefined index but I just can't see what I am doing wrong. I very much appreciate if someone has a glue as I am devastating for quite some while now. Thanks!
Dump:
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
)
)
)
My access code:
<?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 ?>">
I am not even getting to the div as error is thrown already on $company['company']
Thanks for all the answers! I don't really know whats wrong. Up to now I always assumed that when there is a dump produced then this dump can be accessed, apparently not. I added my preparation code below.
My preparation code:
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
);
productis not part of the array,productsis.$itemscontains the array you dumped, this should work. Produce a complete example we can run ourselves, otherwise we can't really help much.