Summer Sale! All accounts are 50% off this week.

Linda Maya's avatar

Undefined array key 1, ErrorException while trying to pass a variable to view

sorry for bothering, I was expected to get some results after passing $wordsset to the view with compact. Similar things work in other parts of the project. But here it doesn't

I would be very pleased to hear from you if you have any idea?

in the function


$wordsset = [[1],[[["1"],["1"],["scaun"]]],[[["2"],["1"],["stuhl"]]],[[["3"],["1"],["chair"]],[["4"],["2"],["seat"]]],[[["5"],["la"],["chaise"]]]];

return view('back.editword', compact('wordsset'));

in the blade php


{{var_export($wordsset,true)}}

0 likes
15 replies
Linda Maya's avatar

dd($wordsset); gives me exactly this:


array:5 [▼
  0 => array:1 [▼
    0 => 1
  ]
  1 => array:1 [▼
    0 => array:3 [▼
      0 => array:1 [▼
        0 => "1"
      ]
      1 => array:1 [▼
        0 => "1"
      ]
      2 => array:1 [▼
        0 => "scaun"
      ]
    ]
  ]
  2 => array:1 [▼
    0 => array:3 [▼
      0 => array:1 [▼
        0 => "2"
      ]
      1 => array:1 [▼
        0 => "1"
      ]
      2 => array:1 [▼
        0 => "stuhl"
      ]
    ]
  ]
  3 => array:2 [▼
    0 => array:3 [▼
      0 => array:1 [▼
        0 => "3"
      ]
      1 => array:1 [▼
        0 => "1"
      ]
      2 => array:1 [▼
        0 => "chair"
      ]
    ]
    1 => array:3 [▼
      0 => array:1 [▼
        0 => "4"
      ]
      1 => array:1 [▼
        0 => "2"
      ]
      2 => array:1 [▼
        0 => "seat"
      ]
    ]
  ]
  4 => array:1 [▼
    0 => array:3 [▼
      0 => array:1 [▼
        0 => "5"
      ]
      1 => array:1 [▼
        0 => "la"
      ]
      2 => array:1 [▼
        0 => "chaise"
      ]
    ]
  ]
]

Ysior's avatar

I had the same error. In my case problem was in my blade. I leave open loop and dont saw it... (-‸ლ) So, to all visitors searching for answer, chceck your blades :)

jlrdw's avatar

Where is the error coming from? The array is a correct array. Remember an array is 0 based, starts at 0.

Linda Maya's avatar

There is this, stack trace opening in browser. And 59 pages open respectively. None of them is anything that I see my own code.

There is the 55th. page which does not show anything, but on the title it says "1 unknown frame" . somehing mysterious.

jlrdw's avatar

That array is correct, do you get error before view is loaded, or after?

Or you calling correct view?

Linda Maya's avatar

I suppose the things I have done until the dd($wordset); is correct as it shows the array.

It is interesting because I have removed the

{{var_export($wordsset,true)}}

from blade file, and there is not a single variable calling now. But I am still getting the same "Undefined array key 1" error.

jlrdw's avatar
jlrdw
Best Answer
Level 75

