Jump to content

arrays


Ninjakreborn

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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.