how to remove the duplicate item from the object in PHP? - php

here's the sample array
array(
0 => blah object
(
[bagid] => 12345
[userid] => 12345,
and so on and so forth..
)
)
this is the output when I var_dump the $data object, if i loop it through
foreach loop,it will print the bagid,userid,and etc....of the owner of the data..
now the question is, I only want to display 1 unique bagid coming from a user...
no matter how many bagid the user has, is that doable ?how?

use array_unique
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
output :
Array
(
[a] => green
[0] => red
[1] => blue
)
http://php.net/manual/en/function.array-unique.php

Related

Remove duplicate array on the base of key in multidimendional array PHP [duplicate]

This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 6 years ago.
From following array key with ID 16 is comming twice. Hown can we remove this this duplicate ID. (REMOVE IF ID IS DUPLICATE OTHER FIELDS IGNORE)
Array
(
[1] => Array
(
[ID] => 16
[username] => dudda
[message-time] => 2016-08-25 12:12:53
)
[2] => Array
(
[ID] => 16
[username] => dudda
[message-time] => 2016-08-25 12:01:54
)
[3] => Array
(
[ID] => 3
[username] => himanshu
[message-time] => 2016-08-15 12:53:38
)
[4] => Array
(
[ID] => 15
[username] => dawinder
[message-time] => 2016-08-10 11:40:33
)
)
I Got solution
I have develop this function for same :
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
Now, call this function anywhere from your code,
something like this,
$details = unique_multidim_array($array_name,'key');
We used this to de-duplicate results from a variety of overlapping queries.
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
Read this:http://php.net/manual/en/function.array-unique.php
For Example
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
Output will be:
Array
(
[a] => green
[0] => red
[1] => blue
)

Handling multi-dimensional PHP Array

