All TalkersCode Topics

Follow TalkersCode On Social Media

PHP Array Same Key Different Value

Last Updated : Jul 1, 2023

PHP Array Same Key Different Value

In this article we will show you the solution of PHP array same key different value, the array is a homogeneous collection of elements. In php, there are three types of arrays-

Indexed array: this type of array contain some values.


$array= array(‘value1’, ‘value2’, ‘value3’…) ;

Associative array: the associative array containing key pair values.


$array= array(‘key1’=>‘value1’,’key1’=>‘ ‘value2’, ..…);

Multidimensional array: this type of array contained multiple array.


$array = array (
'1' => array ('key1' => 'value1', 'key2' => 'value2') ,
'2' => array ('key1' => 'value1', 'key2' => 'value2'),..

Now if multiple array contains the same key but got different values we can compare the arrays and got return the different values with the array_diff_key() function.

Step By Step Guide On PHP Array Same Key Different Value :-

in the example below we compared three different types of arrays and got the different values using array_diff_key() function.

<!DOCTYPE html>
<html lang = " en " >
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> php array same key different value </title>
    <link rel="stylesheet" href="">
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> php array same key different value </h2>
$first = array('a' => 'yellow', 'b' => 'pink', 'c' => 'white', 1 => 'blue', 'green') ;
$second = array ('a' => 'black', 1 =>'red') ;
$third = array ('green') ;
echo '<pre>' ;
print_r ($first) ;
echo '<pre>' ;
print_r ($second) ;
echo '<pre>' ;
print_r ($third) ;
echo '<pre>' ;
print_r (array_diff_key ($first, $second, $third)) ;
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Attach an external CSS file using <link> tag
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. <h1> tag used to add heading here and also adding the inline CSS here.
  8. Close the HTML tag with </html>
  9. At first <?php tag to write the php.
  10. Create three different arrays.
  11. $first with some key pair value, $second with the same key and different values as some of the keys of $first. Lastly $third with some value without the key
  12. First print all the arrays using print_r()
  13. Then using array_diff_key() function with print_r() to display the values with different keys.
  14. Close the php code with ?> tag

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to array the same key different values using PHP.

I hope this article on PHP array same key different value helps you and the steps and method mentioned above are easy to follow and implement.