How to merge array by value - php

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)
}
}

Related

combine 2 array with same key into 1 array

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
)
)

compare values from multidimensional array and add key to array

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,
),
)

PHP - accesing to data of multi-dimensional array

I am trying to access data in a multi-dimenstional array.
I need to get values of this data (EC000001, EG000017, EN, EF007220) but I am struggling with foreach loops in PHP, especially when need to nested foreach Could you please help me and give me solution to access wanted data?
I need to loop over all $c's and then loop inside to get all needed data.This is how I collected value EC000001 before, but I believe that there is a better solution.
foreach ($c as $classCodes => $value) {
$classCode = key($c[$classCodes]); //classCode -> EC000001
}
Structure of array:
Array
(
**[EC000001]** => Array
(
[0] => **EG000017**
[1] => Array
(
[0] => Array
(
[0] => **EN**
[1] => Busbar terminal
)
[1] => Array
(
[0] => **nl-NL**
[1] => Aansluitklem stroomrail
)
)
[2] => Array
(
[0] => Array
(
[0] => **EF007220**
[1] => EU570448
[2] => Array
(
)
)
[1] => Array
(
[0] => EF007219
[1] => EU570448
[2] => Array
(
)
)
[2] => Array
(
[0] => EF000073
[1] =>
[2] => Array
(
[0] => EV009241
[1] => EV009472
)
)
[3] => Array
(
[0] => EF007092
[1] => EU570448
)
[4] => Array
(
[0] => EF004969
[1] => EU570126
)
)
)
)
I can not test it, but you can try with this inside the loop:
$value[0]; // -> 1
$value[1][0][0]; // -> 2
$value[1][1][1]; // -> 3
$EG000002Array[0][EG000001][0]
$EG000002Array[0][EG000001][1][0]
$EG000002Array[0][EG000001][1][1][1]
there are many ways to get array values from multi dimensional array
for example using foreach():
$flavors = array('Japanese' => array('hot' => 'wasabi',
'salty' => 'soy sauce'),
'Chinese' => array('hot' => 'mustard',
'pepper-salty' => 'prickly ash'));
// $culture is the key and $culture_flavors is the value (an array)
foreach ($flavors as $culture => $culture_flavors) {
// $flavor is the key and $example is the value
foreach ($culture_flavors as $flavor => $example) {
print "A $culture $flavor flavor is $example.\n";
}
}
or using for( ) :
$specials = array( array('Chestnut Bun', 'Walnut Bun', 'Peanut Bun'),
array('Chestnut Salad','Walnut Salad', 'Peanut Salad') );
// $num_specials is 2: the number of elements in the first dimension of $specials
for ($i = 0, $num_specials = count($specials); $i < $num_specials; $i++) {
// $num_sub is 3: the number of elements in each sub-array
for ($m = 0, $num_sub = count($specials[$i]); $m < $num_sub; $m++) {
print "Element [$i][$m] is " . $specials[$i][$m] . "\n";
}
}
the output should be like :
Element [0][0] is Chestnut Bun
Element [0][1] is Walnut Bun
Element [0][2] is Peanut Bun
Element [1][0] is Chestnut Salad
Element [1][1] is Walnut Salad
Element [1][2] is Peanut Salad
You can use recursion and regex to check if it's bolded :)
Notice we put& to $classCodes to pass it by reference and not
by value.
Function:
function get_bolded_data($c, &$classCodes = array()){
foreach($c as $k1 => $v1){
if(is_array($v1)){
//If $v1 is an array we call get_bolded_data() again and pass
//$v1 and $classCode
get_bolded_data($v1,$classCodes);
}else if(preg_match("/(\*\*).*(\*\*)/", $v1)){
$classCodes[] = $v1;
}
}
}
Usage:
$classCodes = array();
$c = array(
0 => array(
'**EC000001**' => array(
0 => '**EG000017**',
1 => array(
0 => array(
0 => '**EN**',
1 => 'Busbar terminal'
) ,
1 => array(
0 => '**nl-NL**',
1 => 'Aansluitklem stroomrail'
)
) ,
2 => array(
0 => array(
0 => '**EF007220**',
1 => 'EU570448',
2 => array()
) ,
1 => array(
0 => 'EF007219',
1 => 'EU570448',
2 => array()
) ,
2 => array(
0 => 'EF000073',
1 => '',
2 => array(
0 => 'EV009241',
1 => 'EV009472'
)
) ,
3 => array(
0 => 'EF007092',
1 => 'EU570448'
) ,
4 => array(
0 => 'EF004969',
1 => 'EU570126'
)
)
)
)
);
//Call our function
get_bolded_data($c, $classCodes);
Here is the result from var_dump:
array(4) {
[0]=>
string(12) "**EG000017**"
[1]=>
string(6) "**EN**"
[2]=>
string(9) "**nl-NL**"
[3]=>
string(12) "**EF007220**"
}

