Array (
[0] => Array ( [2] => 3 )
[1] => Array ( [12] => 32 )
[2] => Array ( [2] => 3 )
[3] => Array ( [24] => 42 )
);
How can i get the output as unique key value. I need to remove the duplicate values.
I need the o/p like this
Array (
[0] => Array ( [2] => 3 )
[1] => Array ( [12] => 32 )
[2] => Array ( [24] => 42 )
);
array_reduce is likely designed for this purpose:
$a = [ [ 2 => 3 ], [ 3 => 4 ], [ 3 => 15 ], [ 2 => 3 ] ];
array_reduce($a, function($memo, $el) {
if(false === array_search($el, $memo)) array_push($memo, $el);
return $memo;
}, array())
#⇒ array(3) {
# [0] =>
# array(1) {
# [2] =>
# int(3)
# }
# [1] =>
# array(1) {
# [3] =>
# int(4)
# }
# [2] =>
# array(1) {
# [3] =>
# int(15)
# }
#}
Try this:
$array = array(
array(2 => 3),
array(12 => 32),
array(2 => 3),
array(24 => 32)
);
echo 'before array_unique_multi:' . "\n";
print_r($array);
echo 'after array_unique_multi:' . "\n";
print_r(array_unique_multi($array));
function array_unique_multi($array)
{
$hashAry = array();
foreach($array as $k=>$v){
$hash = md5(serialize($v));
if(in_array($hash, $hashAry)) unset($array[$k]);
else $hashAry[]=$hash;
}
$array = array_values($array);
return $array;
}
Output:
before array_unique_multi:
Array
(
[0] => Array
(
[2] => 3
)
[1] => Array
(
[12] => 32
)
[2] => Array
(
[2] => 3
)
[3] => Array
(
[24] => 32
)
)
after array_unique_multi:
Array
(
[0] => Array
(
[2] => 3
)
[1] => Array
(
[12] => 32
)
[2] => Array
(
[24] => 32
)
)
You can use the function array_unique.
http://php.net/manual/fr/function.array-unique.php
foreach ($myMultiArray as $index => $doublonArray) {
$myMultiArray[$index] = array_unique($doublonArray);
}
Use this. This will store all the different key / value pairs in the new array.
$test = array(
array(2 => 3),
array(12 => 32),
array(2 => 3),
array(24 => 42)
);
$tmpArray = array();
$newArray = array();
foreach ($test as $elements) {
foreach ($elements as $key => $val) {
if (!array_key_exists($key, $tmpArray) || $tmpArray[$key] !== $val) {
$tmpArray[$key] = $val;
}
}
}
foreach ($tmpArray as $key => $val) {
$newArray[] = array($key => $val);
}
var_dump($newArray);
Output:
array
0 =>
array
2 => int 3
1 =>
array
12 => int 32
2 =>
array
24 => int 42
$check_arr = Array (
[0] => Array ( [13] => 1 )
[1] => Array ( [13] => 3 )
[2] => Array ( [13] => 1 )
[3] => Array ( [10] => 3 )
) ;
print_r(array_unique($check_arr));
This my result.
Array ( [0] => Array ( [13] => 1 ) );
Related
I have 2 Array:
$arr1 = Array (
[2] => Array ( [0] => 41000 [1] => 31079 )
[3] => Array ( [0] => 42963 [1] => 41189 )
)
$arr2 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 )
[3] => Array ( [0] => 42998 [1] => 34567 )
)
I want to combine these two arrays to this array with same key:
$arr3 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 [2] => 41000 [3] => 31079 )
[3] => Array ( [0] => 42998 [1] => 34567 [2] => 42963 [3] => 41189 )
)
I tried almost anything (merge, combine, join) but I can't figure it out. Any suggestions?
This is a nice and easy thing to do.
$arr1 = [2 => [0 => 41000, 1 => 31079], 3 => [0 => 42963, 1 => 41189]];
$arr2 = [2 => [0 => 40213, 1 => 42054], 3 => [0 => 42998, 1 => 34567]];
function addArray(&$bucket, $water)
{
foreach ($water as $key => $drop) {
$bucket[$key] = array_merge($bucket[$key] ?? [], $drop);
}
}
$combined = [];
addArray($combined, $arr1);
addArray($combined, $arr2);
var_export($combined);
See a PHP fiddle here.
The output is:
array (
2 =>
array (
0 => 41000,
1 => 31079,
2 => 40213,
3 => 42054,
),
3 =>
array (
0 => 42963,
1 => 41189,
2 => 42998,
3 => 34567,
),
)
How does it work? I wrote a little function that would add the input arrays to an array that combines all arrays.
$arr1 = array(
2 => array(41000, 31079),
3 => array(42963, 41189)
);
$arr2 = array(
2 => array(40213, 42054),
3 => array(42998, 34567)
);
$arr3 = array();
foreach($arr1 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
foreach($arr2 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
echo '<pre>';
print_r($arr3);
Prints:
Array
(
[2] => Array
(
[0] => 41000
[1] => 31079
[2] => 40213
[3] => 42054
)
[3] => Array
(
[0] => 42963
[1] => 41189
[2] => 42998
[3] => 34567
)
)
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
)
)
I want to compare the value of key[0] of each array.
If they are the same then I would like to add a new key[3] to each array with an id.
This is an array of variable products if the product has the same key[0] then its the same product with different variations.
If the key[0] is different from the previous then add id+1, in this example, I would like to end up with:
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
[3] => 1
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
[3] => 2
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
[3] => 3
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
[3] => 3
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
[3] => 3
)
)
can you guys help me with this?
I tried this:
but does not work
foreach ($new as $current_key => $current_array) {
foreach ($new as $search_key => $search_array) {
$ref1 = $current_array[0];
$ref2 = $search_array[0];
if (($search_key != $current_key) and ($ref1 == $ref2)) {
$current_array[3] = $p_id_product;
}
else{
$current_array[3] = $p_id_product++;
}
}
}
Assuming you have already sorted the array by the initial index, so at least they are grouped:
<?php
$data =
[
[
'foo',
'spam',
'bar',
],
[
'foo',
'eggs',
],
[
'bar',
'ham'
],
];
$output = [];
$counter = 0;
$last = null;
foreach($data as $k => $v) {
if($last !== $v[0])
$counter++;
$v[3] = $counter;
$output[$k] = $v;
$last = $v[0];
}
var_export($output);
Output:
array (
0 =>
array (
0 => 'foo',
1 => 'spam',
2 => 'bar',
3 => 1,
),
1 =>
array (
0 => 'foo',
1 => 'eggs',
3 => 1,
),
2 =>
array (
0 => 'bar',
1 => 'ham',
3 => 2,
),
)
I can generate array from mysql, if there is a way for making it easy. I get rows and currently I am rendering like that, if there is a way to generate on fly
3, 0.4311 |
3, 0.1803 |
4, 0.1149 |
4, 0.0775 |
5, 0.4291 |
5, 0.5100|
Considering this array, how to merge it :
Array
(
[0] => Array
(
[channel_id] => 3
[value] => 0.4311
)
[1] => Array
(
[channel_id] => 3
[value] => 0.1803
)
[2] => Array
(
[channel_id] => 4
[value] => 0.1149
)
[3] => Array
(
[channel_id] => 4
[value] => 0.0775
)
...
)
so it will look like this this:
Array
(
[0] => Array
(
[channel_id] => 3
[value] => 0.4311
[value] => 0.1803
)
[1] => Array
(
[channel_id] => 4
[value] => 0.1149
[value] => 0.0775
)
)
Here is the code that I generate this array:
while($row = $result->fetch_assoc()) {
$array[] = array('channel_id'=>$row['channel_id'],'value'=>$row['value'] );
}
OK this seem impossible, but this is closed I could get:
$channels_byid = array();
foreach($array as $v){
#$channel = $channels_byid[$v['channel_id']];
if ($array){
if(!is_array($channel[0])){
unset($channels_byid[$v['channel_id']]);
$channels_byid[$v['channel_id']] = $channel;
}
$channels_byid[$v['channel_id']][] = $v;
} else {
$channels_byid[$v['channel_id']] = $v;
}
}
print_r($channels_byid);
Which outputs:
[3] => Array
(
[0] => Array
(
[channel_id] => 3
[value] => 0.7513
)
[1] => Array
(
[channel_id] => 3
[value] => 0.7234
)
)
[4] => Array
(
[0] => Array
(
[channel_id] => 4
[value] => 0.9798
)
[1] => Array
(
[channel_id] => 4
[value] => 0.7625
)
)
Not exactly what you were after, but it might be useful. The channel_id becomes the top-level key and value becomes the values of a new array.
$data[] = array('channel_id' => 3, 'value' => 0.4311);
$data[] = array('channel_id' => 3, 'value' => 0.1803);
$data[] = array('channel_id' => 4, 'value' => 0.1149);
$data[] = array('channel_id' => 4, 'value' => 0.0775);
foreach($data as $k => $v) {
$merge[$v['channel_id']][] = $v['value'];
}
And the result of $merge is:
array(2) {
[3]=> array(2) {
[0]=> float(0.4311)
[1]=> float(0.1803)
}
[4]=> array(2) {
[0]=> float(0.1149)
[1]=> float(0.0775)
}
}
I have a multi-dimensional array in PHP as follows:
$array = Array (
[0] => Array ( [bill] => 1 )
[1] => Array ( [bill] => 1 )
[2] => Array ( [bill] => 1 )
[3] => Array ( [bill] => 1 )
[4] => Array ( [jack] => 5 )
[5] => Array ( [jack] => 5 )
[6] => Array ( [jake] => 6 )
[7] => Array ( [jake] => 6 )
[8] => Array ( [jake] => 6 )
[9] => Array ( [jake] => 6 )
[10] => Array ( [sara] => 7 )
[11] => Array ( [sara] => 7 )
[12] => Array ( [sara] => 7 )
[13] => Array ( [sara] => 7 )
[14] => Array ( [william] => 8 )
[15] => Array ( [william] => 8 )
[16] => Array ( [william] => 8 )
[17] => Array ( [william] => 8 )
[19] => Array ( [william] => 8 )
[20] => Array ( [martin] => 9 )
[21] => Array ( [martin] => 9 )
[22] => Array ( [martin] => 9 )
[23] => Array ( [tara] => 10 )
[24] => Array ( [tara] => 10 )
[25] => Array ( [tara] => 10 )
[26] => Array ( [tara] => 10 )
);
I want to convert $array to $result so that it can first sort the array based on the number of similar elements and then returning the first five values. For example, the $array has five similar [william] => 8, four similar [bill] => 1, four similar [jake] => 6, four similar [sara] => 7, four similar [tara] => 10, three similar [martin] => 9 and two similar [jack] => 5 in $array variable. So, the $result variable with the first five most repeated elements should be as follows:
$result = Array (
[0] => Array ( [william] => 8 )
[1] => Array ( [bill] => 1 )
[2] => Array ( [jake] => 6 )
[3] => Array ( [sara] => 7 )
[4] => Array ( [tara] => 10 )
);
Could you please help me how to solve this problem?
Create a new array that combines the keys and values and use array_count_values to count the # of occurrences. Finally sort and retrieve the top 5.
$new = array();
foreach($array as $key => $value) {
$new[] = key($value) . '_' . current($value);
}
$counts = array_count_values($new);
arsort($counts);
$results = array();
foreach(array_splice($counts,0,5) as $key => $count) {
$key = explode('_',$key);
$results[] = array($key[0] => $key[1]);
}
print_r($results);
$result = array();
$occurrence_map = array();
$pair_map = array();
foreach( $array as $record )
{
$key = key($record);
$pair_map[ $key ] = $record;
++$occurrence_map[ $key ];
}
while( 5 > count($result) && !empty($occurrence_map) )
{
$related_key = '';
$max_value = $occurrence_map[ key($occurrence_map) ];
foreach( $occurrence_map as $key => $value )
{
if( $value > $max_value )
{
$max_value = $value;
$related_key = $key;
}
}
$result[] = $pair_map[$related_key];
unset( $occurrence_map[$related_key] );
}
Result:
array(5) {
[0]=>
array(1) {
["william"]=>
int(8)
}
[1]=>
array(1) {
["bill"]=>
int(1)
}
[2]=>
array(1) {
["jake"]=>
int(6)
}
[3]=>
array(1) {
["sara"]=>
int(7)
}
[4]=>
array(1) {
["tara"]=>
int(10)
}
}
Hope this help you.
I have a multidimensional array as follows
Array
(
[0] => Array
(
[1] => val01
[2] => val02
[3] =>
)
[1] => Array
(
[1] => val11
[2] => Array
(
[sub1] => 1
[sub2] =>
[sub3] => Array
(
[primarysub1] =>
[primarysub2] => pmy2
)
)
[3] => val3
)
[2] => Array
(
[1] => val21
[2] =>
[3] => val23
)
)
And I need to filter the empty values and expected output is as follow.
Array
(
[0] => Array
(
[1] => val01
[2] => val02
)
[1] => Array
(
[1] => val11
[2] => Array
(
[sub1] => 1
[sub3] => Array
(
[primarysub2] => pmy2
)
)
[3] => val3
)
[2] => Array
(
[1] => val21
[3] => val23
)
)
Could somebody help me out with a best way using PHP?
You have to use recursive function for this. hope this might help you
<?php
$rs = unsetValues($arr);
function unsetValues($a)
{
foreach($a as $k=>$v)
{
if(is_array($v))
{
$arr2[$k] = unsetValues($v);
} else {
if($v!="")
$arr2[$k] = $v;
}
}
return $arr2;
}
?>
function filter($input, $callback = null)
{
foreach ($input as &$value)
{
if (is_array($value))
{
$value = filter($value, $callback);
}
}
return array_filter($input, $callback);
}
function remove_empty($val)
{
return !empty($val);
}
$test_arr = array(
0 => array(1 => "val01", 2 => "val02", 3 => ""),
1 => array(1 => "val11", 2 => array("sub1" => 1, "sub2" => "", "sub3" => array("primarysub1" => "", "primarysub2" => "pmy2")), 3 => "val3"),
2 => array(1 => "val21", 2 => "", 3 => "val23")
);
echo '<pre>' . print_r($test_arr, true) . '</pre>';
// filter empty
$result = filter($test_arr, remove_empty);
echo '<pre>' . print_r($result, true) . '</pre>';