Merge array with same value in same key php - php

I have this code:
$order = wc_get_order( 988613 );
$product_array = array();
$daty = array();
$counter = 1;
foreach ($order->get_items() as $item_key => $item ){
$daty_dostaw = $item->get_meta('Daty dostaw');
$product_id = $item->get_product_id();
$daty_dostaw_explode = explode(',',$daty_dostaw);
$daty[$counter]['product_id'] = $product_id;
foreach($daty_dostaw_explode as $key => $data){
$daty[$counter]['data'][] = $data;
}
$counter++;
}
When I print it it shows me this
Array
(
[1] => Array
(
[product_id] => 988012
[data] => Array
(
[0] => 13-08-2022
[1] => 25-08-2022
[2] => 30-08-2022
)
)
[2] => Array
(
[product_id] => 988087
[data] => Array
(
[0] => 25-08-2022
[1] => 31-08-2022
[2] => 30-09-2022
)
)
)
I want to combine an array that have the same dates to display like this:
Array
( [1] => Array
(
[product_id] => array(988012, 988087)
[data] => Array
(
[0] => 25-08-2022
)
)
[2] => Array
(
[product_id] => 988012
[data] => Array
(
[0] => 13-08-2022
[1] => 30-08-2022
)
)
[3] => Array
(
[product_id] => 988087
[data] => Array
(
[0] => 31-08-2022
[1] => 30-09-2022
)
)
)
I want to merge the array those with the same dates. I don't know how to explain it exactly, above I showed what I would like to achieve. I have already written thousands of lines of code using foreach and have not been able to achieve this :(

Basically you want to convert product_id into an array if there are multiple products with the same date. I would search for such matches, create the merges, remove the elements from the individual occurrence or even the individual occurrence if it gets emptied. An example would be this:
$cachedDates = []; //Here we will store the dates we have gone through so far
foreach ($product_array as $key => $product) {
foreach ($product_array[$key]["data"] => $date) {
if (!in_array($date, $cachedDates)) {
//Creating a cache entry for this date
$cachedDates[$date] = ["keys" => [], "values" => []];
}
//Adding the key and product to the date's cache
$cachedDates[$date]["keys"][]= $key;
$cachedDates[$date]["values"][]= $product["product_id"];
}
}
//Adding the new items to $product_array and cleaning up items that become redundant
foreach ($cachedDates as $date => $entry) {
//We only merge items that have a not-unique date
if (count($entry["values"]) > 0) {
//adding a new item with the correct values and the date
$product_array[]= [
"product_id" => $entry["values"],
"data" => $date
];
//Cleanup
for ($index = 0; $index < count($entry["keys"]); $index++) {
//We remove the date from the data entry after finding its index
$removeIndex = array_search($date, $product_array[$entry["keys"][$index]]["data"]);
unset($product_array[$entry["keys"][$index]]["data"][$removeIndex]);
//If the data array is empty, then the whole product was already merged into some other item(s)
if (!count($product_array[$entry["keys"][$index]]["data"])) {
unset($product_array[$entry["keys"][$index]]);
}
}
}
}
The code above was untested, if you find issues with it and errors, then provide the appropriate PHP input instead of its print, even a json_encode result is good-enough

Related

How can i filter a multidimensional array into another one in PHP?

