0

My result from a SQL-query is saved in the variable $res.

A print_r() from $res says this, which is ok (I think):

Array ( [0] => Array ( [hoejdemeter] => 1152 [laengde] => 24120 ) ) 

The $res is returned to a function and looped over to get the two variables:

foreach ($Data as  $value) {
    list ($length, $hight) = $Data;
}

Then I get this problem:

Notice: Undefined offset: 0 in C:\xampp\htdocs\PhpProject1\BjergCykel1.php on line 95

What is wrong?

dferenc
  • 7,918
  • 12
  • 41
  • 49
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – cteski Jan 15 '18 at 15:42
  • PS: Its "height" not "hight". – IncredibleHat Jan 15 '18 at 16:05

3 Answers3

0

You can simply do not use list:

foreach ($Data as  $value) {
    $hight  = $value['hoejdemeter'];
    $length = $value['laengde'];
}
Vladimir
  • 1,373
  • 8
  • 12
0

The problem you have is with using list with an associative array. The list documentation clearly states:

list() only works on numerical arrays and assumes the numerical indices start at 0.

Your array has the two string keys hoejdemeter and laengde instead of the numerical indices 0 and 1.

The cleanest way to retrieve the data would be:

foreach ($Data as $value) {
    $height = $value['hoejdemeter'];
    $length = $value['laengde'];
    //... Do something with $height and $length inside the foreach
}

Using a function fo retrieve the values instead of using the keys and assigning them manually just makes your code less readable and should not be done in your case.

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
0

Your question is incomplete - we can't see the actual code example so it's hard to tell what happens inside your function. I strongly recommend reading this excellent article How To Ask Questions The Smart Way

I recommend that you construct a minimalist example that shows the error like so:

<?php

 function test($data){
                print_r($data);

                foreach ($data as  $value) {
                list($length, $hight) = $value;
                    echo("DEBUG: $length, $hight\n");
                }
        }

        $data=array(array ("hoejdemeter" => 1152, "laengde" => 24120 ) );
        test($data);

?>

Output then is:

Array
(
    [0] => Array
        (
            [hoejdemeter] => 1152
            [laengde] => 24120
        )

)
PHP Notice:  Undefined offset: 0 in /tmp/foo.php on line 7
PHP Notice:  Undefined offset: 1 in /tmp/foo.php on line 7
DEBUG: , 

I guess what you wanted to do instead was:

<?php

 function test($data){
                print_r($data);

                foreach ($data as  $value) {
                print_r($value);
                list($length, $hight) = array_values($value);
                    echo("DEBUG: $length, $hight\n");
                }
        }

        $data=array(array ("hoejdemeter" => 1152, "laengde" => 24120 ) );
        test($data);

?>

Output:

Array
(
    [0] => Array
        (
            [hoejdemeter] => 1152
            [laengde] => 24120
        )

)
Array
(
    [hoejdemeter] => 1152
    [laengde] => 24120
)
DEBUG: 1152, 24120
Konrads
  • 2,206
  • 2
  • 30
  • 45