没有使用索引的键的回声数组


Echo Array without key using index

我有一个数组,如下所示

 Array
    (
        [ifour consultancy 123] => Array
            (
                [Company] => ifour consultancy 123
                [Physical] => B-515, Gopal Palace, Near shiromani complex,
                [address] => test,
             )
    )

我试着用打印

echo $array[0]['Company'];

它给我的信息:

未定义的偏移量:0而不是给我看我们的咨询123

您可以使用array_values()函数来实现您的目标:

// Extract the values of the array and re-use as indexed array
$array = array_values($array);
echo $array[0]['Company'];

// If you want to keep your associative array as well then do this
$array = array_merge($array, array_values($array));
echo $array[0]['Company'];
// OR
echo $array['ifour consultancy 123']['Company'];

对于此类演练,您需要使用foreach:

foreach ($array as $value) {
  print_r($value);
}

或者,如果你想获得0或基于数字的索引,你需要使用array_values():

$numbased = array_values($array);
$numbased[0]["Company"]; // ifour consultancy 123