I have the following array:
Array
(
[copier-clb009] => Array
(
[status] => Printing
[ping] => PING OK - Packet loss = 0%, RTA = 0.47 ms
[model] => "Xerox WorkCentre 4265"
)
[copier-cor000] => Array
(
[status] => Printing
[ping] => PING OK - Packet loss = 0%, RTA = 0.53 ms
[model] => "Xerox WorkCentre 4265"
)
[printer-001] => Array
(
[status] => Idle
[ping] => PING OK - Packet loss = 0%, RTA = 0.55 ms
[model] => "Xerox Phaser 4600"
)
[printer-002-s1] => Array
(
[status] => Idle
[ping] => PING OK - Packet loss = 0%, RTA = 0.86 ms
[model] => "Xerox WorkCentre 5955 v1 Multifunction System"
)
)
For example, I have tried:
echo $printers[0];
and
echo array_values($printers)[0];
expecting for the results to be "copier-clb009", but neither of these works. What am doing wrong?
Thanks!
i think you have to go with foreach loop this will work for you.
foreach($printers as $key => $value){
echo $key.'<br />';
}
this will print all your key if you need to use specific key value you can set in variable according your requirement.
It's unclear why you want to do that, but PHP already offers you a very convenient construct for iterating over traversable objects, like arrays, called foreach
foreach($printers as $key => $value) {
echo "$key => \n";
foreach($value as $k => $v) {
echo "$k: $v\n";
}
echo "\n";
}
The foreach
construct gives you both access to the key and value of each element you're iterating with every iteration. So there's really no need to do any chicken pecking with array_values
or array_keys
here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.