PHP-no se puede usar un escalar como una advertencia de matriz


Tengo el siguiente código:

 $final = array();
    foreach ($words as $word) {
        $query = "SELECT Something";
        $result = $this->_db->fetchAll($query, "%".$word."%");
        foreach ($result as $row)
        {
            $id = $row['page_id'];
            if (!empty($final[$id][0]))
            {
                $final[$id][0] = $final[$id][0]+3;
            }
            else
            {
                $final[$id][0] = 3;
                $final[$id]['link'] = "/".$row['permalink'];
                $final[$id]['title'] = $row['title'];
            }
        } 
    }

El código parece funcionar bien, pero recibo esta advertencia:

Warning: Cannot use a scalar value as an array in line X, Y, Z (the line with: $final[$id][0] = 3, and the next 2).

¿Alguien puede decirme cómo arreglar esto?

Author: Script47, 2011-05-16

5 answers

Necesita establecer$final[$id] a una matriz antes de agregarle elementos. Intiializarlo con

$final[$id] = array();
$final[$id][0] = 3;
$final[$id]['link'] = "/".$row['permalink'];
$final[$id]['title'] = $row['title'];

O

$final[$id] = array(0 => 3);
$final[$id]['link'] = "/".$row['permalink'];
$final[$id]['title'] = $row['title'];
 54
Author: brian_d,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-16 15:49:19

Un poco tarde, pero para cualquiera que se pregunte por qué están recibiendo el mensaje "Advertencia: No se puede usar un valor escalar como una matriz";

La razón es porque en algún lugar primero ha declarado su variable con un entero o cadena normal y luego está tratando de convertirla en una matriz.

Espero que ayude

 56
Author: Lan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-10 12:11:04

El Otro Problema que he visto en esto es cuando los arrays de anidamiento tienden a lanzar la advertencia, considere lo siguiente:

$data = [
"rs" => null
]

Esto anterior funcionará absolutamente bien cuando se usa como:

$data["rs"] =  5;

Pero lo siguiente lanzará una advertencia::

$data = [
    "rs" => [
       "rs1" => null;
       ]
    ]
..

$data[rs][rs1] = 2; // this will throw the warning unless assigned to an array
 2
Author: Peace Ngara,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-19 10:49:57

También asegúrese de no declararlo como una matriz y luego intente asignar algo más a la matriz como una cadena, float, integer. Yo tenía ese problema. Si haces algunos ecos de salida estaba viendo lo que quería la primera vez, pero no después de otra pasada del mismo código.

 1
Author: Allyn O,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-30 15:31:14

Asegúrese de no declararlo como un entero, float, string o booleano antes. http://php.net/manual/en/function.is-scalar.php

 1
Author: Benjamin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-30 13:38:54