Setting data sessions and array_merge_recursive() - php

Wondering if anyone can help with the following. Basically I have a form which allows users to select a start and end date, start time and end time.
The user can add time blocks, so they select the info they want, post it, then the page is returned, this happens until they process the form with the complete button.
The issue I am facing is, I need my array to append each post to my array, I have the following code:
if($this->input->post('submit', TRUE) == 'more')
{
$start_date = $this->input->post('start_date', TRUE);
$start_time = $this->input->post('start_time', TRUE);
$end_time = $this->input->post('end_time', TRUE);
$start_dates['start_date'] = $start_date;
$start_dates['start_time'] = $start_time;
$start_dates['end_time'] = $end_time;
if(isset($this->session->userdata['start_dates']) )
{
$merged = array_merge_recursive($start_dates, $this->session->userdata['start_dates']);
}
else
{
$merged = null;
$this->session->set_userdata('start_dates', $start_dates);
}
Problem is, each time we add a new time block one of the items in the array is overwritten.
The expected output for the array is:
Array
(
[start_date] => Array
(
[0] => 2012-08-31
[1] => 2012-08-29
)
[start_time] => Array
(
[0] => 08:00
[1] => 09:00
)
[end_time] => Array
(
[0] => 12:00
[1] => 17:00
)
)
Array key [1] is always overwritten in this case, if we add only two time blocks this would not be a problem - however the user needs to be able to add more than 2 blocks, total limit is up to user to a set limit we'll specify later.
Could anyone help get the logic sorted out.
Thanks in advance.
Mark

You need to save the merged data into the session too and the array merge should apply to the stored array in session:
if($this->session->userdata['start_dates'])
{
$merged = array_merge_recursive($this->session->userdata['start_dates'],
$start_dates);
}
else
{
$merged = $start_dates;
}
$this->session->set_userdata('start_dates', $merged);

Related

PHP - Multiple Time Difference Calculations

For the life of me I cannot find anywhere on the internet that has any documentation on how to solve my problem. I've seen a massive amount of ways to calculate differences in time but I can not seem to put something together that works for my situation.
I'm currently building a time management/ticket system that is a plugin for WordPress. I am using custom tables and not the WordPress tables. I have a table that contains all the main ticket information and another table that contains all the start/stop times. Once the user marks the ticket as complete it will then take all the start/stop time values and calculate the total time spent and then place that total time value into a "total_time" column in the main ticket table.
I'm querying the database and pulling the start/stop times by using:
$start_times = $wpdb->get_results($start_times_query);
$stop_times = $wpdb->get_results($stop_times_query);
Now I have the information in 2 array's. Is this the way I should be doing this?
The array's give me this (for start times):
Array ( [0] => Array ( [time] => 2016-04-29 12:02:43 ) [1] => Array ( [time] => 2016-04-29 12:04:18 ) [2] => Array ( [time] => 2016-04-29 12:06:07 ) [3] => Array ( [time] => 2016-04-29 12:07:56 ) [4] => Array ( [time] => 2016-04-29 12:10:30 ) [5] => Array ( [time] => 2016-04-29 12:11:59 ) )
(Same format for the end times)
Then I break down the array using:
$startTimes = array_column($startTimes, 'time');
$endTimes = array_column($endTimes, 'time');
Which now gives me this as the array (this one is just the start times, but same format for the end times):
Array ( [0] => 2016-04-29 12:02:43 [1] => 2016-04-29 12:04:18 [2] => 2016-04-29 12:06:07 [3] => 2016-04-29 12:07:56 [4] => 2016-04-29 12:10:30 [5] => 2016-04-29 12:11:59 )
Usually I can do a foreach ($start_time as $time){} to itterate through all the values, but (correct me if I'm wrong) I can't put two array's into the foreach brackets and I can't place another foreach statement in the current one because then it will just return all the times for the second array and only return one value for the first array. I feel like I'm missing something simple, what can I do to accomplish this?
EDIT
Thanks to Scopey I've worked out a while loop, but it's not functioning correctly. Here is what I have:
$startTimes = array_column($startTimes, 'time');
$endTimes = array_column($endTimes, 'time');
while (($startTime = current($startTimes)) !== false && ($endTime = current($endTimes) !== false)) {
$startTimesConv = strtotime($startTimes);
$endTimesConv = strtotime($endTimes);
$totalTime = ($startTimesConv - $endTimesConv)/60/60;
next($startTimes);
next($endTimes);
}
If you want to loop two arrays at the same time, you can use the PHP functions for traversing arrays by manipulating their internal pointer: current, next, reset, end and prev.
You just need to make a while loop:
while (($startTime = current($startTimes)) !== false && ($endTime = current($endTimes) !== false) {
// ... Do things
next($startTimes);
next($endTimes);
}
See the documentation for next and current
To iterate two regular arrays with the same length use for instead of foreach:
$length = count($start_times);
for ($i = 0; $i < $length; $i++) {
$start_time = $start_times[$i]["time"];
$stop_time = $stop_times[$i]["time"];
// do things
}
But in your case, I strongly recommend that calculate the differences in SQL.
I would take a step back and look at the queries themselves. You are trying to do a calculation between two times which are related in your database however, the relationship gets lost by separating the times into two separate arrays. Or at the very least is ambiguously related as an array index.
My pseudocode would look like this:
#For each ticket which is closed
#Get an array (time entries) of arrays (start, stop) (each having it's start and stop time)
#total_time = 0
#For each time entry
#Perform a time calc function to get_time_differnce(start, stop)
#Add the time to Total_time
#Add total_time to ticket record in DB
If you showed some more detail I might be able to expand. (i.e The queries themselves)

Editing a poorly formatted JSON string in PHP

I have a site which I used JSON to save some data.
Here is how it saves data
{"1":{"english":{"grade":"7","time":"79"},"physics":{"grade":"3","time":"48"}}}
Note: I know this is a poor way of doing it but I did it when I was not so vast!
The 1 is the user_id which changes according to the id of the user that takes an exam on the platform. The english, physics are the subjects this user took.
The maximum number of exam a user can take at a time is for so the json string will look like {"1":{"english":{"grade":"7","time":"79"},"physics":{"grade":"3","time":"48"},"maths":{"grade":"7","time":"79"},"chemistry":{"grade":"3","time":"48"}}}
First I think this is the best way to save the result as a JSON string
[{"subject":"english","grade":"7","time":"79"}, {"subject":"physics", "grade":"3","time":"48"}}]
My problem is that I want to use PHP to work on the former on. I have done some few stripping of the string and I'm left with this {"english":{"grade":"7","time":"79"},"physics":{"grade":"3","time":"48"}}
I tried this chunk
$JSONResult = json_decode($aScore->result);
foreach ($JSONResult as $subjectKey => $aSubject)
{
foreach ($aSubject as $theResult)
{
$userResult = '
Subject: **This is what I've not been able to get**
Grade: '.$theResult->grade.'
Time: '.$theResult->time.'
';
}
}
I tried $aSubject->subjectKey to get the associative key value from the first foreach statement but it did not work
Any help?
Added: Please leave comments about the way the JSON string was stored. I'd love to learn.
You don't need the inner loop. Each subject is just a single object, not an array. And the subject key is just $subjectKey, it's not a part of the object.
$JSONResult = json_decode($aScore->result, true); // to get an associative array rather than objects
foreach ($JSONResult as $subjectKey => $aSubject) {
$userResult = "
Subject: $subjectKey
Grade: {$aSubject['grade']}
Time: {$aSubject['time']}
";
}
DEMO
You could use the second argument to json_decode!
It changes your $JSONResult from stdClass to an associative Array!
Which means you can do something like this:
$str = '{"1":{"english":{"grade":"7","time":"79"},"physics":{"grade":"3","time":"48"}}}';
$result = json_decode($str, true); // Put "true" in here!
echo "<pre>".print_r($result, true)."</pre>"; // Just for debugging!
Which would output this:
Array
(
[1] => Array
(
[english] => Array
(
[grade] => 7
[time] => 79
)
[physics] => Array
(
[grade] => 3
[time] => 48
)
)
)
And in order to loop through it:
foreach ($result as $idx => $exams) {
echo $exams['english']['grade']."<br>";
echo $exams['physics']['grade']."<br>";
}
Which would output this:
7
3
Update
Without knowing the containing arrays data (Based on the example above)
$exams will be an Array (which could contain any sort of information):
Array
(
[english] => Array
(
[grade] => 7
[time] => 79
)
[physics] => Array
(
[grade] => 3
[time] => 48
)
)
If you want to loop through $exams:
foreach ($result as $idx => $exams) {
foreach ($exams as $subject => $info) {
echo $info['grade']."<br>";
}
}
This would produce the same output as the above example, without needing to know a subject name!

Google Spreadsheet to PHP script modification

I've found this useful script to convert Google Spreadsheets into array.
https://github.com/VernierSoftwareTechnology/google-spreadsheet-to-php-array/blob/master/google-spreadsheet-to-array.php
The resulting array is in the form:
Array
(
[1] => Array
(
[A] => id
[B] => start_date
[C] => title
...
[2] => Array
(
[A] => 10551920077
[B] => 27/03/2014 19:00:00
[C] => Up and Down The City Road
)
and so on...
But I want the first row values to act as keys for the array, like:
Array
(
[2] => Array
(
[id] => 10551920077
[start_date] => 27/03/2014 19:00:00
[title] => Up and Down The City Road
)
and so on...
I've tried to modify the code, but with no success.
Any help?
Thanks in advance! :)
Rather than modifying someone else's code (which is often very difficult), it's probably easier to change the data after the fact:
// get the column headers
$headers = $data[1];
// walk the array and change the keys
$data = array_map(function($row) use($headers) {
// create an array to hold the new row
$new = array();
// step through the array keys...
foreach(array_keys($row) as $k)
// ...and add the new key to the new row with the old data
$new[$headers[$k]] = $row[$k];
// add the new row to the array
return $new;
}, $data);
// now remove the header row from the data
unset($data[1]);
There's probably a way (using some combination of array_walk, array_map or other array functions) to replace that ugly foreach loop in there.

Sorting multidimensional arrays in PHP?

I'm having some troubles sorting events for my website in wordpress.
I'm using custom fields to create events with a title, a description and a date.
I put every events date in an array:
$datesEvenements = array();
array_push($datesEvenements,get_field('date'));
I then get the timestamp for each date:
foreach($datesEvenements as $value){
$timestamp = strtotime($value);
array_push($tsEvenements,$timestamp);
}
I then proceed to sort my timestamp by replacing the past event by 0 or an association of the event timestamp with the difference between that timestamp and the present one.
$arrEventDelta = array();
for($i=0;$i<count($tsEvenements);$i++){
$tsi = $tsEvenements[$i];
if($ts>$tsi){
array_push($arrEventDelta,0);
}
elseif($ts<=$tsi){
array_push($arrEventDelta,array('TimestampEvenements'=>$tsi,'DeltaEvenements'=>$deltaTs));
}
After that, I would like to order the array '$arrEventDelta' by the 'DeltaEvenements' value.
Unfortunately, I'm a bit confused with all those arrays. I've tried the array_multisort function like so:
$DeltaEvents = array();
foreach($arrEventDelta as $key => $row){
$DeltaEvents[$key] = $row['DeltaEvents'];
}
array_multisort($DeltaEvents, SORT_ASC, $arrEventDelta);
But it doesn't work. Any tips on how I could fix this?
This is what is outputed from this,
Array ( [0] => 0 [1] => Array ( [TimestampEvenements] => 1394582400 [DeltaEvenements] => **31923** ) [2] => Array ( [TimestampEvenements] => 1394668800 [DeltaEvenements] => **118323** ) [3] => 0 [4] => 0 )
I need to order the highlighted numbers in increasing order.

how to get value from array with 2 keys

i have array like
Array
(
[1] => Array
(
[user_info] => Array
(
[id] => 1
[name] => Josh
[email] => u0001#josh.com
[watched_auctions] => 150022 150031
)
[auctions] => Array
(
[150022] => Array
(
[id] => 150022
[title] => Title of auction
[end_date] => 2013-08-28 17:50:00
[price] => 10
)
[150031] => Array
(
[id] => 150031
[title] => Title of auction №
[end_date] => 2013-08-28 16:08:03
[price] => 10
)
)
)
so i need put in <td> info from [auctions] => Array where is id,title,end_date but when i do like $Info['id'] going and put id from [user_info] when i try $Info[auctions]['id'] there is return null how to go and get [auctions] info ?
Try:
foreach( $info['auctions'] as $key=>$each ){
echo ( $each['id'] );
}
Or,
foreach( $info as $key=>$each ){
foreach( $each['auctions'] as $subKey=>$subEach ){
echo ( $subEach['id'] );
}
}
Given the data structure from your question, the correct way would be for example:
$Info[1]['auctions'][150031]['id']
$array =array();
foreach($mainArray as $innerArray){
$array[] = $innerArray['auctions'];
}
foreach($array as $key=>$val){
foreach($val as $k=>$dataVal){
# Here you will get Value of particular key
echo $dataVal[$k]['id'];
}
}
Try this code
Your question is a bit malformed. I don't know if this is due to a lacking understanding of the array structure or just that you had a hard time to explain. But basically an array in PHP never has two keys. I will try to shed some more light on the topic on a basic level and hope it helps you.
Anyway, what you have is an array of arrays. And there is no difference in how you access the contents of you array containing the arrays than accessing values in an array containing integers. The only difference is that what you get if you retrieve a value from your array, is another array. That array can you then in turn access values from just like a normal array to.
You can do all of this in "one" line if you'd like. For example
echo $array[1]["user_info"]["name"]
which would print Josh
But what actually happens is no magic.
You retrieve the element at index 1 from your array. This happens to be an array so you retrieve the element at index *user_info* from that. What you get back is also an array so you retrieve the element at index name.
So this is the same as doing
$arrayElement = $array[1];
$userInfo = $arrayElement["user_info"];
$name = $userInfo["name"];
Although this is "easier" to read and debug, the amount of code it produces sometimes makes people write the more compact version.
Since you get an array back you can also do things like iterating you array with a foreach loop and within that loop iterate each array you get from each index within the first array. This can be a quick way to iterate over multidimensional array and printing or doing some action on each element in the entire structure.

Categories