probably this question is duplicate because I don't know how to describe the problem to find an answer.
I've got an array with 3 parameters: date, events and tags.
A date contains a unix timestamp,
An events is an arrya that contain events' id,
Tags is an array that contain events' tags (comma-separated string if the number is not alone).
This is the array:
Array
(
[date] => 1554328800
[events] => Array
(
[0] => 130
[1] => 131
[2] => 163
)
[tags] => Array
(
[0] => 4
[1] => "1,3,4"
[2] => "1,3"
)
)
The relationship between events and tags is in the key, so the event 130 that has position 0 has tag 4.
As you can see there are some tags repeated (events 130 and 131 or 131 and 163).
How could I get an array with only repeated events like this:
Array
(
[0] => Array
(
[date] => 1554328800
[events] => Array
(
[0] => 130
[1] => 131
)
[tags] => 4
)
[1] => Array
(
[date] => 1554328800
[events] => Array
(
[0] => 131
[1] => 163
)
[tags] => Array
(
[0] => 1
[1] => 3
)
)
)
Here is how I would do it:
List the events per individual tag
This will give several sets of events, which can be used in the next step
List the tags per set of events that occurred in previous step
Produce the result from step 2
Here is the code, also runnable at 3v4l.org:
// Sample input
$data = [
"date" => 1554328800,
"events" => [130, 131, 163],
"tags" => [4, "1,3,4", "1,3"]
];
// 1. List the events per individual tag
foreach($data["tags"] as $i => $val) {
$vals = explode(",", $val);
foreach($vals as $val) {
$eventsByTag[$val][] = $data["events"][$i];
}
}
// 2. List the tags per set of events
foreach($eventsByTag as $tag => $events) {
sort($events, SORT_NUMERIC);
$tagsByEvents[implode(",", $events)][] = $tag;
}
// 3. produce the result
foreach($tagsByEvents as $events => $tags) {
$events = explode(",", $events);
if (count($tags) == 1) $tags = $tags[0];
$result[] = [
"date" => $data["date"],
"events" => $events,
"tags" => $tags
];
}
print_r($result);
$date = array();
$date['date'] = 1554328800;
$date['events'] = array(130, 131, 163);
$date['tags'] = array(4, "1,3,4", "1,3");
$events_by_tag = array(); //gather events grouped by tag
foreach ($date['events'] as $pos => $event) { //parse all events
if (is_string($date['tags'][$pos])) { //if tag is a string <=> if there are more than one tag for the current event
$tags = explode(',', $date['tags'][$pos]); //explode string to loop over the tags
foreach ($tags as $tag) {
if (is_array($events_by_tag[$tag])) { //if tag has already been found and then an array exists to store it
array_push($events_by_tag[$tag], $event);
} else {
$events_by_tag[$tag] = array($event); //else create an array for the next times this tag will be found and store it inside
}
}
} else { //if there's a single tag which is a integer
if (is_array($events_by_tag[$tag])) { //if tag has already been found and then an array exists to store it
array_push($events_by_tag[$date['tags'][$pos]], $event);
} else {
$events_by_tag[$date['tags'][$pos]] = array($event); //else create an array for the next times this tag will be found and store it inside
}
}
}
$result_array = array(); //final array reorganized + date
foreach ($events_by_tag as $tag => $events) {
$tmp_array['date'] = $date['date'];
$tmp_array['events'] = $events;
$tmp_array['tags'] = $tag;
array_push($result_array, $tmp_array);
}
This is NOT exactly what you expected because it will not merge events sharing tags. I think this part needs another post to be developed, but otherwise I can modify my answer to give you the way to go if necessary.
pl check this
$sarr=['date'=>1554328800,
'events'=>
[
130,
131,
163
],
'tags'=>
[
4,
"1,3,4",
"1,3"
]
];
$tagarr=[];
$events=$sarr['events'];
$index=0;
foreach( $sarr['tags'] as $tag)
{
$t=explode(",",$tag);
$cnt=count($t);
for($idx=0;$idx<$cnt;$idx++)
$tagarr[$t[$idx]][]=$events[$index];
$index++;
}
$cnt=count($tagarr);
$myarr=[];
foreach($tagarr as $key=>$value)
{
$myarr[]=['date'=>$sarr['date'],'events'=>$value,'tags'=>$key];
}
ec ho "<pre>";
print_r($myarr);
echo "</pre>";
output is
Array
(
[0] => Array
(
[date] => 1554328800
[events] => Array
(
[0] => 130
[1] => 131
)
[tags] => 4
)
[1] => Array
(
[date] => 1554328800
[events] => Array
(
[0] => 131
[1] => 163
)
[tags] => 1
)
[2] => Array
(
[date] => 1554328800
[events] => Array
(
[0] => 131
[1] => 163
)
[tags] => 3
)
)

How to update an Associative Array

