36

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?

esafwan
  • 17,311
  • 33
  • 107
  • 166

11 Answers11

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

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

Mchl
  • 61,444
  • 9
  • 118
  • 120
  • 1
    This is fine... just wanted to know if it possible to get a value individually by referring it to as 0 or 1 etc... I have just started with these stuffs. Thank You very much!! – esafwan Aug 04 '10 at 15:28
  • 2
    that's a very common thing actually, to address an array items individually. – Your Common Sense Aug 04 '10 at 15:37
  • Oh, OK. I understood your question as 'how to echo all key-value elements from an array without using a loop' - which would be silly thing to do IMHO (unless using array_walk like KennyTM did). – Mchl Aug 04 '10 at 15:43
  • 1
    If your array consist only of one key and one value, it would be convenient not to have to use the loop. If you don't know the key: `key($page).' is at '.$page[key($page)];` – Simon Jul 18 '18 at 11:38
16

Without a loop, just for the kicks of it...


You can either convert the array to a non-associative one, by doing:

$page = array_values($page);

And then acessing each element by it's zero-based index:

echo $page[0]; // 'index.html'
echo $page[1]; // 'services.html'

Or you can use a slightly more complicated version:

$value = array_slice($page, 0, 1);

echo key($value); // Home
echo current($value); // index.html

$value = array_slice($page, 1, 1);

echo key($value); // Service
echo current($value); // services.html
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
8

If you must not use a loop (why?), you could use array_walk,

function printer($v, $k) {
   echo "$k is at $v\n";
}

array_walk($page, "printer");

See http://www.ideone.com/aV5X6.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
6

Echo key and value of an array without and with loop

$array = array(
            'kk6NFKK'=>'name',
            'nnbDDD'=>'claGg',
            'nnbDDD'=>'kaoOPOP',
            'nnbDDD'=>'JWIDE4',
            'nnbDDD'=>'lopO'
         );


print_r(each($array));  

Output

Array
(
    [1] => name
    [value] => name
    [0] => kk6NFKK
    [key] => kk6NFKK
)
Jørgen R
  • 10,568
  • 7
  • 42
  • 59
avi
  • 61
  • 1
  • 1
  • 1
    The `each()` function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged. Source: [php.net](https://www.php.net/manual/en/function.each.php) – Genki Aug 30 '20 at 07:15
5

for the first question

$key = 'Home';
echo $key." is at ".$page[$key];
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
3
function displayArrayValue($array,$key) {
   if (array_key_exists($key,$array)) echo "$key is at ".$array[$key];
}

displayArrayValue($page, "Service"); 
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
2

How to echo key and value of an array without and with loop

$keys = array_keys($page);
implode(',',$keys);
echo $keys[0].' is at '.$page['Home'];
cloud
  • 21
  • 1
1

My version without a loop would be like this:

echo implode(
    "\n", 
    array_map(
         function ($k, $v) { 
             return "$k is at $v"; 
         }, 
         array_keys($page), 
         array_values($page)
    )
);
Karol Samborski
  • 2,757
  • 1
  • 11
  • 18
0

A recursive function for a change;) I use it to output the media information for videos etc elements of which can use nested array / attributes.

function custom_print_array($arr = array()) {
    $output = '';
    foreach($arr as $key => $val) {
        if(is_array($val)){
            $output .= '<li><strong>' . ucwords(str_replace('_',' ', $key)) . ':</strong><ul class="children">' . custom_print_array($val) . '</ul>' . '</li>';
        }
        else {
            $output .=  '<li><strong>' . ucwords(str_replace('_',' ', $key)) . ':</strong> ' . $val . '</li>';
        }
    }
    return $output;
}
Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
Shivanand Sharma
  • 434
  • 3
  • 13
0
array_walk($v, function(&$value, $key) {
   echo $key . '--'. $value;
 });

Learn more about array_walk

Omair
  • 41
  • 1
-2

You can try following code:

foreach ($arry as $key => $value) 
{
      echo $key;
      foreach ($value as  $val) 
      {
         echo $val; 
      }
}
Ankur Tiwari
  • 2,762
  • 2
  • 23
  • 40
mayur
  • 1