Hướng dẫn php array unique multidimensional

Why are you making it so hard,

 $in = array [
    1 => 
    array [
        'country' => 'US',
        'state' => 'Albama',
        'city' => 'Brest',
        'postcode' => '225001-225003',
        'shipping_info' => 'DeliveryAvailable',
        'is_zip_range' => 1,
        'zip_from' => 225001,
        'zip_to' => 225003
    ],
    2 => 
    array [
        'country' => 'BY',
        'state' => 'Brest',
        'city' => 'Brest',
        'postcode' => '225001-225003',
        'shipping_info' => 'DeliveryAvailable',
        'is_zip_range' => 1,
        'zip_from' => 225001,
        'zip_to' => 225003
    ]
];

$out = [];
foreach[$in as $i] if[!isset[$out[$i['postcode']]]] $out[$i['postcode']] = $i;

Sandbox

You can do the same thing with in_array, but the isset is faster.

In fact you dont even need to do the isset

foreach[$in as $i] $out[$i['postcode']] = $i;

Array keys are always unique, but this will retain the last duplicate where as the previous code keeps the first one.

And if the keys bug you latter just do $out = array_values[$out] to reset them.

Last update on August 19 2022 21:50:29 [UTC/GMT +8 hours]

PHP Array: Exercise-38 with Solution

Write a PHP function to create a multidimensional unique array for any single key index.

Sample Solution:

PHP Code:



Sample Output:

Array                                                       
[                                                           
    [0] => Array                                            
        [                                                   
            [city_id] => 1                                  
            [name] => Sara                                  
            [mobile_num] => 1111111111                      
        ]                                                   
                                                            
    [1] => Array                                            
        [                                                   
            [city_id] => 2                                  
            [name] => Robin                                 
            [mobile_num] => 2222222222                      
        ]                                                   
                                                            
]

Flowchart:

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP script to count the total number of times a specific value appears in an array.
Next: Write a PHP program to remove duplicate values from an array which contains only strings or only integers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

PHP: Tips of the Day

PHP: Check if PHP session has already started

Recommended way for versions of PHP >= 5.4.0, PHP 7

if [session_status[] == PHP_SESSION_NONE] {
    session_start[];
}

For versions of PHP < 5.4.0

if[session_id[] == ''] {
    session_start[];
}

Ref : //bit.ly/31TMQuq

  • Exercises: Weekly Top 12 Most Popular Topics
  • Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • C Programming Exercises, Practice, Solution : For Loop
  • Python Exercises, Practice, Solution
  • Python Data Type: List - Exercises, Practice, Solution
  • C++ Basic: Exercises, Practice, Solution
  • SQL Exercises, Practice, Solution - exercises on Employee Database
  • SQL Exercises, Practice, Solution - exercises on Movie Database
  • SQL Exercises, Practice, Solution - exercises on Soccer Database
  • C Programming Exercises, Practice, Solution : Recursion

Chủ Đề