Merge two arrays when string is identical - php

I have two arrays that I want to merge. I created the first one through a for() loop so that it includes the last seven days (only three here to keep it short):
array(7) {
[0]=>
array(2) {
["created_at"]=>
string(10) "2017-08-15"
["errorCount"]=>
string(1) "0"
}
[1]=>
array(2) {
["created_at"]=>
string(10) "2017-08-16"
["errorCount"]=>
string(1) "0"
}
[2]=>
array(2) {
["created_at"]=>
string(10) "2017-08-17"
["errorCount"]=>
string(1) "0"
}
}
The other array includes data from a DB:
array(2) {
[0]=>
array(2) {
["created_at"]=>
string(10) "2017-08-15"
["errorCount"]=>
string(1) "4"
}
[1]=>
array(2) {
["created_at"]=>
string(10) "2017-08-16"
["errorCount"]=>
string(1) "12"
}
}
I want to merge these two together so that errorCount in the first array is overwritten whenever created_at is identical. I tried it with array_merge() directly but it only adds the rows of the second array to the end of the first one.
Any suggestions on how to solve this? Or is there a different way to approach it?

Filter result by following statement:
$finArray = array_values(array_combine(array_map(function ($value) {
return $value['created_at'];
}, $mergedArray), $mergedArray));

Related

Sorting through a list of transactions to find the top 10 most popular

Okay so I have an array, laid out as follows (only the first item is shown):
array(600) {
[0]=>
array(12) {
["id"]=>
string(4) "1163"
["aliasID"]=>
string(1) "1"
["date"]=>
string(10) "2017-06-09"
["type"]=>
string(12) "DD"
["description"]=>
string(18) "GYM MEMBERSHIP"
["plusminus"]=>
string(1) "0"
["amount"]=>
string(2) "15"
["balance"]=>
string(6) "50.00"
["ts"]=>
string(19) "2019-01-27 22:32:29"
["alias"]=>
string(3) "Gym"
["categoryID"]=>
string(1) "1"
["category"]=>
string(10) "Recreation"
}
In this instance there are 600 transactions. I want to run through the list, find the 10 most popular (as in, the most frequent occuring) and display them. How can I achieve this? I'm not great at sorting - but I wrote the following code to add them up:
foreach($transactions as $t) {
if(isset($popular_dataset[$t['description']])) {
$popular_dataset[$t['description']]++;
} else {
$popular_dataset[$t['description']] = 1;
}
}
Which gives me an array that I can view the highest ones, but I'm unsure as to how to proceed from here. Any advice would be great - am I on the right path or is there a simpler way?
I would extract the description into an array and count the values, then sort descending and slice the first ten:
$popular_dataset = array_count_values(array_column($transactions, 'description'));
arsort($popular_dataset);
$top_ten = array_slice($popular_dataset, 0, 10);

PHP select inner index in first iteration of foreach loop in a nested array of objects

I know this may sound weird or repetitive, but I have an special requirement, where I got a nested array of objects, which I am trying to display in a Slick slider. The goal is to display outer array (which also serves as an index of inner array) as Slick slides of dates, and inner arrays of objects as the content of that slide available on that date. here is var_dump of my array:
array(5) {
[0]=>
array(2) {
[0]=>
object(stdClass)#36 (40) {
["id"]=>
string(1) "9"
["title"]=>
string(16) "Chemistray Class"
["found_dates"]=>
string(19) "2016-08-01 00:00:00"
}
[1]=>
object(stdClass)#40 (40) {
["id"]=>
string(1) "6"
["title"]=>
string(10) "Math Class"
["found_dates"]=>
string(19) "2016-08-01 00:00:00"
}
}
[1]=>
array(2) {
[0]=>
object(stdClass)#37 (40) {
["id"]=>
string(1) "9"
["title"]=>
string(16) "Chemistray Class"
["found_dates"]=>
string(19) "2016-08-02 00:00:00"
}
[1]=>
object(stdClass)#41 (40) {
["id"]=>
string(1) "6"
["title"]=>
string(10) "Math Class"
["found_dates"]=>
string(19) "2016-08-02 00:00:00"
}
}
[2]=>
array(2) {
[0]=>
object(stdClass)#38 (40) {
["id"]=>
string(1) "9"
["title"]=>
string(16) "Chemistry Class"
["found_dates"]=>
string(19) "2016-08-03 00:00:00"
}
[1]=>
object(stdClass)#42 (40) {
["id"]=>
string(1) "6"
["title"]=>
string(10) "Math Class"
["found_dates"]=>
string(19) "2016-08-03 00:00:00"
}
}
[3]=>
array(1) {
[0]=>
object(stdClass)#43 (40) {
["id"]=>
string(1) "6"
["title"]=>
string(10) "Math Class"
["found_dates"]=>
string(19) "2016-08-04 00:00:00"
}
}
[4]=>
array(1) {
[0]=>
object(stdClass)#39 (40) {
["id"]=>
string(1) "9"
["title"]=>
string(16) "Chemistry Class"
["found_dates"]=>
string(19) "2016-09-04 00:00:00"
}
}
}
here is my php code:
<?php
ksort($searches);
$searches = array_values($searches);
if(count($searches) > 0){ $count = 0; ?>
<div class="dates-slick-slider">
<?php foreach($searches as $key => $sr){
$date = new DateTime($sr[$key]->found_dates); ?>
<p><?= $date->format('M d Y'); ?></p>
<?php foreach ($sr as $key => $search) { ?>
<div>$search->title</div>
<?php } } } ?>
</div>
My problem is: I can successfully extract and display any data in inner loop, but as you can see, the outer loop doesn't hold the correct offset always, as some of the sub arrays hold more than one objects, thus it fails. It can display only those values (dates), which have single objects in array, but if number of objects exceeds 1, it shows undefined offset error, which off course is correct, as it doesn't know which object's '$found_dates' variable it has to extract. I have tried numerous ways, but can't think any way I can do that.
Is there anybody who can shade some light on this or suggest some way to do this?
All comments and answers are welcome and already thanked!

