Jump to content

Learning array functions need help and examples please.


redarrow

Recommended Posts

Advance thank you. Have a grate xmas all.

 

I am currently going throw all the array functions, i need to learn them all,

as i have noticed there very important,

 

I have the current array functions below i am learning right now.

 

Can anybody give grate examples, if you got time, to the functions below.

 

Real life example be grate but please detail the code right out.

 

As you can see, I sort off understand them, but not all thank you.

 

Even the ones i understand, grate real life examples be grate thank you.

<?php

$a=array("mum","dad","brother","son");
$b=array("john","dad","mum","john");

$c=array_diff($a,$b); // echos the values of the ellements that are dirrent in the array. 
$d=array_intersect($a,$b); // echos the vales that are the same in the array.
$e=array_unique($b);  // echos the values deletes the same value found.
$f=array_merge($a,$b); // merge the two values together.
$g=array_shift($a); //need help
$h=array_push($a,$b); //need help.
$i=array_pop($a); //need help
$j=array_unshift($a,$b); //need help

var_dump($g);

?>

Link to comment
Share on other sites

Coincidentally, I wrote a very in-depth explanation of all those.

 

I also give a few examples, and even added a comment system

 

so people can post clever uses or limitations of them. 

 

You can find all this at

 

http://www.php.net/manual

 

I even made it to where you can put for instance,

 

http://www.php.net/array_diff

 

and it will take you right to the page about it. 

 

And it's all for free!

Link to comment
Share on other sites

example look how grate and easy it to learn array's

 

no added sub array functions, just learning what you want,i  need some think like this to understand

the array functions, i have posted easy and straight froward like this code.

 

<?php
#
// Creating an array
#
$colorList = array("apple"=>"red",
#
"grass"=>"green",
#
"sky"=>"blue",
#
"night"=>"black",
#
"wall"=>"white");
#
#
//Display array item
#
echo "The sky is ".$colorList["sky"]
#
." and the grass is ".$colorList["grass"];
#
#
#
// Display array size
#
echo "The array size is: ".sizeof($colorList);
#
#
// Remove one element from the array
#
unset($colorList["sky"]);
#
echo "The new array size is: ".sizeof($colorList);
#
#
echo "";
#
#
// Check the existence of an element
#
if (isset($colorList["grass"])) echo "grass key is present";
#
else echo "grass key is not present";
#
#
if (isset($colorList["sky"])) echo "sky key is present";
#
else echo "sky key is not present";
#
#
// Display the complete array content
#
echo "Array before sorting:";
#
print_r($colorList);
#
// Display the complete array content after sorting
#
echo "Array after asort:";
#
print_r (asort($colorList));
#
echo "Array after sort:";
#
print_r (sort($colorList));
#
#
#
// Creating a multidimensional array
#
$myLists['colors'] = array("apple"=>"red",
#
"grass"=>"green",
#
"sky"=>"blue",
#
"night"=>"black",
#
"wall"=>"white");
#
#
$myLists['cars'] = array("BMW"=>"M6",
#
"Mercedes"=>"E 270 CDI",
#
"Lexus"=>"IS 220d",
#
"Mazda"=>"6",
#
"Toyota"=>"Avensis");
#
#
// Display an item from the array
#
echo "A demo item is:".$myLists['cars']['Toyota'];
#
#
// Create a new array
#
$colorList2[] = "red";
#
$colorList2[] = "green";
#
$colorList2[] = "blue";
#
$colorList2[] = "black";
#
$colorList2[] = "white";
#
#
// DUmp it's content
#
echo "Dump colorList2 with print_r:<br/>";
#
print_r($colorList2);
#
echo "Dump colorList2 with var_dump:<br/>";
#
var_dump($colorList2);
#
echo "";
#
#
// Display array elements from loop
#
echo "Array content:<br/>";
#
for ($i=0;$i<=4;$i++){
#
echo $colorList2[$i]."";
#
}
#
#
// Display array elements with foreach
#
echo "Array content:";
#
foreach ($colorList2 as $value) {
#
echo $value."<br/>";
#
}
#
?>

Link to comment
Share on other sites

wait wait wait.  Are you seriously suggesting that that example code you just gave, is easier to read and understand than what's in the manual?

 

All this time I thought you wrote like that because you just didn't know any better.  Now that I know that you actually do that on purpose...man, I just don't know what to think about that...

Link to comment
Share on other sites

That what php.net is for, but it dose not solve the problem in wanting to learn each array function properly,

your notice on php.net that they add extra sub functions on top.

 

example want to learn array_diff have a look your sourounded with guru work with thrown in extra array functions.

 

it like lets now confuse the reader.

 

not lets help him.

Link to comment
Share on other sites

Crayon Violent .. << your winding me up, u no i find it all hard i am trying to better my self.

 

you no my written is bad but i try.

 

sorry but i do find the posted example easier then php.net well most off it.

 

only because ive read also 30 books lol.

 

 

Link to comment
Share on other sites

Are you reading the right manual?  This is the array_diff example.  I have never used it before but it seems clear to me what it does from the example.

 

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

 

Result:

 

Array
(
    [1] => blue
)

 

Yes there are a few pitfalls but it is enough to be aware of them now rather than get in too deep or you will never progress...

Link to comment
Share on other sites

Crayon Violent << u always been good to me anyway your grate mean that.

 

Like this now i am learning array_diff and array_unique as real life example.

<?php
$a1=array("first", "second", "third","fourth","fifth");
$a2=array("second","fourth","sixth","second");

$new_array=array_diff($a1,array_unique($a2));

while (list ($key, $val) = each ($new_array)) {
echo "$key -> $val <br>";
}
?>

 

array_merge array_unique array_diff

 

slowly getting there understanding how to use them together.

<?php
$a1=array("first", "second", "third","fourth","fifth");
$a2=array("second","fourth","sixth","second");

$new_array=array_diff($a1,array_merge(array_unique($a2)));

while (list ($key, $val) = each ($new_array)) {
echo "$key -> $val <br>";
}
?>

Link to comment
Share on other sites

 

array_diff  array_unique  array_shift  array_push  array_merge

 

so far so good getting there.

<?php

$a1=array("first", "second", "third","fourth","fifth");

$a2=array("second","fourth","sixth","second");

$new_array=array_shift($a1);

$new_array=array_push($a1,'first');

$new_array=array_diff($a1,array_merge(array_unique($a2)));

while (list ($key, $val) = each ($new_array)) {
echo "$key -> $val <br>";
}
?>

Link to comment
Share on other sites

last one

<?php

$a1=array("first", "second", "third","fourth","fifth");

$a2=array("second","fourth","sixth","second");

$new_array=array_shift($a1);

$new_array=array_push($a1,'first');

$new_array=array_pop($a1);

$new_array=array_unshift($a1,'first','secound');

$new_array=array_diff($a1,array_merge(array_unique($a2)));

while (list ($key, $val) = each ($new_array)) {
echo "$key -> $val <br>";
}
?>

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.