73918

PHP - array unique by value in multidimensional array

Question:

I want to remove duplicates from input array <strong>by only value of user_id</strong>

<strong>Input array:</strong>

array (size=3) 0 => array (size=3) 'user_id' => string '3' (length=1) 'rate' => string '0' (length=1) 'note' => string 'a' (length=1) 1 => array (size=3) 'user_id' => string '3' (length=1) 'rate' => string '2' (length=1) 'note' => string 'b' (length=1) 2 => array (size=3) 'user_id' => string '4' (length=1) 'rate' => string '3' (length=1) 'note' => string 'c' (length=1)

<strong>Output:</strong>

array (size=3) 0 => array (size=3) 'user_id' => string '3' (length=1) 'rate' => string '0' (length=1) 'note' => string 'a' (length=1) 1 => array (size=3) 'user_id' => string '4' (length=1) 'rate' => string '3' (length=1) 'note' => string 'c' (length=1)

So far:

$result1 = array_unique($inputArray, SORT_REGULAR); $result2 = array_map("unserialize", array_unique(array_map("serialize", $inputArray)));

Answer1:

If you're running PHP >= 5.6 then the following will work:

$data = [ [ 'user_id' => '3', 'rate' => '0', 'note' => 'a', ], [ 'user_id' => '3', 'rate' => '2', 'note' => 'b', ], [ 'user_id' => '4', 'rate' => '3', 'note' => 'c', ], ]; $result = array_filter( $data, function ($value, $key) use ($data) { return $key === array_search($value['user_id'], array_column($data,'user_id')); }, ARRAY_FILTER_USE_BOTH ); var_dump($result);

Answer2:

This should work for you:

<?php $arr = array( array( 'user_id' => '3', 'rate' => '0', 'note' => 'a' ), array( 'user_id' => '3', 'rate' => '2', 'note' => 'b' ), array( 'user_id' => '4', 'rate' => '3', 'note' => 'c' ) ); $tmp = array(); foreach($arr as $key => $innerArray) { if(in_array($innerArray["user_id"], $tmp)) unset($arr[$key]); else $tmp[] = $innerArray["user_id"]; } print_r($arr); ?>

<strong>EDIT:</strong>

If you want a more <em>fancyer</em> version:

$tmp = array(); $result = array(); $result = array_values(array_filter(array_map(function($innerArray)use(&$tmp){ if(!in_array($innerArray["user_id"], $tmp)) { $tmp[] = $innerArray["user_id"]; return $innerArray; } }, $arr))); print_r($result);

Answer3:

try this:

$result=array();

foreach($yourArray as $innerArray) { if(in_array($innerArray["user_id"], $result)){ continue; } $result[] = $innerArray; } print_r($result);

Answer4:

If the user_id is just an int or a string, you could use it as the index, like this:

$a = [ [ 'id' => 1, ], [ 'id' => 2, ], [ 'id' => 3, ], [ 'id' => 1, ], ]; $b = array_reduce($a, function($carry, $item) { $carry[$item['id']] = $item; return $carry; }, []); var_dump($b); // Or more plainly like this: $b = []; foreach ($a as $val) { $b[$val['id']] = $val; } var_dump($b); // If you want numerical indexes, just do this $b = array_values($b);

Recommend

  • scraping javascript array [closed]
  • array+=value not work in ksh?
  • inserting record into mysql table if column count value is 2
  • Shared Data in pthread Programming
  • Magento 2 Data Migration Edit Product Unable to unserialize value
  • Fastest serialize data format form PHP reading
  • how to serialize phpseclib?
  • How to use PHP7's $mysqli->real_escape_string with an array
  • str_getcsv() alternative for older PHP version, gives me an empty array at the end
  • remove “” tags from an array in php
  • Put Values in Multidimensional Array keys
  • Store array in cookie
  • GODI-Batteries: Installation problems
  • How to do custom filtering in Datatables with comma separated values?
  • turn two nested associative arrays into one flat array?
  • Python: how to split and return a list from a function to avoid memory error
  • jQuery validate: submitting form only when fields are validated
  • java.lang.IllegalStateException: getInputStream() has already been called for this request + Struts2
  • Whats the best way of persisting data to Isolated Storage on Windows Phone 7?
  • PHP Listener Script for Paypal Webhooks
  • What Makes These Two Array Adds Different?
  • How to access profile picture with Facebook API in Swift 3?
  • Center align outputs in ipython notebook
  • Install PHP intl extension on MacOS
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Bad request using file_get_contents for PUT request in PHP
  • Refering to the class itself from within a class mehod in Objective C
  • Get data from AJAX - How to
  • ImageMagick, replace semi-transparent white with opaque white
  • Is there a javascript serializer for JSON.Net?
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • ActionScript 2 vs ActionScript 3 performance
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • How can I estimate amount of memory left with calling System.gc()?
  • Apache 2.4 - remove | delete | uninstall
  • A cron job substitute?
  • json Serialization in asp
  • Is it possible to post an object from jquery to bottle.py?