-8

I have this array that the key of this, is string :

array
(
    [FJD] => 2.04104
    [MXN] => 20.47961
    [STD] => 20466.377105
    [SCR] => 21.203708
    [CDF] => 1975.464421
    [BBD] => 2.0
    [GTQ] => 7.770557
    [CLP] => 734.801634
)

How can I echo without foreach?

FJD: 2.04104
MXN: 20.47961
STD: 20466.377105
.
.
.
Sfili_81
  • 2,061
  • 5
  • 22
  • 34
Mohammad Aghayari
  • 952
  • 3
  • 13
  • 35
  • By doing the exact thing you said - `foreach` and `echo`. It's all pretty basic. Have you actually tried anything before posting the question? – El_Vanja Feb 01 '21 at 14:49
  • Without `foreach`? You could use `for`. Without iterating at all? You can't. Unless you're satisfied with one of the formats that `var_dump`, `print_r` or `var_export` offer. – El_Vanja Feb 01 '21 at 14:54
  • 1
    Why is your title named: `how to foreach the array with string key` – Example person Feb 01 '21 at 15:11

3 Answers3

2

You could use array_walk to apply a function to each member of the array if you don't want a foreach or for loop:

array_walk($array, function($value, $key) {
    echo $key.': '.$value.PHP_EOL;
});
cOle2
  • 4,606
  • 1
  • 24
  • 26
1
foreach ($array as $key => $value) {
   echo $key . ': ' . $value . "\n";
}
nikserg
  • 376
  • 1
  • 10
1

Try:

print_r($array);

Or:

var_dump($array);

Or:

var_export($array);

Or:
Full Credits to @cOle2:

array_walk($array, function($value, $key) {
    echo $key.': '.$value. "<br>";
});

Or, if you change your mind, you can always just use foreach:

foreach ($array as $key => $value) {
   echo $key . ': '. $value . "<br>";
}

Have a nice day cod3ing.

Example person
  • 2,832
  • 3
  • 12
  • 39