Delete Duplicates in Multidimensional Array [duplicate] - php

This question already has answers here:
How can you make a multidimensional array unique? [duplicate]
(6 answers)
Closed 5 years ago.
I have an Array which has 3 Dimensions:
$data_ary[$k_0][$v_0[4]]['...'] = ...
In the brackets with '...' I write Parameters like "Country", "Designation", "Year" and so on. Now as I run this through a loop the values for $k_0 increases every loop by 1 and $v_0[4] always changes the specific value.
My Problem: Very often the values are duplicates across the different dimensions. For Example:
$data_ary[1][1]['Country'] = 'Germany';
$data_ary[1][1]['Year'] = '2017';
$data_ary[2][1]['Country'] = 'Germany';
$data_ary[2][1]['Year'] = '2017';
How do I delete those duplicates? I have tried array_unique() but the result was that from the 27.000 entries 26999 got deleted..
Some Example Input:
$data_ary[6]['GERMANY']['YEAR'] = 2017;
$data_ary[6]['GERMANY']['MONTH'] = 1;
$data_ary[6]['GERMANY']['ID'] = 6010;
$data_ary[6]['GERMANY']['COUNTRY'] = 'GERMANY';
$data_ary[7]['ITALY']['YEAR'] = 2016;
$data_ary[7]['ITALY']['MONTH'] = 4;
$data_ary[7]['ITALY']['ID'] = 52752;
$data_ary[7]['ITALY']['COUNTRY'] = 'ITALY';
$data_ary[8]['GERMANY']['YEAR'] = 2017;
$data_ary[8]['GERMANY']['MONTH'] = 1;
$data_ary[8]['GERMANY']['ID'] = 6010;
$data_ary[8]['GERMANY']['COUNTRY'] = 'GERMANY';
As you can See the first and the second are basically the same but differ by the value in the first bracktes.

You can do it like below:-
$input = array_map("unserialize", array_unique(array_map("serialize", $data_ary)));
print_r($input);
Output:- https://eval.in/903355
Reference:- How to remove duplicate values from a multi-dimensional array in PHP

Related

How do I extracting the multidimensional array value in php? [duplicate]

This question already has answers here:
Looping a multidimensional array in php
(3 answers)
Output multidimensional array with keys and values
(3 answers)
Closed 4 months ago.
How do I get the value of the associative arrays for the year and quarter in a loop in php? I want to extract the numbers like 2021 4, the other value doesn't matter.
$this->financials[2021][4] = 5;
$this->financials[2022][1] = 7;
$this->financials[2022][2] = 9;
$this->financials[2022][3] = 11;
I attempted this method, but it didn't work, but hopefully gives you a better idea of what I am trying to achieve. I was hoping for the result e.g. 20214
foreach($this->financials as $f[$y][$q]) {
echo $y.$q;
}
$q in your foreach loop is an associative array so you have to break it down with another loop.
foreach ($this->financials as $year => $quarterArray) {
foreach ($quarterArray as $quarter => $value) {
echo $year.$quarter;
}
}

Remove duplicates from comma separated string and count total strings [duplicate]

This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 3 years ago.
im trying to remove from my string the comma separeted and multiples strings and then to count strings as total, i have no luck so.. any help will be apreciated, thanks!
Example names [alex,daniel,obama,alex,alex,alex,diana] = 7 names and without duplicates = 4 as total. This i'm trying to achive.
Here's the Php code:
$q = mysqli_query($db,"SELECT DISTINCT(names) FROM users WHERE uid = 1 ");
while ($data = mysqli_fetch_array($q)) {
$names = count($data['names']);
echo implode(',', array_keys(array_flip(explode(',',$names))));
}
If $data['names'] is a comma separated list of names, you can get only the unique values like this:
$names = array_unique(explode(',', $data['names']));
$count = count($names);
$names = implode(',', $names);
echo "$count names: $names\n";
For your sample data, this gives
4 names: alex,daniel,obama,diana
Demo on 3v4l.org

