Remove duplicate elements in array - php

i have search SO but most of the results is similar to How to remove duplicate values from a multi-dimensional array in PHP
i've tried array_unique, but does not work.
Array
(
[0] => Array
(
[0] => ball_24.gif
[1] => ball_8.gif
[2] => ball_1.gif
[3] => ball_33.gif
[4] => ball_43.gif
[5] => ball_1.gif
[6] => ball_8.gif
[7] => ball_24.gif
[8] => ball_33.gif
[9] => ball_43.gif
)
)

$array[0] = array_unique($array[0]);

Related

How to convert array first value as key and second value as value

Hi I am working on some array operations.
I need to convert first value of array as key and second value of array as value.
I have one variable $testArray which stores array like below.
Array
(
[0] => Array
(
[0] => Color
[1] => White on Red
)
[1] => Array
(
[0] => Depicted Text
[1] => EMPTY
)
[2] => Array
(
[0] => Depth [Nom]
[1] => 0.004 in
)
[3] => Array
(
[0] => Language
[1] => English
)
[4] => Array
(
[0] => Length [Nom]
[1] => 10 in
)
[5] => Array
(
[0] => Material
[1] => Adhesive Vinyl
)
[6] => Array
(
[0] => Mounting
[1] => Surface
)
[7] => Array
(
[0] => Width [Nom]
[1] => 14 in
)
[8] => Array
(
[0] => Wt.
[1] => 0.056 lb
)
)
Expected output :
Array
(
[0] => Array
(
[Color] => White on Red
)
[1] => Array
(
[Depicted Text] => EMPTY
)
[2] => Array
(
[Depth [Nom]] => 0.004 in
)
[3] => Array
(
[Language] => English
)
[4] => Array
(
[Length [Nom]] => 10 in
)
[5] => Array
(
[Material] => Adhesive Vinyl
)
[6] => Array
(
[Mounting] => Surface
)
[7] => Array
(
[Width [Nom]] => 14 in
)
[8] => Array
(
[Wt.] => 0.056 lb
)
)
I have already tried with array function array_keys and array_values but it won't working
Simple solution using array_map function:
$result = array_map(function($v){
return [$v[0] => $v[1]];
}, $testArray);
Assuming that structure will always be the same, you could do this:
$output = array();
foreach($testArray as $v){
$output[] = array($v[0] => $v[1]);
}
See it in action here.

Convert a Multidimensional array to single Dimension

