Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo key and value of an array without and with loop

Tags:

arrays

php

echo

This is an array i have

<?php $page['Home']='index.html'; $page['Service']='services.html'; ?> 

How do i get to echo something like this for individual one like

Home is at index.html 

and again how can i do this through a loop and echo all?

like image 201
esafwan Avatar asked Aug 04 '10 14:08

esafwan


People also ask

How do you find the value of an array without a loop?

Either use array_column() for PHP 5.5: $foo = array(["type"=>"a"], ["type"=>"b"], ["type"=>"c"]); $result = array_column($foo, 'type'); Or use array_map() for previous versions: $result = array_map(function($x) { return $x['type']; }, $foo);

How do you find the key and value of an array?

The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

How do you echo all elements in an array?

<? php $colors = array("Red", "Green", "Blue", "Yellow", "Orange"); // Loop through colors array foreach($colors as $value){ echo $value . "<br>"; } ?>


1 Answers

foreach($page as $key => $value) {   echo "$key is at $value"; } 

For 'without loop' version I'll just ask "why?"

like image 184
Mchl Avatar answered Oct 02 '22 16:10

Mchl