Cake PHP: error de desplazamiento indefinido

i am newbie with Cake PHP. I am trying to run application that I have downloaded recently but it doesn't work.

My debug.log file says: 2012-07-31 12:31:47 Debug: Notice (8): Undefined offset: 0 in [C:\wamp\www\app\models\vwbrowse.php, line 78]

And my error.log file: 2012-07-31 12:31:47 Warning: Warning (2): array_keys() expects parameter 1 to be array, null given in [C:\wamp\www\app\models\vwbrowse.php, line 78]

And finally this is function that causes problem:

function afterFind($results, $primary)
{
    if(!$primary) return $results;

    $ret = array();

    //we are primary and not part of an associated find
    if(!is_array($results)) //find first
    {
        $tables = array_keys($results);
        $record = array();
        foreach($tables as $table){
            $record = array_merge($record,$result[$table]);
        }
        $ret['Vwbrowse'] = $record;

    }else{ //merge all arrays if separated
        $tables = array_keys($results[0]);
        foreach($results as $result){
            $record['Vwbrowse'] = array();
            foreach($tables as $table){
                $record['Vwbrowse'] = array_merge($record['Vwbrowse'],$result[$table]);
            }
            $ret[] = $record;
        }
    }

    return $ret;
}

**This is line 78: $tables = array_keys($results[0]); **

What is wrong? Thanks in advance for solution.

preguntado el 31 de julio de 12 a las 13:07

$results no contiene lo que crees que contiene. var_dump($results) to see what it contains - I suspect it isn't a multidimensional array when you expect it to be. -

$results is an array but it does not have an index as 0. So please try using var_dump -

2 Respuestas

Estás comprobando si $results NO ES an array, then telling your script to perform an array_keys() on it. Does not compute.

Also, you're trying to access $results[0] without confirmation that the 0th index exists.

Pruebe esto primero:

if (array_key_exists(0, $results)) { ... }

Respondido 31 Jul 12, 13:07

Yep that's weird and will fail later, but what is failing for the OP now is the otros llamar a array_keys($results[0]) en la else condición. - Michael Berkowski

@Michael good eyes...I missed that. Edited the answer to reflect this new realization. - Matt

Los programas undefined index error is stating that the index of the array, in your case [0] es indefinido

estas comprobando si $results es una matriz con if (!is_array($results)), but not if it has any data. Try changing it to if (!is_array($results) || (count($results) == 0)).

Respondido 31 Jul 12, 13:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.