-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
.
.
.
3
  • By doing the exact thing you said - foreach and echo. It's all pretty basic. Have you actually tried anything before posting the question? Commented Feb 1, 2021 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. Commented Feb 1, 2021 at 14:54
  • 1
    Why is your title named: how to foreach the array with string key Commented Feb 1, 2021 at 15:11

3 Answers 3

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;
});
Sign up to request clarification or add additional context in comments.

Comments

1
foreach ($array as $key => $value) {
   echo $key . ': ' . $value . "\n";
}

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.