-3

I want to write my Array into a readable format.

$result = array_diff($one, $two);
print_r($result);
Array ( [1] => 298GS [2] => 09283 [3] => U4235 )

This is how it should look like:

298GS
09283
U4235

My only idea is to write it like this:

echo $result[1];
echo $result[2];
echo $result[3];

But this is not very useful because I never know how many values my array will have.

3 Answers 3

1

There isn't much to say, just loop through the array and show the values. This works for variable number of items

foreach($result as $r){
    echo $r."<br>";
}

Since you had difficulties doing such thing, I suggest you to study about the basics of the language (IF, lopps, variables, etc) - maybe that's what you are doing, IDK. Foreach and More.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, this works very well! Just one question to learn: Why did people downvote my question? Didn't I ask it right?
@Jarla Sometimes I don't see the reason for downvotes, but I'd guess that it's because something like this does not require too much search to find an answer (already answered). (First result for php show readable array Display an array in a readable/hierarchical format). Also, don't forget to accept my answer if that's what you were looking for ^^.
Thank you :) Yes I am a beginner, so it is all new :D
0

I use the following function as I can read the keys too, the output showed below is what you'll actually see so it is formatted.

function debugArray($arr)   
{
    return "<pre>" . var_export($arr, true) . "</pre>";
}

$arr = [
    1, 
    2,
    3,
    4,
    5
];

echo debugArray($arr);

Output

array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, )

Actual image of output.

Image for output.

Comments

0

You might want to check json_encode to export your data in json format so you can use it better with UI

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.