I have arrays of fonts with their name and weight e.g.
print_r($fontarray1);
Array (
[font] => Open+Sans
[weight] => normal
)
print_r($fontarray2);
Array (
[font] => Open+Sans
[weight] => bold
)
print_r($fontarray3);
Array (
[font] => Lato
[weight] => bolditalic
)
I have many arrays of fonts $fontarray1, $fontarray2, $fontarray3.. I want to make one final array of fonts and their weights.. for example, if "Open+Sans" is already in the final array, it should not be added, but if "Open+Sans" is in final Array with a different "weight" then only the "weight" should be added alongwith the "Open+Sans" key.. the expected result should be something like:
Array (
[font] => Oen+Sans
[weight] => Array (
[0] => normal
[1] =>bold
)
[font] => Lato
[weight] => Array (
[0] => bolditalic
)
)
I tried something like below, but confused on how to add another array within one array..:
$final_arr = Array();
if (!in_array($fontarray1['font'], $final_arr)) {
$final_arr []= $fontarray1['font'];
} else {
// already in array, lets check font weight
}
}
any help or thought would be highly appreciated. cheers
First
Use array_merge
<?php
$beginning = 'foo';
$end = array(1 => 'bar');
$result = array_merge((array)$beginning, (array)$end);
print_r($result);
?>
Second
Use array_unique
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
Look` like structure like this can be usefull for you:
Array(
"fontName" => Array(
[weight] => Array(
[0] => "normal"
)
)
)
And your function would be like
$final_arr = Array();
$fontName = $fontarray1['font'];
$fontWeight = $fontarray1['weight'];
if (!isset($final_arr[$fontName])) {
$final_arr[$fontName] = array('weight' => $fontWeight);
} else {
$final_arr[$fontName]['weight'][] = $fontWeight;
}
You're just trying to add $fontarray1 to $final_arr?
$final_arr[] = $fontarray1
Creating a multidimensional array:
$theArray = array();
$theArray[] = array('key1'=>'val1', 'key2'=>'val2'); // creates a subarray

php array find duplicates, sum them up & delete duplicates

i have an array:
Array
(
[0] => Array
(
[setid] => 2
[income] => 100
)
[1] => Array
(
[setid] => 2
[income] => 120
)
[2] => Array
(
[setid] => 3
[income] => 700
)
)
i need to find entrys with the same setid, sum their income up and delete duplicate entrys - in the end it should look like this:
Array
(
[0] => Array
(
[setid] => 2
[income] => 220
)
[1] => Array
(
[setid] => 3
[income] => 700
)
)
does someone know a sophosticated solution for my problem or do i have to take the long road and do every step manually?
thanks and greetings
Just create a new array which you make fast adressable by using the setid as key. And reindex the array at the end.
$result = array();
foreach ($array as $val) {
if (!isset($result[$val['setid']]))
$result[$val['setid']] = $val;
else
$result[$val['setid']]['income'] += $val['income'];
}
$result = array_values($result); // reindex array
This should work:
$values = array();
foreach($array as $val) {
if(isset($values[$val['setid']])) {
$values[$val['setid']] += $val['income'];
} else {
$values[$val['setid']] = $val['income'];
}
}
//all values are now in $values array, keys are setid and values are income
Write your own function, that's no long road at all.
$result = array_reduce($originalArray, function($memo, $item){
isset($memo[$item['setid']])
? $memo[$item['setid']] = $item['income']
: $memo[$item['setid']] += $item['income'];
return $memo;
}, []);
You should use the array_unique function that php's official site offers.
An example:
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
Output:
Array
(
[a] => green
[0] => red
[1] => blue
)
To sum the duplicated values you can use the array_count_values function:
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Output would be:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)

php building / populating an Array of Arrays

I have seen alot of tutorial on how to get the data out of a 2D array but I need to build one similar to this and I have not found any logic I can follow:
$array = array("socks" => array("blue", "red", "green"),
"shirts" => array("small", "medium", "large"));
I cant seem to figure out the logic to even start the code....
for each clothingType // I did this
get options // I did this
for each option //I did this
add to the clothingOption Array //... help!
Im only stuck on the building of the clothingOption 2D Array
could it be someting like
foreach clothingType as $kClothes =>VClothes
get Options
for each Options as $kOptions =>$VOption
$array[$VClothes][]= $VOption
Thanks and I hope this is not too vague....
How about the following?
<?php
function getMe($type) {
//do some processing and construct array
return array('a','b','c');
}
$myTypes = array('type1','type2','type3');
$answer = array();
foreach($myTypes as $val) {
$answer[$val] = getMe($val);
}
print_r($answer);
?>
Output:
Array ( [type1] => Array ( [0] => a [1] => b [2] => c ) [type2] => Array ( [0] => a [1] => b [2] => c ) [type3] => Array ( [0] => a [1] => b [2] => c ) )
maybe (sorry for errors - by hand):
//$array = array("socks" => array("blue", "red", "green"),
// "shirts" => array("small", "medium", "large"));
$types = array('socks', 'shirts');
$socks = array("blue", "red", "green");
$shirts = array("small", "medium", "large");
$array = array();
foreach ($types as $type) {
foreach($$type as $cloth) {
$array[$type][] = $cloth;
}
}
print_r($array);

Merging multiple PHP arrays

My problem is this: I'm creating an Array in order to store these 2 types of 'refinement'. However, what is happening is as the information is collected from the database, each 'refinement' is assigned to it's own specific entry when the arrays are created within the while loop.
while($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)){
etc...
}
So for instance, the 1st array would be a reference to 'Die Hard' and the 2nd, 'Breaking Bad' and the 3rd, 'Greys Anatomy'. What i'm trying to achieve is to merge them into 1 single array.
Array
(
[genreType] => Action
[mediaType] => Film
)
Array
(
[genreType] => Action
[mediaType] => TV
)
Array
(
[genreType] => Drama
[mediaType] => TV
)
Thanks for any help.
Try looking at this, http://php.net/manual/en/function.array-merge.php
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
OUTPUT
Array (
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
Why can't you just use array_merge? from the php docs http://php.net/manual/en/function.array-merge.php
while{$row...
{
$movies[] = $row;
//OR
$tmp['genreType'] = $row['genre'];
//and the like.....
$movies[] = $tmp;
}
Resulting in
$movies = array(
array(
"title"=>"Die Hard",
"genreType"=>"Action",
"mediaType"=>"Film"
),
array(
"title"=>"some other movie",
"genreType"=>"Comedy",
"mediaType"=>"TV"
)
);
like this?

Categories