Reputation: 43
I have an Array as shown bellow
Array
(
[ifour consultancy 123] => Array
(
[Company] => ifour consultancy 123
[Physical] => B-515, Gopal Palace, Near shiromani complex,
[address] => test,
)
)
i am try to print it using
echo $array[0]['Company'];
and it give me Message:
Undefined offset: 0 instead of showing me ifour consultancy 123
Upvotes: 1
Views: 731
Reputation: 167182
For these kind of walkthroughs, you need to use foreach
:
foreach ($array as $value) {
print_r($value);
}
Or if you want to get 0
or number based indices, you need to use array_values()
:
$numbased = array_values($array);
$numbased[0]["Company"]; // ifour consultancy 123
Upvotes: 1
Reputation: 20737
You can use the array_values()
function to achieve your goal:
// 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'];
Upvotes: 3