In the below array linked_article_id
has value 3 two times but I want to remove one entry from this array (i.e I want only one unique linked_article_id
value). I tried the below function but did't work for me:
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Array:
Array ( [0] => Array ( [id] => 193 [linked_article_id] => 2 [article_id] => 1 [article_title] => Test header link [slug] => test-header-link )
[1] => Array ( [id] => 195 [linked_article_id] => 3 [article_id] => 1 [article_title] => upload image test [slug] => upload-image-test )
[2] => Array ( [id] => 197 [linked_article_id] => 4 [article_id] => 1 [article_title] => Adrenal Fatigue [slug] => arrowdesigns01-test )
[3] => Array ( [id] => 200 [linked_article_id] => 9 [article_id] => 1 [article_title] => Recipe2 [slug] => recipe2 )
[4] => Array ( [id] => 211 [linked_article_id] => 15 [article_id] => 1 [article_title] => New [slug] => new )
[5] => Array ( [id] => 214 [linked_article_id] => 3 [article_id] => 1 [article_title] => upload image test [slug] => upload-image-test )
)
Serialization is used to compare if the entire arrays are indentical, not just one key.
You can extract the array and index (which must be unique) by linked_article_id
:
$input = array_column($input, null, 'linked_article_id');
If you really need to re-index after (optional):
$input = array_values(array_column($input, null, 'linked_article_id'));
I don't see how anyone could be running PHP < 5.5.0 now, but just in case:
foreach($input as $v) {
$result[$v['linked_article_id']] = $v;
}
Then if needed:
$result = array_values($result);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments