PHP - 注意:未定义的偏移量:0英寸

I'm having troubles using arrays. I don't know why, but my menu can't read my arrays...

I always receive this notice: PHP - Notice: Undefined offset: 0

function DB_array($query,$return_type){
    connect();
    $q = mysql_query($query);


    switch ($return_type){

        case 'a+':
            $num_fields = mysql_num_fields($q); 
            $j=0;
            $x = 0;
            $val = array();
            while($row = mysql_fetch_array($q)){  
                for ($j=0; $j < $num_fields; $j++){
                    $name = mysql_field_name($q, $j);
                    $val[$x][$name] = $row[$name];
                }
                $x++;
            }
            break;

Menu code

<?php

 /*Return Type - Symbology
 *      //SELECT
 *      a+ | Data to array
 */

include(PATH_ROOT . '/modules/menu/view/menu_view.php');
function menuModel(){
        $query = ("SELECT * FROM menu WHERE back = 1");
        $val = DB_array($query,'a+');
        $name = $val['0']['friendlyname'];
        $url = $val['0']['url'];
        menu($url,$name);
}
?>

you are accessing array key as a string when it's an integer since you set it this way:

$val[$x][$name] assuming that your x is $x = 0;

try this instead:

$name = $val[0]['friendlyname'];
$url = $val[0]['url'];

Unless your array keys are strings you are accessing them wrong. Rather than doing

$name = $val['0']['friendlyname'];
$url = $val['0']['url'];

You need to do

$name = $val[0]['friendlyname'];
$url = $val[0]['url'];

If you are still getting the error then it's problem because the array is empty. So it would be a good idea to check that before you try to use it. For example

function menuModel(){
    $query = ("SELECT * FROM menu WHERE back = 1");
    $val = DB_array($query,'a+');

    //Check to see if the array contains anything
    if(!empty($val)) {
        $name = $val[0]['friendlyname'];
        $url = $val[0]['url'];
        menu($url,$name);
    }
}