Combining an array based off value - php

Alright, I have been pulling my hair out on this one! I have tried an SQL call to add the features to a product, but since the product is one row and the features are not it creates a new row for each product. I thought I could add both to an array and join them based off the product ID. Here are the arrays
array
0 =>
array
'id_product' => string '9' (length=1)
'name' => string 'Brady ENV100 MAXX Enhanced Sorbents' (length=35)
'number_sold' => string '6' (length=1)
'link_rewrite' => string 'brady-env100-maxx-enhanced-sorbents' (length=35)
'id_image' => string '27' (length=2)
1 =>
array
'id_product' => string '10' (length=2)
'name' => string 'Brady GP100 MAXX Enhanced Heavy Pad' (length=35)
'number_sold' => string '3' (length=1)
'link_rewrite' => string 'brady-gp100-maxx-enhanced-heavy-pad' (length=35)
'id_image' => string '29' (length=2)
array
0 =>
array
'id_product' => string '9' (length=1)
'name' => string 'Height' (length=6)
'value' => string '5' (length=1)
1 =>
array
'id_product' => string '9' (length=1)
'name' => string 'Width' (length=5)
'value' => string '5' (length=1)
2 =>
array
'id_product' => string '9' (length=1)
'name' => string 'Depth' (length=5)
'value' => string '5' (length=1)
3 =>
array
'id_product' => string '9' (length=1)
'name' => string 'Weight' (length=6)
'value' => string '5' (length=1)
4 =>
array
'id_product' => string '10' (length=2)
'name' => string 'Height' (length=6)
'value' => string '10' (length=2)
I have looked at many tutorials, but can't seem to get anything to work. What I would like it to look like is
array
0 =>
array
'id_product' => string '9' (length=1)
'name' => string 'Brady ENV100 MAXX Enhanced Sorbents' (length=35)
'number_sold' => string '6' (length=1)
'link_rewrite' => string 'brady-env100-maxx-enhanced-sorbents' (length=35)
'id_image' => string '27' (length=2)
name' => string 'Height' (length=6)
'value' => string '5' (length=1)
'name' => string 'Width' (length=5)
'value' => string '5' (length=1)
'name' => string 'Depth' (length=5)
'value' => string '5' (length=1)
and so on.

