0

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
            );
8
  • What parts of the array are you trying to access? Commented Sep 10, 2014 at 15:29
  • 1
    please put more code, with the given information, the problem should not occur Commented Sep 10, 2014 at 15:32
  • 1
    Well, product is not part of the array, products is. Commented Sep 10, 2014 at 15:33
  • Assuming that $items contains the array you dumped, this should work. Produce a complete example we can run ourselves, otherwise we can't really help much. Commented Sep 10, 2014 at 15:34
  • sorry for that, I added more code. But I am not getting to the div, the error is thrown on $company['company']. product(s) was a typo Commented Sep 10, 2014 at 15:35

4 Answers 4

2
  1. $item = $company['product']; // throws undefined index product

In here you are passing the wrong key use $item = $company['products'];

print_r($company['products']);

  1. In $company['company'] its an object so you can access the values by the following way echo $company['company']->name;
Sign up to request clarification or add additional context in comments.

Comments

1

the array has about 3 layers. i'll go ahead and assume that the $items is the outtermost layer. according to your structure, your code should look like this:

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']; 
    }

the first $item will hold this value

 [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
        )

the second one will hold

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

they both contain arrays, i don't think you can use them in the div because none of the new $item arrays has an index [company]. to get the innermost arrays, change the codes to

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

you'll get all the values with their indices as the variable name eg

$category_id =42;

1 Comment

i think this is what you are trying to do
1

In your example you're overwriting $item each time you declare it. Here is a sample of how to loop through the array - 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
}

You could also use PHP's recursive iterator class - http://php.net/manual/en/class.recursiveiteratoriterator.php

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

In essence by calling it like this you can flatten the array. http://phpfiddle.org/lite/code/5p3h-fb21

Comments

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>';        
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.