-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.

peace_love
  • 6,229
  • 11
  • 69
  • 157

3 Answers3

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.

FirstOne
  • 6,033
  • 7
  • 26
  • 45
  • 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? – peace_love Oct 23 '15 at 15:48
  • @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](http://stackoverflow.com/questions/5393085/display-an-array-in-a-readable-hierarchical-format)). Also, don't forget to accept my answer if that's what you were looking for ^^. – FirstOne Oct 23 '15 at 15:54
  • Thank you :) Yes I am a beginner, so it is all new :D – peace_love Oct 23 '15 at 16:08
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.

Script47
  • 14,230
  • 4
  • 45
  • 66
0

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

Oras
  • 1,036
  • 1
  • 12
  • 18