I even tried:

      $wordsset = [[1],[[["1"],["1"],["scaun"]]],[[["2"],["1"],["stuhl"]]],[[["3"],["1"],["chair"]],[["4"],["2"],["seat"]]],[[["5"],
       
       echo $wordsset[0][0];
       die;

Which gives:

1

See if it works in blade

{{ $wordsset[0][0] }}

edit, I just tryed and this workded:

       $wordsset = [[1],[[["1"],["1"],["scaun"]]],[[["2"],["1"],["stuhl"]]],[[["3"],["1"],["chair"]],[["4"],["2"],["seat"]]],[[["5"],["la"],["chaise"]]]];
       
       return view('testarray.atest', compact('wordsset'));

And in view, just a quick setup:

<!DOCTYPE html>
<html lang="en">
    <head>
        
    </head>
    <body>
        {{ $wordsset[0][0] }}
  </body>    
</html>

And got 1.

Make sure you are calling the correct view, works on my end.

Linda Maya's avatar

I was just about to write here my final thank you words. But I did final trial after a normal restart. It worked! Thanks for trying for me. This had happened to me before with the system restart thing.

Now I think we have discovered something. With this kind of error Undefined array key 1, ErrorException you cant really see where the error is coming from.

Thanks.

Linda Maya's avatar

@jlrdw

No actually, same problem when I try the array with index 3 with this foreach function:

There are two arrays under the $wordsset[3] ,

But I get the same error now. How pity.


 @foreach($wordsset[3] as $elements)

              {{$elements}}

  @foreach

Linda Maya's avatar

Could we escalate this problem somehow?

Undefined array key 1

jlrdw's avatar

See example here of how I flatten an array, https://gist.github.com/jimgwhit/cbbe5bb0d2556fdc7e37a86d3630239c

edit:

       $wordsset = [[1],[[["1"],["1"],["scaun"]]],[[["2"],["1"],["stuhl"]]],[[["3"],["1"],["chair"]],[["4"],["2"],["seat"]]],[[["5"],["la"],["chaise"]]]];
       
       $s = $this->array_flatten($wordsset);
        $keys = array_keys($s);

        for ($i = 0; $i < count($s); $i++) {
            foreach ($s[$keys[$i]] as $key => $value) {
                echo $key . " : " . $value . "<br>";
            }
            echo "-----------------------------------";
            echo "<br>";
        }

Results:

0 : 1
-----------------------------------
0 : 1
-----------------------------------
0 : 1
-----------------------------------
0 : scaun
-----------------------------------
0 : 2
-----------------------------------
0 : 1
-----------------------------------
0 : stuhl
-----------------------------------
0 : 3
-----------------------------------
0 : 1
-----------------------------------
0 : chair
-----------------------------------
0 : 4
-----------------------------------
0 : 2
-----------------------------------
0 : seat
-----------------------------------
0 : 5
-----------------------------------
0 : la
-----------------------------------
0 : chaise
-----------------------------------

Your loop is not set up correctly.

Or for key3

   $s = $this->array_flatten($wordsset[3]);   //here
        $keys = array_keys($s);

        for ($i = 0; $i < count($s); $i++) {
            foreach ($s[$keys[$i]] as $key => $value) {
                echo $value . "<br>";
            }
            echo "-----------------------------------";
            echo "<br>";
        }

Gives:

3
-----------------------------------
1
-----------------------------------
chair
-----------------------------------
4
-----------------------------------
2
-----------------------------------
seat
-----------------------------------
Linda Maya's avatar

Would I be able to separately pick values, when I flatten the array?

jlrdw's avatar

You could do similar as above and put them into a collection, where there are a bunch of helper methods:

https://laravel.com/docs/8.x/collections

https://laravel.com/docs/8.x/collections#available-methods

Collections are pretty powerful.

Also I'd consider refactoring the array with meaningful key names; ie:

$collection = collect([
    ['product' => 'Desk', 'price' => 200],
    ['product' => 'Chair', 'price' => 100],
]);

Just example.

edit

When you have array's in array's and just numbers for keys you end up with things like having to:

       $s = $this->array_flatten($wordsset[3]);
       echo $s[2][0];

which gives:

chair

If this is an API, you always want consistent nesting in the data. And better key names if possible:

For example from a previous question:

        $currencies = '{
	"USD": {
		"symbol": "$",
		"name": "US Dollar",
		"symbol_native": "$",
		"decimal_digits": 2,
		"rounding": 0,
		"code": "USD",
		"name_plural": "US dollars"
	},
	"EUR": {
		"symbol": "€",
		"name": "Euro",
		"symbol_native": "€",
		"decimal_digits": 2,
		"rounding": 0,
		"code": "EUR",
		"name_plural": "euros"
	},
	"GBP": {
		"symbol": "£",
		"name": "British Pound Sterling",
		"symbol_native": "£",
		"decimal_digits": 2,
		"rounding": 0,
		"code": "GBP",
		"name_plural": "British pounds sterling"
	},
	"INR": {
		"symbol": "Rs",
		"name": "Indian Rupee",
		"symbol_native": "টকা",
		"decimal_digits": 2,
		"rounding": 0,
		"code": "INR",
		"name_plural": "Indian rupees"
	}
}';

See the consistency and names make a big difference.

Having things like:

[[["3"],["1"],["chair"]],[["4"],["2"],["seat"]]],

instead of

[["3"],["1"],["chair"]],[["4"],["2"],["seat"]],

To me makes it harder to setup loops.

Mohamed1Gohar's avatar
# this solution worked with me :
$todos = $request->user()->todos->groupBy('completed');
    $todos[boolval(false)];
    $todos[boolval(true)];

Please or to participate in this conversation.