PHP Tutorial #10 Array Methods (Array_Diff_Key, Array_Diff, Array_Intersect_Key and Array_Change_Key_Case )

Image Source

Repository

https://github.com/php/php-src  

What Will I Learn?

I will learn the array methods  (Array_Diff_Key, Array_Diff, Array_Intersect_Key and Array_Change_Key_Case ) with examples.

  • What's the array_diff_key / array_diff methods  and how to use them.
  • The definition of array_intersect_key method and their uses .
  • The concept of array_change_key_case method.

Requirements

  • Server support PHP , Xampp or Wamp for example
  • An IDE like Sublime text.
  • Browser (Chrome for example) 

Difficulty

  • Basic

Description 

1 – Array_Diff_Key  

The method compares the passed arrays keys as parameters and returns the difference between them. This function is similar to the array_diff () function, but it is compared by using keys, not values. 

It has at least two arrays as parameters and it returns a new array of values in the first array whose keys do not exist in any of the other arrays. 

This method checks only one dimension of multidimensional arrays. Of course you can check deeper dimensions using square brackets.  

To use the " array_diff_key " you must pass at least two arrays as parameters

array_diff_key($array1, $array2);

I have two arrays " Languages and ExistingLanguages " I want to print all languages exist in the world and not exist in my country

$languages = array(

"en" => "English",

"fr" => "French",

"es" => "Spanish"

);

$existingLanguages = array(

"fr" => "French"

);

print_r(array_diff_key($languages, $existingLanguages));

The array_diff_key will return an array containing all elements by key present in the first array and not present in the second array

2 – Array_Diff

The array_diff method compares the array with one or more other arrays and returns the values in them that are not in other arrays. 

It has at least two arrays as parameters and it returns a new array containing all elements in the first array that are not present in any of the other arrays.  

To use the " array_diff " you must pass at least two arrays as parameters

array_diff($array1, $array2);

I have a list of students and another list contains the successful students, I need the rest or the students who did not succeed

$students = array("Alexendre", "Alex", "Mick", "Arden", "John");

$studentsSucc = array("Alexendre", "Arden", "John");

echo "<h5>Students</h5>";

print_r($students);

echo "<h5>Succ Students</h5>";

print_r($studentsSucc);

echo "<h5>The Rest</h5>";

print_r(array_diff($students, $studentsSucc));

The array_diff will return all the elements by value exist in the first array and not in the second array , it means it will return the rest of students

3 – Array_Intersect_Key 

The method returns all elements of the array whose keys are in all other arrays. 

It has at least two arrays as parameters and it returns a linked array containing all elements of the existing array with all other arrays. 

It's like the array_intersect method but it's interested in comparing keys.

To use the " array_intersect_key " you must pass at least two arrays as parameters

array_intersect_key($array1, $array2);

I want to get the languages exist in the world and in my country and for that I need to use the array_intersect_key method

echo "<h3>Array_Intersect_Key</h3>";

print_r(array_intersect_key($languages, $existingLanguages));

The languages array contains " English, French and Spanish " , and the existingLanguages contains just the " French " and this is the result

4 – array_change_key_case 

This method returns an array that contains all the array keys passed as a parameter to the function after converting its case to large or small. Note that numeric keys will remain unchanged as they are. 

It has two parameters , the first parameters is the array The second parameters is the case which is the Latin case large CASE_UPPER or small CASE_LOWER . 

This method returns an array containing keys whose case has been converted to a large or small case, or the false value if the parameter type is not an array. If the array is empty it twill return an E-WARNING error.  

To use the array_change_key_case method we need to pass two parameters , the array and the choice between UPPER or lower

array_change_key_case($array, CASE_UPPER/LOWER);

I have an array and I want to change the case of keys to UPPER and LOWER 

echo "<h3>Array_Change_Key_Case</h3>";

$flippedArray = array_flip($languages);

print_r($flippedArray);

echo "<h5>UPPER</h5>";

print_r(array_change_key_case($flippedArray, CASE_UPPER));

echo "<h5>LOWER</h5>";

print_r(array_change_key_case($flippedArray, CASE_LOWER));

I have used the array_flip to be more clear, and this is the result

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/diff.php

Sort:  

Hello @alexendre-maxim

Thank you for your contribution to video-tutorial.

Overall, I see that you are trying to improve in your teaching.

You can pay more attention to the following:

  • speak more distinctly so that your words can be heard clearly
  • try not to rush from one word to another
  • Make your points more clear on the instructional level so that your lessons can be followed by learners
  • For your written format, use more simple sentences in the tutorial so that people can grasp the point without reading back and forth to understand what you are trying to say
  • I find that the font sizes are quite small in some sections, so if you can zoom in or make them larger so that people do not need to squint their eyes.

I look forward to your next video instruction.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @rosatravels! Keep up the good work!

Thank you @rosatravels , I will try to speak slowly and with understandable words.

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

Hi @alexendre-maxim!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @alexendre-maxim!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.032
BTC 63585.64
ETH 3035.86
USDT 1.00
SBD 3.84