how to sort an array based on a value in php [duplicate]

This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 3 years ago.
I have some comments from customers, and I want to sort them by popularity. I can put them in an array to count each comment, but I want to order the comments by said count, how is this achieved?
I can make the arrays like this:
$arr['comment1'] = 4;
$arr['comment2'] = 2;
$arr['comment3'] = 6;
Or
$arr[] = array('comment'=>'comment1','count'=>4);
$arr[] = array('comment'=>'comment2','count'=>2);
$arr[] = array('comment'=>'comment3','count'=>6);
So I would like to re-order the array into this order - comment3, comment1, comment2 - i.e. in order of count. Is this possible?
You probably need asort() or arsort(). This will sort the array while keeping the keys with the proper values.
Ascending: $sorted = asort($arr);
Descending: $sorted = arsort($arr);

I have 5 arrays each array contains 10 values, I want to invert these arrays into 10 arrays and each array contains 5 values using php? [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
For Example:
$array_1 = [1,'a','b','c','d','e','f','g','h','i'];
$array_2 = [2,'aa','bb','cc','dd','ee','ff','gg','hh','ii'];
$array_3 = [3,'aaa','bbb','ccc','ddd','eee','fff','ggg','hhh','iii'];
$array_4 = [4,'aaaa','bbbb','cccc','dddd','eeee','ffff','gggg','hhhh','iiii'];
$array_5 = [5,'aaaaa','bbbbb','ccccc','ddddd','eeeee','fffff','ggggg','hhhhh',
'iiiii'];
Using PHP Script I want to convert it into
$array_1 = [1,2,3,4,5];
$array_2 = ['a','aa','aa a','aa aa','aa aa a'];
$array_3 = ['b','bb','bb b','bb bb','bb bb b'];
$array_4 = ['c','cc','cc c','cc cc','cc cc c'];
$array_5 = ['d','dd','dd d','dd dd','dd dd d'];
$array_6 = ['e','ee','ee e','ee ee','ee ee e'];
.
...
$array_10 = ['i','ii','iii','iiii','iiiii'];
Can anyone provide me a solution for that.
Simply you can use array_map like as
$array_1 = [1,'a','b','c','d','e','f','g','h','i'];
$array_2 = [2,'aa','bb','cc','dd','ee','ff','gg','hh','ii'];
$array_3 = [3,'aaa','bbb','ccc','ddd','eee','fff','ggg','hhh','iii'];
$array_4 =
[4,'aaaa','bbbb','cccc','dddd','eeee','ffff','gggg','hhhh','iiii'];
$array_5 =
[5,'aaaaa','bbbbb','ccccc','ddddd','eeeee','fffff','ggggg','hhhhh',
'iiiii'];
$result = array_map(null,$array_1,$array_2,$array_3,$array_4,$array_5);
print_r($result);
Demo

PHP - multidimensional array from arrays [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
I've got 6 arrays - 1 with name and 5 with some properties - which should be assigned to that name. All values are of course in order. I'd like to make a 2-dimensional array with will be later put into CSV and the result should be as on the table here:
I guess that i have to do 2 loops here, but I can't make them work. How to construct such array?
Solution found
I've connected all arrays:
$final_array = array($nazwa_array,$new_ilosc_array,$new_koszt_array,$new_cena_lifo_array,$new_cena_fifo_array,$new_rodzaj_array);
I've found a matrix transposition function, which returns array in correct order:
function transpose($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
$a = array();
foreach ( $names AS $key => $value ) {
$a[$key]['name'] = $value;
$a[$key]['property1'] = $value.'->'.$property1_array[$key];
$a[$key]['property2'] = $value.'->'.$property2_array[$key];
$a[$key]['property3'] = $value.'->'.$property3_array[$key];
$a[$key]['property4'] = $value.'->'.$property4_array[$key];
$a[$key]['property5'] = $value.'->'.$property5_array[$key];
}

Categories