array has same key twice (php)

im reading in some data and want to sort it into an array.
To be more specific, im reading in an obj model and i want to sort all edges into an array. Each edge element then should exist of an array of all faces which use this edge. So after sorting, each edge should have 2 entries existing of the two faces which use them. But after the actual sorting most edges occur twice with the same key and a single face as an entry. Here in some more details what i mean :
First Face i read in is
array(4) {
[0]=>
string(1) "f"
[1]=>
string(1) "1"
[2]=>
string(1) "2"
[3]=>
string(2) "3
which sorts the edges 1_2 , 2_3 and 1_3 into this array (value is the face which uses this edge, face number 0 )
array(3) {
["1_2"]=>
array(1) {
[0]=>
int(0)
}
["2_3"]=>
array(1) {
[0]=>
int(0)
}
["1_3"]=>
array(1) {
[0]=>
int(0)
}
When the second face is read
array(4) {
[0]=>
string(1) "f"
[1]=>
string(1) "1"
[2]=>
string(1) "3"
[3]=>
string(2) "4"
the resulting edge array looks like this :
array(6) {
["1_2"]=>
array(1) {
[0]=>
int(0)
}
["2_3"]=>
array(1) {
[0]=>
int(0)
}
["1_3"]=>
array(1) {
[0]=>
int(0)
}
["1_3"]=>
array(1) {
[0]=>
int(1)
}
["3_4"]=>
array(1) {
[0]=>
int(1)
}
["1_4"]=>
array(1) {
[0]=>
int(1)
}
}
the edge 1_3 is used by both faces, but how the heck does the key "1_3" appear twice in a single array with different values?! i expected it to look like this
["1_3"]=>
array(2) {
[0]=>
int(0)
[0]=>
int(1)
}
i do not have a clue why or how this happens, because as far as i know keys should be unique. when giving out the element "1_3" i then only recieve the latest entry ( int(1) ).
full sorting code looks like this
if ($line_temp[0]=="f"){
for ($j=0;$j<3;$j++){
$line_temp2 = explode("//",$line_temp[$j+1]);
$data["f"][$face_number][$j] = $line_temp2[0];
}
if ($data["f"][$face_number][0]<$data["f"][$face_number][1]){
$data["e"][$data["f"][$face_number][0]."_".$data["f"][$face_number][1]][]=$face_number;
} else {
$data["e"][$data["f"][$face_number][1]."_".$data["f"][$face_number][0]][]=$face_number;
}
if ($data["f"][$face_number][1]<$data["f"][$face_number][2]){
$data["e"][$data["f"][$face_number][1]."_".$data["f"][$face_number][2]][]=$face_number;
} else {
$data["e"][$data["f"][$face_number][2]."_".$data["f"][$face_number][1]][]=$face_number;
}
if ($data["f"][$face_number][0]<$data["f"][$face_number][2]){
$data["e"][$data["f"][$face_number][0]."_".$data["f"][$face_number][2]][]=$face_number;
} else {
$data["e"][$data["f"][$face_number][2]."_".$data["f"][$face_number][0]][]=$face_number;
}
var_dump($data["e"]);
var_dump($data["e"]["1_3"]);
$face_number++;
}

How to loop through a multidimensional array and add key values and out the final calculation

Working in CI, wanting to loop through a result_array and add values from an amount key value. The array is multidimensional
array(2)
{
[0]=> array(9)
{
["id"]=> string(1) "1"
["resident_id"]=> string(1) "1"
["charge_amt"]=> string(6) "250.00"
["charge_key"]=> string(3) "HOM"
["charge_desc"]=> string(25) "Homeowner Association Fee"
["charge_date"]=> string(19) "2014-03-04 03:08:08"
["active"]=> string(1) "1"
["created_at"]=> string(19) "2014-03-03 14:17:00"
["updated_at"]=> NULL
}
[1]=> array(9)
{
["id"]=> string(1) "2"
["resident_id"]=> string(1) "1"
["charge_amt"]=> string(5) "25.00"
["charge_key"]=> string(3) "LAT"
["charge_desc"]=> string(8) "Late Fee"
["charge_date"]=> string(19) "2014-03-04 04:11:10"
["active"]=> string(1) "1"
["created_at"]=> string(19) "2014-03-03 04:10:09"
["updated_at"]=> NULL
}
}
How do I get it to loop through each array and add up each ["charge_amt"] and then print the final calculation once?
Not sure if I am missing anything but it's just a foreach loop where $result represents the second layer of the array. So it's either
foreach($result as $item) or foreach($array['result'] as $item)
$total = 0;
foreach ($result as $item){
$total = $total + $item['charge_amt'];
}
echo $total;
If you don't need the loop for anything else, this might be faster/shorter (PHP 5 >= 5.5.0):
$charge_amts = array_column($result_array, 'charge_amt');
$total_charge_amt = array_sum($charge_amts);
print $total_charge_amt;
Anyway, since this looks like a result from a database query, why don't you let the database sum up the total amount directly?

PHP Associative Arrays with Variable Key Names

I am trying to create an associative array with the keys being email addresses and the values being passwords. It is reading from an XML database to get the information. Here is my code:
$data = simplexml_load_file("Treasury.xml");
//Add in all passwords
for ($i = 0; $i < count($data->Member); $i++) {
$key = $data->Member[$i]->Email + '';
$USERS[$key] = $data->Member[$i]->Pin;
}
The problem comes in the for loop. It gets a correct count of the members (I had that print out) but the key is always being labeled as the number 0, resulting in only the last pin being stored in an array on length 1. Is there something syntactically that I am doing wrong?
Thanks in advance.
EDIT: I did a var_dump of the first user in the XML document. Here it is (Sorry for how long it is):
object(SimpleXMLElement)#4 (5) { ["Name"]=> string(19) "Mackenzie Daugherty" ["PC"]=> object(SimpleXMLElement)#2 (0) { } ["Email"]=> string(16) "dau53688#obu.edu" ["Pin"]=> string(4) "0000" ["Payments"]=> object(SimpleXMLElement)#3 (1) { ["Payment"]=> array(2) { [0]=> object(SimpleXMLElement)#5 (7) { ["Type"]=> string(4) "Dues" ["Description"]=> string(18) "Dues for Fall 2013" ["DateIssued"]=> string(7) "8/26/13" ["DateEnd"]=> string(6) "9/9/13" ["Owed"]=> string(2) "55" ["Paid"]=> string(2) "55" ["Plan"]=> object(SimpleXMLElement)#7 (5) { ["InPlan"]=> string(1) "0" ["PlanDescription"]=> object(SimpleXMLElement)#8 (0) { } ["Intervals"]=> string(1) "0" ["Completed"]=> string(1) "0" ["PerInterval"]=> string(1) "0" } } [1]=> object(SimpleXMLElement)#6 (7) { ["Type"]=> string(19) "Tiger Tunes Tickets" ["Description"]=> string(18) "Two Saturday Night" ["DateIssued"]=> string(7) "8/26/13" ["DateEnd"]=> string(7) "8/26/13" ["Owed"]=> string(2) "30" ["Paid"]=> string(2) "30" ["Plan"]=> object(SimpleXMLElement)#7 (5) { ["InPlan"]=> string(1) "0" ["PlanDescription"]=> object(SimpleXMLElement)#8 (0) { } ["Intervals"]=> string(1) "0" ["Completed"]=> string(1) "0" ["PerInterval"]=> string(1) "0" } } } } }
As clearly stated in the documentation that I'm sure you've been studying carefully, the PHP concatenation operator is ., not +.
Your code takes two operands, and attempts to perform arithmetic addition on them. Since they are not [meaningful] numbers, you end up with 0.
(I couldn't give a more detailed assessment without knowing the precise values of your operands, which you did not provide.)
Your code should read:
$key = $data->Member[$i]->Email . '';
// ^
// (is the concatenation necessary at all?
// isn't Email already a string?)
Make the same correction elsewhere.

Categories