You can try this
For your product array do this:
$result = array();
foreach ($mainItems as $item){
$result[$item["id_product"]] = $item;
}
Then for each of your attribute arrays
foreach($attributes as $val){
if (array_key_exists($val["id_product"], $result)){
$result[$val["id_product"]][$val["name"]] = $val["value"];
}
}
You should end up with an array that looks like
[9] => array(
[id_product] => 9
[name] => "Brady ENV100 MAXX Enhanced Sorbents"
[number_sold] => "6"
[link_rewrite] => "brady-env100-maxx-enhanced-sorbents"
[id_image] => "27"
[Height] => 5
[Width] => 5
[Depth] => 5
),
[10] => array(
[id_product] => '10'
[name] => 'Brady GP100 MAXX Enhanced Heavy Pad'
[number_sold] => '3'
[link_rewrite] => 'brady-gp100-maxx-enhanced-heavy-pad'
[id_image] => '29'

Related

PHP Array calculate when value is equal

I'm stuck and hoping you can help me out.
I'm getting information from the database and putting them in to two different arrays
next I'm merging them, making the values unique and reset the keys.
$totalMatch = array_merge($homeArray, $awayArray);
$totalMatchClean = array_unique($totalMatch, SORT_REGULAR);
$totalMatchCleanKey = array_values($totalMatchClean);
the outcome is the following.
array (size=6)
0 =>
array (size=6)
'Locatie' => string 'Thuis' (length=5)
'TeamID' => string '1' (length=1)
'Matches' => string '1' (length=1)
'GoalsVoor' => string '2' (length=1)
'GoalsTegen' => string '3' (length=1)
'DoelSaldo' => int -1
1 =>
array (size=6)
'Locatie' => string 'Thuis' (length=5)
'TeamID' => string '3' (length=1)
'Matches' => string '1' (length=1)
'GoalsVoor' => string '1' (length=1)
'GoalsTegen' => string '4' (length=1)
'DoelSaldo' => int -3
2 =>
array (size=6)
'Locatie' => string 'Thuis' (length=5)
'TeamID' => string '4' (length=1)
'Matches' => string '2' (length=1)
'GoalsVoor' => string '2' (length=1)
'GoalsTegen' => string '8' (length=1)
'DoelSaldo' => int -6
3 =>
array (size=6)
'Locatie' => string 'Uit' (length=3)
'TeamID' => string '2' (length=1)
'Matches' => string '1' (length=1)
'GoalsVoor' => string '3' (length=1)
'GoalsTegen' => string '2' (length=1)
'DoelSaldo' => int 1
4 =>
array (size=6)
'Locatie' => string 'Uit' (length=3)
'TeamID' => string '1' (length=1)
'Matches' => string '2' (length=1)
'GoalsVoor' => string '8' (length=1)
'GoalsTegen' => string '2' (length=1)
'DoelSaldo' => int 6
That's almost what I want but not completely.
As you can see in array 0 TeamID is 1 and in array 4 TeamID is 1 as well.
Now if that is the case I want to calculate the following fields.
Matches -> Total
GoalsVoor -> Total
GoalsTegen -> Total
DoelSaldo -> Total
in this case those two wil make a new sub array
TeamID => string '1'
Matches => string '3'
GoalsVoor => string '10'
GoalsTegen => string '5'
DoelSaldo => string '5'
But still keeping the other where there are no duplicates...
But I have absolutely no idea how to get to this.
I hope you guys can help me.
Patrick
Array export
array ( 0 => array ( 'Locatie' => 'Thuis', 'TeamID' => '1', 'Matches' => '1', 'GoalsVoor' => '2', 'GoalsTegen' => '3', 'DoelSaldo' => -1, ), 1 => array ( 'Locatie' => 'Thuis', 'TeamID' => '3', 'Matches' => '1', 'GoalsVoor' => '1', 'GoalsTegen' => '4', 'DoelSaldo' => -3, ), 2 => array ( 'Locatie' => 'Thuis', 'TeamID' => '4', 'Matches' => '2', 'GoalsVoor' => '2', 'GoalsTegen' => '8', 'DoelSaldo' => -6, ), 3 => array ( 'Locatie' => 'Uit', 'TeamID' => '2', 'Matches' => '1', 'GoalsVoor' => '3', 'GoalsTegen' => '2', 'DoelSaldo' => 1, ), 4 => array ( 'Locatie' => 'Uit', 'TeamID' => '1', 'Matches' => '2', 'GoalsVoor' => '8', 'GoalsTegen' => '2', 'DoelSaldo' => 6, ), 5 => array ( 'Locatie' => 'Uit', 'TeamID' => '5', 'Matches' => '1', 'GoalsVoor' => '4', 'GoalsTegen' => '1', 'DoelSaldo' => 3, ), )
I think this will work.
I can't test it because I don't feel like cleaning up your array so that I can use it in a code.
But the code should create a new array.
If the subarray with key (TeamID) is set it will sum itself with new array with same TeamID.
$new =array();
Foreach($totalMatchCleanKey as $subarray){
If(!isset($new[$subarray['TeamID']])){
$new[$subarray['TeamID']] = $subarray;
}Else{
$new[$subarray['TeamID']]['Matches'] += $subarray['Matches'];
$new[$subarray['TeamID']]['GoalsVoor'] += $subarray['GoalsVoor'];
$new[$subarray['TeamID']]['GoalsTegen'] += $subarray['GoalsTegen'];
$new[$subarray['TeamID']]['DoelSaldo'] += $subarray['DoelSaldo'];
}
}
Var_dump($new);
Try it here: https://3v4l.org/plSre
You can use array_reduce function to get fields total based on the teamID:
$totals = array_reduce( $totalMatchCleanKey , function ( $carry, $item ) {
$key = array_search($carry, array_column($array, 'TeamID'))
if ($key === false ) {
$carry[] = $item;
} else {
$carry[$key]['Matches'] += $item['Matches'];
$carry[$key]['GoalsVoor'] += $item['GoalsVoor'];
$carry[$key]['GoalsTegen'] += $item['GoalsTegen'];
$carry[$key]['DoelSaldo'] += $item['DoelSaldo'];
}
return $carry;
}, []);

fill missing dates for specific key and value

I have an array:
array (size=3)
0 =>
array (size=3)
'quantity' => string '20' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '2' (length=1)
1 =>
array (size=3)
'quantity' => string '10' (length=2)
'date' => string '2016-03-01' (length=10)
'eID' => string '2' (length=1)
2 =>
array (size=3)
'quantity' => string '15' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '1' (length=1)
how can i fill missing date for each 'eID'? so the output would be:
array (
0 =>
array (
'quantity' => string '20' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '2' (length=1)
1 =>
array (
'quantity' => null
'date' => string '2016-02-01' (length=10)
'eID' => string '2' (length=1)
2 =>
array (
'quantity' => string '10' (length=2)
'date' => string '2016-03-01' (length=10)
'eID' => string '2' (length=1)
...until 2016-12-01. 12 arrays for each 'eID' key with same value

how to decode a json string and add to array in PHP

I have a few records from database and I fetched all records. The array return as the code below
array (size=10)
0 =>
array (size=5)
'id' => string '1' (length=1)
'type' => string 'group' (length=5)
'members' => null
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
1 =>
array (size=5)
'id' => string '2' (length=1)
'type' => string 'group' (length=5)
'members' => null
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
2 =>
array (size=5)
'id' => string '3' (length=1)
'type' => string 'team' (length=4)
'members' => string '["3","5","6"]' (length=13)
'parents' => string '1' (length=1)
'level' => string '2' (length=1)
3 =>
array (size=5)
'id' => string '4' (length=1)
'type' => string 'team' (length=4)
'members' => string '["1"]' (length=5)
'parents' => string '1' (length=1)
'level' => string '2' (length=1)
4 =>
array (size=5)
'id' => string '5' (length=1)
'type' => string 'group' (length=5)
'members' => null
'parents' => string '1' (length=1)
'level' => string '2' (length=1)
5 =>
array (size=5)
'id' => string '8' (length=1)
'type' => string 'team' (length=4)
'members' => string '["4"]' (length=5)
'parents' => string '5' (length=1)
'level' => string '3' (length=1)
6 =>
array (size=5)
'id' => string '9' (length=1)
'type' => string 'team' (length=4)
'members' => string '["2"]' (length=5)
'parents' => string '5' (length=1)
'level' => string '3' (length=1)
7 =>
array (size=5)
'id' => string '10' (length=2)
'type' => string 'team' (length=4)
'members' => null
'parents' => string '5' (length=1)
'level' => string '3' (length=1)
8 =>
array (size=5)
'id' => string '11' (length=2)
'type' => string 'team' (length=4)
'members' => null
'parents' => string '5' (length=1)
'level' => string '3' (length=1)
9 =>
array (size=5)
'id' => string '12' (length=2)
'type' => string 'group' (length=5)
'members' => null
'parents' => string '1' (length=1)
'level' => string '2' (length=1)
the members field has been encoded as json string. I want to build a array with format as the code below
array('1'=>array(1,2,3,4,5,6),
'2'=>array(),
'3'=>array(3,5,6),
'4'=>array(1),
'5'=>array(2,4),
'8'=>array(4),
'9'=>array(2))
The record's id will be key of array and the members has decoded will become the value. I wrote a function handle the array return from the database but the result not as my intention. This is my code
$results = mysql_query('select id,type,members,parents,level from team');
$array = array();
recursiveDate($results,0,$array);
function recursiveData($sourceArr,$parents = 0, &$newMenu){
if(count($sourceArr)>0){
foreach ($sourceArr as $key => $value){
if($value['parents'] == $parents){
if(isset($newMenu[$value['id']])){
$newMenu[$value['id']] = array();
}
$newMenu[$value['id']] = $value['members']?json_decode($value['members']):array();
if(isset($newMenu[$parents])){
$newMenu[$parents] = array_merge($newMenu[$value['id']],$newMenu[$parents]);
}
$newParents = $value['id'];
unset($sourceArr[$key]);
$this->recursiveData($sourceArr,$newParents, $newMenu);
}
}
}
}
And this is the array after handled
array (size=10)
1 =>
array (size=4)
0 => string '1' (length=1)
1 => string '3' (length=1)
2 => string '5' (length=1)
3 => string '6' (length=1)
3 =>
array (size=3)
0 => string '3' (length=1)
1 => string '5' (length=1)
2 => string '6' (length=1)
4 =>
array (size=1)
0 => string '1' (length=1)
5 =>
array (size=2)
0 => string '2' (length=1)
1 => string '4' (length=1)
8 =>
array (size=1)
0 => string '4' (length=1)
9 =>
array (size=1)
0 => string '2' (length=1)
10 =>
array (size=0)
empty
11 =>
array (size=0)
empty
12 =>
array (size=0)
empty
2 =>
array (size=0)
empty
Please help me build that array
I quickly wrote this so not sure if it's exactly what you're looking for, looks like you over complicated it hugely though. I kept the original result structure incase you wanted that but you can easily overwrite it if you wish.
function modifyData($data = array()) {
foreach($data as &$entry) {
if(!empty($entry['members'])) {
$entry['members'] = json_decode($entry['members'], true);
} else {
$entry['members'] = array();
}
}
return $data;
}
I tested this with
$data = array(
array(
'id' =>'1',
'type' =>'group',
'members' => null,
'parents' =>'0',
'level' =>'1'
),
array(
'id' =>'1',
'type' =>'group',
'members' => '["3","5","6"]',
'parents' =>'0',
'level' =>'1'
),
array(
'id' =>'1',
'type' =>'group',
'members' => '["3"]',
'parents' =>'0',
'level' =>'1'
)
);
And the output is
array (size=3)
0 =>
array (size=5)
'id' => string '1' (length=1)
'type' => string 'group' (length=5)
'members' =>
array (size=0)
empty
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
1 =>
array (size=5)
'id' => string '1' (length=1)
'type' => string 'group' (length=5)
'members' =>
array (size=3)
0 => string '3' (length=1)
1 => string '5' (length=1)
2 => string '6' (length=1)
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
2 =>
array (size=5)
'id' => string '1' (length=1)
'type' => string 'group' (length=5)
'members' =>
array (size=1)
0 => string '3' (length=1)
'parents' => string '0' (length=1)
'level' => string '1' (length=1)

combine products with same category in array

I have an array that contains products and their categoryID, some of products have the same category. the output of the array is below
array (size=5)
'categoryID' =>
array (size=3)
0 => string '5' (length=1)
1 => string '2' (length=1)
2 => string '2' (length=1)
'name' =>
array (size=3)
0 => string 'HP Player' (length=9)
1 => string 'Android App' (length=11)
2 => string 'TV' (length=2)
'price' =>
array (size=3)
0 => string '600' (length=3)
1 => string '111' (length=3)
2 => string '1' (length=1)
'qty' =>
array (size=3)
0 => string '22' (length=2)
1 => string '22' (length=2)
2 => string '222' (length=3)
'exprice' =>
array (size=3)
0 => string '13200' (length=5)
1 => string '2442' (length=4)
2 => string '222' (length=3)
I want to combine the products that have same categoryID, the output should be like below:
array (size=2)
=> 0 array
'categoryID' =>
array
0 => string '5' (length=1)
'name' =>
array
0 => string 'HP Player' (length=9)
'price' =>
array
0 => string '600' (length=3)
'qty' =>
array
0 => string '22' (length=2)
'exprice' =>
array
0 => string '13200' (length=5)
=> 1 array
'categoryID' =>
array
0 => string '2' (length=1)
'name' =>
array
0 => string '2' (length=1)
1 => string '2' (length=1)
'price' =>
array
0 => string '111' (length=3)
1 => string '1' (length=1)
'qty' =>
array
0 => string '22' (length=2)
1 => string '222' (length=3)
'exprice' =>
array
0 => string '2442' (length=4)
1 => string '222' (length=3)
how could I do that? Thanks
$arr = array (
'categoryID' =>
array (
0 => '5',
1 => '2',
2 => '2'
),
'name' =>
array (
0 => 'HP Player',
1 => 'Android App',
2 => 'TV'
),
'price' =>
array (
0 => '600',
1 => '111',
2 => '1'
),
'qty' =>
array (
0 => '22',
1 => '22',
2 => '222'
),
'exprice' =>
array (
0 => '13200',
1 => '2442',
2 => '222'
)
);
$cat_count = count(array_keys($arr['categoryID']));
$new_arr = array();
for($i=0;$i<$cat_count;$i++){
for($j=0;$j<$i;$j++){
if($arr['categoryID'][$i] == $arr['categoryID'][$j]){
$arr_key = array_search($arr['categoryID'][$i], $arr['categoryID']);
$cat_exists = 1;
}
}
if($cat_exists){
$new_arr[$arr_key]['name'][] = $arr['name'][$i];
$new_arr[$arr_key]['price'][] = $arr['price'][$i];
$new_arr[$arr_key]['qty'][] = $arr['qty'][$i];
$new_arr[$arr_key]['exprice'][] = $arr['exprice'][$i];
}else{
$new_arr[$i]['categoryID'][] = $arr['categoryID'][$i];
$new_arr[$i]['name'][] = $arr['name'][$i];
$new_arr[$i]['price'][] = $arr['price'][$i];
$new_arr[$i]['qty'][] = $arr['qty'][$i];
$new_arr[$i]['exprice'][] = $arr['exprice'][$i];
}
}
P.S :: $arr is input array & $new_arr is output(result) array!

Add 2 static keys and valur to each inner array of 2 dimension array

I have a 2 dimension array with the help of Baba and now I am in problem again as I need to add [exam] => testOne and [exam_id] => 1 to all inner arrays
array
0 =>
array
'id' => string '7' (length=1)
'name' => string 'Name 1' (length=6)
'marks' => string '8' (length=1)
'grade' => string '4' (length=1)
1 =>
array
'id' => string '8' (length=1)
'name' => string 'Name 2' (length=6)
'marks' => string '5' (length=1)
'grade' => string '2.5' (length=3)
2 =>
array
'id' => string '9' (length=1)
'name' => string 'another name' (length=12)
'marks' => string '8' (length=1)
'grade' => string '4' (length=1)
I need to add
[exam] => testOne and [exam_id] => 1 to all inner arrays want results as follwing
array
0 =>
array
'exam_id' =>'1'
'exam' => 'testOne'
'id' => string '7' (length=1)
'name' => string 'Name 1' (length=6)
'marks' => string '8' (length=1)
'grade' => string '4' (length=1)
1 =>
array
'exam_id' =>'1'
'exam' => 'testOne'
'id' => string '8' (length=1)
'name' => string 'Name 2' (length=6)
'marks' => string '5' (length=1)
'grade' => string '2.5' (length=3)
2 =>
array
'exam_id' =>'1'
'exam' => 'testOne'
'id' => string '9' (length=1)
'name' => string 'another name' (length=12)
'marks' => string '8' (length=1)
'grade' => string '4' (length=1)
This is such a great place for learners like me.
You can try
foreach ( $final as &$var ) {
$var['exam'] = "testOne";
$var['exam_id'] = "1";
}
var_dump($final);
If you want to retain the keys on top (As Requested)
$addOn = array("exam" => "testOne","exam_id" => 1);
end($addOn);
foreach ( $final as &$var ) {
$var = array_merge($addOn, $var);
}
var_dump($final);

Categories