redarrow Posted December 22, 2008 Share Posted December 22, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/ Share on other sites More sharing options...
.josh Posted December 22, 2008 Share Posted December 22, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721412 Share on other sites More sharing options...
redarrow Posted December 22, 2008 Author Share Posted December 22, 2008 I have read all them pages, and the loops and other array functions, make it imposable to understand. sorry but i like straight good looking code that works with one function not with other added function examples. just scrambles my brain . Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721420 Share on other sites More sharing options...
redarrow Posted December 22, 2008 Author Share Posted December 22, 2008 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/>"; # } # ?> Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721424 Share on other sites More sharing options...
Mad Mick Posted December 22, 2008 Share Posted December 22, 2008 To be honest I think the php.net examples are easier... For me the best thing is to have an awareness of what is possible with arrays and then when you need to do some array work have a look at php.net for the details. Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721429 Share on other sites More sharing options...
.josh Posted December 22, 2008 Share Posted December 22, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721430 Share on other sites More sharing options...
redarrow Posted December 22, 2008 Author Share Posted December 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721432 Share on other sites More sharing options...
redarrow Posted December 22, 2008 Author Share Posted December 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721437 Share on other sites More sharing options...
Mad Mick Posted December 22, 2008 Share Posted December 22, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721440 Share on other sites More sharing options...
redarrow Posted December 22, 2008 Author Share Posted December 22, 2008 look that looks good we all no that but you then need to no how to use it in a real life situation. if u read the comments your see what i mean. guru thrown in sub functions so confusing. Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721446 Share on other sites More sharing options...
.josh Posted December 22, 2008 Share Posted December 22, 2008 redarrow I wasn't poking at your 'bad english' I was poking at your coding style. Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721451 Share on other sites More sharing options...
redarrow Posted December 22, 2008 Author Share Posted December 22, 2008 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721455 Share on other sites More sharing options...
redarrow Posted December 22, 2008 Author Share Posted December 22, 2008 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721466 Share on other sites More sharing options...
redarrow Posted December 22, 2008 Author Share Posted December 22, 2008 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138030-learning-array-functions-need-help-and-examples-please/#findComment-721475 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.