0

I have an array and I want to show the values of the array as part of an error message. But of course when I do the code below I just get my error message with array at the end. Please help

$matches = array("2","35","27");

Now I just want to show the values to in a error message.

if (isset($matches)){

    $error_message = "The following numbers match: " . $matches;

}

echo $error_message;

Result:

The following numbers match: 2 35 27

2 Answers 2

2

The simplest way I can think of is using implode. You may want to do an is_array check, but this should work.

$error_message = "The following numbers match: " . implode(' ', $matches);
Sign up to request clarification or add additional context in comments.

1 Comment

I dont know why I was thinking I had to loop through the values. That was very helpful. Thanks!
1

Try this code:

<?php
$matches = array("2","35","27");
if (isset($matches)){
    $error_message = "The following numbers match: " . var_export($matches, true);
}
echo $error_message . "\n";

?>

OUTPUT

The following numbers match: array (
  0 => '2',
  1 => '35',
  2 => '27',
)

Look at the var_export manual here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.