How can i check the unique key and value in this array

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 ) );

getting sub array from the given array in php

I have a array variable $data which has the following array structure:
Array
(
[0] => Array
(
[rowdata] => Array
(
[0] => Array
(
[wiecont_03] =>
[tevrc_04] =>
[opmerkTXT] => Overall NPS
[0] => Array-----------------------
( |
[0] => rapport |
[1] => npsorg |
[3] => npsdetbe | i want this
part of the array
)-------------------------------
)
[1] => Array
(
[DOELGROEP] => 2
[KANTOOR] => 2
[OBLIGOCATEGORIE] => 3
[NAAM] => dfdf
[WEIGHT] => 0.95
[opmerkTXT] => Overall NPS
[0] => Array
(
[1] => npsorg
[3] => npsdetbe
)
)
)
[reason] => column values are not correct
)
)
from the above array structure i want to fetch the sub array if any as depicted by the dotted line.
Desired Output:
[0] => Array
(
[0] => rapport
[1] => npsorg
[3] => npsdetbe
)
and
[0] => Array
(
[1] => npsorg
[3] => npsdetbe
)
What i have tried: i tried array_slice() and array_chunks() but none of them worked in my case.
Thanks in advance
You also can do it(in short) like this:
Demo: https://eval.in/86588
<?php
$arr = array
(
0 => array
(
"rowdata" => array
(
"0" => array
(
"wiecont_03" => "",
"tevrc_04" => "",
"opmerkTXT" => "Overall NPS",
"0" => array
(
"0" => "rapport",
"1" => "npsorg",
"3" => "npsdetbe"
)
),
"1" => array
(
"DOELGROEP" => 2,
"KANTOOR" => 2,
"OBLIGOCATEGORIE" => 3,
"NAAM" => "dfdf",
"WEIGHT" => 0.95,
"opmerkTXT" => "Overall NPS",
"0" => array
(
"1" => "npsorg",
"3" => "npsdetbe",
)
)
)
)
);
$re = array();
//print_r($arr[0]['row_data']);
$i=0;
foreach($arr[$i]['rowdata'] as $a){
if(is_array($a[0])){
array_push($re,$a[0]);
}
$i =$i+1;
}
print_r($re);
TRY THIS:
Demo : https://eval.in/86560
$arr = array
(
"0" => array
(
"rowdata" => array
(
"0" => array
(
"wiecont_03" => "",
"tevrc_04" => "",
"opmerkTXT" => "Overall NPS",
"0" => array
(
"0" => "rapport",
"1" => "npsorg",
"3" => "npsdetbe"
)
),
"1" => array
(
"DOELGROEP" => 2,
"KANTOOR" => 2,
"OBLIGOCATEGORIE" => 3,
"NAAM" => "dfdf",
"WEIGHT" => 0.95,
"opmerkTXT" => "Overall NPS",
"0" => array
(
"1" => "npsorg",
"3" => "npsdetbe",
)
)
)
)
);
$re = array();
foreach($arr as $a){
foreach($a as $b){
foreach($b as $c){
array_push($re,$c[0]);
}
}
}
print_r($re);
OUTPUT
Array
(
[0] => Array
(
[0] => rapport
[1] => npsorg
[3] => npsdetbe
)
[1] => Array
(
[1] => npsorg
[3] => npsdetbe
)
)
Your pattern is same in all case then this function is fine
function getMyArray($array){
$rowdata = $array[0]['rowdata'];
$resultArray = array();
foreach($rowdata as $val){
$resultArray[] = $val[0];
}
return $resultArray;
}
Usage:-
$array = "YOUR ARRAY HERE";
$result = getMyArray($array);
foreach($data[0]['rowdata'] as $value){
print_r($value[0]);
}

Categories