phparraysmultidimensional-array

Why can't I declare a scalar value and a non-scalar value on the same level of an array?


I have this code:

$mdArray = array(array());
$mdArray[0] = "1000";
$mdArray[0]["status"] = true;

echo $mdArray[0]["status"];

For some reason, this fails with a:

Warning: Illegal string offset 'status'

There error occurs when I try to assign "true" via $mdArray[0]["status"] = "true";

If I already have an array within an array stored in the variable $mdArray, why is adding a "status" key to 0, failing?


Solution

  • because $mdArray[0] is not an array , it should be an array like

    $mdArray = array(array());
    $mdArray[0] = array("1000");
    $mdArray[0]["status"] = true;
    
    echo $mdArray[0]["status"];