This is the array as I currently have,please convert it to the below array with single dimensional array:-
Array
(
[0] => Array
(
[is_custom] => yes
)
[1] => Array
(
[custom_amount] => 45
)
[2] => Array
(
[custom_amount_text] => Enter Amount
)
[3] => Array
(
[amount_btn] => Dropdown
)
[4] => Array
(
[multiple_amounts] => W3sidGl0bGUiOiJMYWJlbCIsImFtb3VudCI6IjQ1In0seyJ0aXRsZSI6IkxhYmVsIiwiYW1vdW50IjoiNDU1In1d
)
[5] => Array
(
[recurring_plans] => W3sibmFtZSI6IkxhYmVsIiwicmVjdXJyaW5nX2Ftb3VudCI6Ijc4In0seyJuYW1lIjoidSIsInJlY3VycmluZ19hbW91bnQiOiI3ODgifV0=
)
[6] => Array
(
[recurring_interval] => WyJNb250aGx5IiwiUXVhcnRlcmx5IiwiSGFsZi1ZZWFybHkiXQ==
)
[7] => Array
(
[admin_mail_subject] =>
)
[8] => Array
(
[admin_mail_body] =>
)
[9] => Array
(
[user_mail_subject] =>
)
[10] => Array
(
[user_mail_body] =>
)
[11] => Array
(
[is_recurrance] => yes
)
[12] => Array
(
[is_onetime] => yes
)
)
Now I want to convert this into something like this
Array([0]=>
[is_custom] => yes
[custom_amount] => 45
[custom_amount_text] => Enter Amount
[amount_btn] => Dropdown
[multiple_amounts] => W3sidGl0bGUiOiJMYWJlbCIsImFtb3VudCI6IjQ1In0seyJ0aXRsZSI6IkxhYmVsIiwiYW1vdW50IjoiNDU1In1d
[recurring_plans] => W3sibmFtZSI6IkxhYmVsIiwicmVjdXJyaW5nX2Ftb3VudCI6Ijc4In0seyJuYW1lIjoidSIsInJlY3VycmluZ19hbW91bnQiOiI3ODgifV0=
)
Array 0 key will have all the keys can It be possible anyhow.I tried but failed.
Please help me with a possible solution
Use RecursiveIteratorIterator for fast and simple.
$output_array = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($input_array)), 0); //$input_array-Replace your arrray
echo "<pre>";
print_r($output_array);
echo "</pre>";
Result:
Array
(
[0] => yes
[1] => 45
[2] => Enter Amount
[3] => Dropdown
[4] => W3sidGl0bGUiOiJMYWJlbCIsImFtb3VudCI6IjQ1In0seyJ0aXRsZSI6IkxhYmVsIiwiYW1vdW50IjoiNDU1In1d
[5] => W3sibmFtZSI6IkxhYmVsIiwicmVjdXJyaW5nX2Ftb3VudCI6Ijc4In0seyJuYW1lIjoidSIsInJlY3VycmluZ19hbW91bnQiOiI3ODgifV0=
[6] => WyJNb250aGx5IiwiUXVhcnRlcmx5IiwiSGFsZi1ZZWFybHkiXQ==
[7] =>
[8] =>
[9] =>
[10] =>
[11] => yes
[12] => yes
)
Run Yourself:
http://sandbox.onlinephpfunctions.com/code/86a50fd58455d64c4de074b888d5d2ea5ee1dd13
try this one
$newArray=array_map(function($v){
return current($v);
},$oldArray);

Filter part of multidimensional array