I do not know what is wrong with my php code. I want to have an Associative Array in the format. When I run my code I get this data. [KY] is an array in the [OH] array.
Array ( [Oh] => Array ( [state] => Oh ) [income] => 100 [count] => 1 [Ky] => Array ( [state] => Ky ) )
Array (
[OH] => Array
( [income] =>
[count] =>
)
[KY] => Array
( [income] =>
[count] =>
)
Here is my code
Example data in $array
Array ( [0] => Array (
[SurveyDate] => 1952-06-21
[Income] => 100
[CountyState] => Hamilton|Oh
[count] => 1 ) )
function update_array_value3( $array )
{
foreach ($array as $row)
{
$arrCountyState = explode( "|", $row['CountyState'] );
$key = $arrCountyState[1]; // OH or KY
if( !isset( $_SESSION['sIncome'][$key] ) )
{
$_SESSION['sIncome'][$key]['state'] = $key;
$_SESSION['sIncome'][$key]['income'] = $row['Income'];
$_SESSION['sIncome'][$key]['count'] = 1;
} else
{
$_SESSION['sIncome'][$key]['state'] = $key;
$_SESSION['sIncome'][$key]['income'] += $row['Income'];
$_SESSION['sIncome'][$key]['count'] += 1;
}
}
I can't tell you the root cause of the problem simply based on this snippet of code, but it seems $row either doesn't contain the data you expect it to, or doesn't contain any data.
In your code you reference $row['CountyState'], yet I don't see any array item called CountyState in $array.
Also, I'm not sure whether or not this is intentional, but in your foreach() loop it looks like $_SESSION['sIncome']['income'] and $_SESSION['sIncome']['count'] are being overwritten. Each time the loop encounters a new $key, it will overwrite those values.

Replace empty entry with previous element in associative array

I have following $data array
Array
(
[0] => Array
(
[match_day] => MD27
[price] => 95.33
)
[1] => Array
(
[match_day] => MD28
[price] => 97.82
)
[2] => Array
(
[match_day] => MD29
[price] =>
)
[3] => Array
(
[match_day] => MD30
[price] =>
)
[4] => Array
(
[match_day] => MD31
[price] =>
)
)
Now, my requirements is - replace empty entry of price attribute with previous price attribute.
No need to go for for loop You can try following code:
$result = [];
array_walk($data, function($v,$k) use (&$result){
$result[$k] = $v;
if(!isset($result[$k]['price']) || $result[$k]['price'] == null){
$result[$k]['price'] = $result[$k - 1]['price'];
}
});
print_r($result);
Working Demo is Here
Perform a foreach loop an your array, store previous price value and if the current item has empty price, use previous to replace it. Example:
$previous = 0;
foreach($data AS $key => $row) {
if (empty($row['price']))
$data[$key]['price'] = $previous;
else
$previous = $row['price'];
}

Pushing a sub array into the same array

I am trying to put content of one array into the same array. Here I have an array $mclass with values such as
Array
(
[0] => stdClass Object
(
[room_id] => 1,3,5
[day] => 1
[class_teacher] => TEA-2014-2
[final_exam_date] => 2015-09-21
)
)
You can see I have room_id index with 1,3,5 value. Now, I want to explode the room_id and get duplicate of same array index data with change of room_id and push into the array. and finally delete the current array index such as [0]. Here I want the final result as.
Array
(
[0] => stdClass Object
(
[room_id] => 1
[day] => 1
[class_teacher] => TEA-2014-2
[final_exam_date] => 2015-09-21
)
[1] => stdClass Object
(
[room_id] => 3
[day] => 1
[class_teacher] => TEA-2014-2
[final_exam_date] => 2015-09-21
)
[2] => stdClass Object
(
[room_id] => 5
[day] => 1
[class_teacher] => TEA-2014-2
[final_exam_date] => 2015-09-21
)
)
Here is my code for the same:
if(count($mclass)>0)
{
foreach($mclass as $mclasskey=>$mclass_row)
{
/* Room ID Calculation */
if(isset($mclass[$mclasskey]))
{
$temp_room_id = explode(',',$mclass_row->room_id);
if(count($temp_room_id)>1)
{
foreach($temp_room_id as $trkey=>$tr)
{
if(!in_array($temp_room_id[$trkey], $morning_class_semester))
{
array_push($morning_class_semester,$temp_room_id[$trkey]);
}
}
if(count($morning_class_semester)>0)
{
foreach($morning_class_semester as $mcskey=>$mcs)
{
$index_count = count($new_test);
$test[$index_count] = $mclass[$mclasskey];
$test[$index_count]->room_id = $morning_class_semester[$mcskey];
array_push($new_test,$test[$index_count]);
}
unset($mclass[$mclasskey]);
}
}
}
}
}
The code below does what you're looking for using only arrays. So you'll have to change the array access operators to -> since you're accessing an object. I'd do so, but it would break the example, so I'll leave that up to you.
Code Explained:
Loop through array selecting each subarray (object in your case), explode on the $item('room_id') ... ($item->room_id in your case) ... and create sub arrays, via loop, from that using the data from the original using each key. Remove the original item (which has the combined room_ids) and combine the placeholder and original array.
<?php
//Establish some data to work with
$array = array(
array(
"room_id" => "1,3,5",
"day" => 1,
"class_teacher" => "TEA-2014-2",
"final_exam_date" => "2015-09-21",
));
foreach ($array as $key => $item) {
$placeholder = array();
$ids = explode(',',$item['room_id']);
if (count($ids) > 1) {
foreach ($ids as $id) {
$push = array(
'room_id' => $id,
'day' => $item['day'],
'class_teacher' => $item['class_teacher'],
'final_exam_date' => $item['final_exam_date']
);
array_push($placeholder, $push);
}
$array = array_merge($array, $placeholder);
unset($array[$key]);
}
}
var_dump($array);
?>

PHP Replacing Array Value

Im creating an array of products, each with an ID and score:
$savedProducts = array( 'product' => array("product_id" => $product_id,"product_score" => $score));
I want to be able to update the score by using the product_id as identifier.
I've tried:
foreach($savedProducts as $key => $val)
{
if ($val == $property_id )
{
$savedProducts[$key] = $score;
break;
}
}
Which keeps adding a new array item, rather than updating the original.
I think the issue is that my initial array setup then doesn't match the edited one.
Initial array:
Array
(
[product] => Array
(
[product_id] => 125026
[product_score] => 5
)
)
After trying to update score:
Array
(
[0] => Array
(
[product] => Array
(
[product_id] => 125026
[product_score] => 4
)
)
[1] => Array
(
[0] => Array
(
[product] => Array
(
[product_id] => 125026
[product_score] => 4
)
)
)
)
So it keeps addding elements, rather than updating the existing.
with PHP 5.5 use:
$savedProducts = array_column($savedProducts, NULL, 'product_id');
then you can access your product with:
$savedProducts[$product_id]
Please try this
foreach($savedProducts as $key => $val)
{
if($val['product_id'] == $property_id)
{
$savedProperties[$key]['product_score'] = $score;
break;
}
}

Categories