Jump to content

Archived

This topic is now archived and is closed to further replies.

Ninjakreborn

arrays

Recommended Posts

I need to figure something out, I was messing around with some code in arrays, and I set up something here to make this more readable, is there an easier way to do this.
below is my code and below that is my output into the browser.
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>php test page</title>
</head>
<body>
<?php
$array1 = array("a"=>"Dave", "b"=>"Jim", "c"=>"Walton", "d"=>"Peter");
$array2 = array("a"=>"Jessy", "b"=>"Mark", "c"=>"Tim", "d"=>"Fairy");
$test1 = array_intersect($array1, $array2);
$test2 = array_diff($array1, $array2);
$test3 = array_merge($array1, $array2);
?>
<pre>
<?php
var_dump($test1);
?>
</pre>
<pre>
<?php
var_dump($test2);
?>
</pre>
<pre>
<?php
var_dump($test3);
?>
</pre>


</body>
</html>
[/code]

And below here is what it outputs into the browser., but is there an easier way to write this code, writing it standard just drops it in a bunch of heavily unreadable lines.
[code]
array(0) {
}
array(4) {
  ["a"]=>
  string(4) "Dave"
  ["b"]=>
  string(3) "Jim"
  ["c"]=>
  string(6) "Walton"
  ["d"]=>
  string(5) "Peter"
}
array(4) {
  ["a"]=>
  string(5) "Jessy"
  ["b"]=>
  string(4) "Mark"
  ["c"]=>
  string(3) "Tim"
  ["d"]=>
  string(5) "Fairy"
}

[/code]

Share this post


Link to post
Share on other sites
If your question is about var_dump() you can make your own prettier output using a foreach loop. See foreach in the php manual.

Share this post


Link to post
Share on other sites
... or if you just want to see contents of arrays without the type and size info, use print_r() instead of var_dump().

[code]echo '<pre>',
       print_r($test1, true),
       print_r($test2, true),
       print_r($test3, true),
       '</pre>';[/code]

-->
[code]Array
(
)
Array
(
    [a] => Dave
    [b] => Jim
    [c] => Walton
    [d] => Peter
)
Array
(
    [a] => Jessy
    [b] => Mark
    [c] => Tim
    [d] => Fairy
)
[/code]

Share this post


Link to post
Share on other sites
ah so that is what print_r was for, I want to experiment with it some on non-array variables later and see what I can come up with too.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.