I would like to filter part of a multidimensional array. I have used the array_filter function. When I print the filtered data, it shows correctly, but I can't seem to save the data back to the array.
Here is the multidimensional array (called $posted_product_details) beforehand, containing the internal array ([data]) which I would like filter:
Array
(
[column_1] => Array
(
[name] => Colour
[data] => Array
(
[0] => Blue
[1] => Green
[2] => Red
[3] => Yellow
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
[column_2] => Array
(
[name] => Pack QTY
[data] => Array
(
[0] => 3
[1] => 3
[2] => 3
[3] => 3
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
[column_3] => Array
(
[name] => Product Code
[data] => Array
(
[0] => 65030
[1] => 65029
[2] => 65028
[3] => 65031
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
[column_4] => Array
(
[name] => Barcode
[data] => Array
(
[0] => 5099570650307
[1] => 5099570650291
[2] => 5099570650284
[3] => 5099570650314
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
[column_5] => Array
(
[name] =>
[data] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
)
Here I attempt to loop through the array and filter the data:
foreach ($posted_product_details as $column => $info) {
$name = $info['name'];
$data = $info['data'];
$info['data'] = array_filter($data);
}
However, upon printing the array afterward, the array has not changed.
Pass the value by reference to modify the original array:
foreach ($posted_product_details as $column => & $info) {
$name = $info['name'];
$data = $info['data'];
$info['data'] = array_filter($data);
}
This will correctly filter the data part of your array. However, if you need to filter out deeper elements, you'll have to use a recursive function, such as this one.
Demo!
The foreach construct makes a copy of each piece of the array as you iterate over it. You have to explicitly call the original array to edit it:
$posted_product_details[$column]['data'] = array_filter($data);

How to sum values via the same key and group by other key

I'd like to sum products by the same ref number, but I have 3rd parameter like dimension e.g. (..., 24, 26mm,...) and I can't sum this values only when they have the same dimension. I tried this: Group a multidimensional array by a particular value? but how to sum values?
My array looks like this:
Array
(
[0] => Array
(
[0] => 2
[1] => 790180X
[2] => 26mm
)
[1] => Array
(
[0] => 4
[1] => 762182Z
)
[2] => Array
(
[0] => 2
[1] => 072182X
)
[3] => Array
(
[0] => 4
[1] => 660122Y
)
[4] => Array
(
[0] => 2
[1] => 790180X
[2] => 24mm
)
[5] => Array
(
[0] => 1
[1] => 225160Y
)
[6] => Array
(
[0] => 1
[1] => 244160Y
)
[7] => Array
(
[0] => 1
[1] => 225160Y
)
[8] => Array
(
[0] => 8
[1] => 954120Y
)
[9] => Array
(
[0] => 3
[1] => 072182X
)
)
I'd like to something like this:
Array
(
[0] => Array
(
[0] => 2
[1] => 790180X
[2] => 26mm
)
[1] => Array
(
[0] => 2
[1] => 790180X
[2] => 24mm
)
[2] => Array
(
[0] => 4
[1] => 762182Z
)
[3] => Array
(
[0] => 5
[1] => 072182X
)
[4] => Array
(
[0] => 4
[1] => 660122Y
)
[5] => Array
(
[0] => 2
[1] => 225160Y
)
[6] => Array
(
[0] => 1
[1] => 244160Y
)
[7] => Array
(
[0] => 8
[1] => 954120Y
)
)
The array in short version:
Array => TO => Array
( (
[0] => 2:790180X:26mm [0] => 2:790180X:26mm
[1] => 4:762182Z [1] => 2:790180X:24mm
[2] => 2:072182X [2] => 4:762182Z
[3] => 4:660122Y [3] => 5:072182X
[4] => 2:790180X:24mm [4] => 4:660122Y
[5] => 1:225160Y [5] => 2:225160Y
[6] => 1:244160Y [6] => 1:244160Y
[7] => 1:225160Y [7] => 8:954120Y
[8] => 8:954120Y )
[9] => 3:072182X
)
Just sum into a new array, using the various "grouping" fields as keys in the new array:
$sums = array();
foreach ($yourarray as $element) {
$sums[$element['dimension1']][$element['dimension2']]++;
}
For every dimension you need to 'group' by, you add a key to the $sums array.

clear php empty array

i have the following array and want to get rid/remove the empty array and rearrange it in an order.can anyone help me please.
Array
(
[ufile] => Array
(
[name] => Array
(
[0] => chicken soup.jpg
[1] =>
[2] => hot n sour sup.jpg
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[type] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[tmp_name] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[error] => Array
(
[0] => 1
[1] => 4
[2] => 1
[3] => 4
[4] => 4
[5] => 4
[6] => 4
[7] => 4
[8] => 4
)
[size] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
)
)
)
I believe this will cleanup your array: remove empty elements (those that evaluate to empty, eg "" and 0 equally), remove duplicate elements, and sort it.
$cleaned = array_map('array_filter', $array_to_be_cleaned);
$cleaned = array_map('array_unique', $cleaned);
$cleaned = array_map('sort', $cleaned);
To filter out the empty elements of the array, check out array_filter.
To sort the elements, check out sort (or refer to this list of sorting functions to see what meets your needs)
$newarray = array_filter($myarray);
sort($newarray);
This will take the array you pass it (ie. $myarray) and it will strip out any empty values, then it will store the results to $newarray. After that, sort will organize the remaining values from least to greatest.
unset( $var['ufile']['type'] );
unset( $var['ufile']['tmp_name'] );
To sort, use sort(), usort(), or any other you want. Here's a good comparison of the different sort methods.
Try array_filter($array). It will remove all NULL elements and return the cleared array.

Categories