Formatting dates in an array - php

I have an array which has changing key depending on the table and it can also have custom fields added. I want to be able to identify if the array contains any values that are dates and then format the date.
I have used two examples here, but they keys could be anything and the dates could be including date and/or time. The language I am using in my app is PHP5.5.
Any ideas?
Array
([0] => Array (
[id] => 1
[filename] => 1051404995566.png
[createdby] => 1
[createddate] => 2014-07-10 13:32:46
[enddate] => 2014-07-10
)
[1] => Array (
[id] => 2
[filename] => 1561404995587.png
[createdby] => 1
[createddate] => 2014-07-10 13:33:07
[enddate] => 2014-08-01
)
)

Related

How to add one array value to existing array?

I'm trying to add one array values with existing array values ,but i'm stuck how to add them
Array-1
[main] => Array
(
[0] => stdClass Object
(
[clientID] => 2
[maintenance_due] => 2016-05-09 00:00:00
[complete_maintenance] => 1
[description] => dfdff
[created_at] => 2016-05-01 00:00:00
[completed_on] => 2016-05-06 00:00:00
[name] => Bottel packing
[type_id] => 1
[machinecode] => HITBTP001
[model_no] => ModelA
[created_date] => 2016-03-27 06:55:05
[updated_date] => 0000-00-00 00:00:00
)
)
Array-2
[dt] => Array
(
[dtm] => 0
[edm] => 1
)
I want like this
[main] => Array
(
[0] => stdClass Object
(
[clientID] => 2
[maintenance_due] => 2016-05-09 00:00:00
[complete_maintenance] => 1
[description] => dfdff
[created_at] => 2016-05-01 00:00:00
[completed_on] => 2016-05-06 00:00:00
[name] => Bottel packing
[type_id] => 1
[machinecode] => HITBTP001
[model_no] => ModelA
[created_date] => 2016-03-27 06:55:05
[updated_date] => 0000-00-00 00:00:00
[dtm] => 0
[edm] => 1
)
)
I'm trying array_push function and also use foreach but isn't working .
Please suggest how can i do this.
The solution using array_merge function and conversion(casting) from array into object and vice versa(only for basic stdClass objects):
// $array1 is your first array with 'main' key
// $array2 is your second array with 'dt' key
$main_arr = (array) current($array1['main']); // converting object into array
$merged = array_merge($main_arr, $array2['dt']); // combining "properties"
$array1['main'] = [(object) $merged]; // saving object with additional properties back to the initial array
print_r($array1); // will give the expected output
[0] => stdClass Object
What you have under index 0 is not an array, but object. Unless it implements ArrayAccess interface, you cannot do this as you'd with regular array.
More on Objects: https://secure.php.net/manual/en/language.types.object.php
Assuming $big_array is the first array you posted and $small_array is the second array you posted then this should work:
$big_array['main'][0] = (object)array_merge((array)$big_array['main'][0], $small_array['dt']);
I dunno what your code looks like so this one might work automatically for you:
$main[0] = (object)array_merge((array)$main[0], $dt);
Per Marcin Orlowski's comment, below is the proper way to do this without the risk of overriding any classes or methods:
$main[0]->dtm = $dt['dtm'];
$main[0]->edm= $dt['edm'];

How to group records from a database in a PHP foreach loop, then combine the values in to a new array?

I have a table in a MySQL database that is used to store games that someone can book on. The games can be held in more than one location but they can also share the same time as another game. Here is an example of the data in the array that is taken from the MySQL database:
[0] => Array
(
[id] => 174
[gamedatetime] => DateTime Object
(
[date] => 2016-02-08 14:00:00.000000
[timezone_type] => 3
[timezone] => Europe/London
)
[available] => 1
[gameID] => 1
[isBooked] =>
)
[1] => Array
(
[id] => 175
[gamedatetime] => DateTime Object
(
[date] => 2016-02-08 14:00:00.000000
[timezone_type] => 3
[timezone] => Europe/London
)
[available] => 1
[gameID] => 1
[isBooked] => 1
)
[2] => Array
(
[id] => 176
[gamedatetime] => DateTime Object
(
[date] => 2016-02-08 15:00:00.000000
[timezone_type] => 3
[timezone] => Europe/London
)
[available] => 1
[gameID] => 1
[isBooked] =>
)
What I need to do is create an array with this data on it, but grouped by the date and time, and the gameID are joined together but comma separated, like this:
[0] => Array
(
[gamedatetime] => 2016-02-08 14:00:00.000000
[id] => 174,175
)
[1] => Array
(
[gamedatetime] => 2016-02-08 15:00:00.000000
[id] => 176
)
How can I achieve this using PHP?
The following will achieve what you are aiming for but the ids will be in a subarray, allowing you to concatenate together as you need them.
$newArray = [];
foreach ($oldArray as $game) {
$newArray[$game['gamedatetime']]['gamedatetime'] = $game['gamedatetime'];
$newArray[$game['gamedatetime']]['ids'][] = $game['id'];
}
or you can change the query to something like:
SELECT gamedatetime, GROUP_CONCAT(id) as `id`
FROM game
WHERE ...
GROUP BY gamedatetime
You would be more efficient by using the mysql GROUP BY word that would immediately return the array you want.
If you just want to do it in php it's simply a matter of concatenate the results as you want them displayed which is not 100% clear to me in your question.
Try group_concat in query.
select gamedatetime, group_concat(`id` separator ',') as `id` from table group by gamedatetime;

While iterating through an array of associative arrays, does PHP's foreach order iterations by key instead of index?

I am having troubling using foreach with an array of associative arrays, where the keys in the associative arrays are numbers.
$rows = $_POST["row"];
// print_r($rows);
foreach ($rows as $r) {
fwrite($f, $r["date"]);
fwrite($f, "#");
fwrite($f, $r["desc-short"]);
fwrite($f, "#");
// etc.
}
The POST variable contains arrays identified by row[index]. If I stick in a print_r() it displays the POST values in the order they appeared in the original form (which is not necessarily numerical order, as rows can be inserted in the middle and the counter represents when they were added, not where), but when I iterate with foreach it ends up printing row[8] (assuming eight rows) last, even though it was inserted after row 2 (for example).
It seems that because my keys are numbers, foreach is treating the keys as if they were the order. How can I avoid this behavior?
Output of example data from print_r($rows):
Array ( [1] => Array ( [date] => 12/12/2013 [desc-short] => Show title [desc-long] => A sample long description [start-time] => 12:30 [duration] => 13 [rating] => TVY ) [2] => Array ( [date] => 12/12/2013 [desc-short] => TEST [desc-long] => TEST [start-time] => 12:45 [duration] => 14 [rating] => TVY ) [8] => Array ( [date] => 12/12/2013 [desc-short] => Calendar of Events [desc-long] => A list of local events displayed every hour on the hour [start-time] => 13:00 [duration] => 15 [rating] => TVY ) [3] => Array ( [date] => 12/12/2013 [desc-short] => Show title [desc-long] => A sample long description [start-time] => 12:45 [duration] => 12 [rating] => TVY ) [4] => Array ( [date] => 12/12/2013 [desc-short] => Calendar of Events [desc-long] => A list of local events displayed every hour on the hour [start-time] => 13:00 [duration] => 15 [rating] => TVY ) [5] => Array ( [date] => 12/12/2013 [desc-short] => Show title [desc-long] => test [start-time] => 13:15 [duration] => 100 [rating] => TVY ) [6] => Array ( [date] => 12/12/2013 [desc-short] => Calendar of Events [desc-long] => A list of local events displayed every hour on the hour [start-time] => 15:00 [duration] => 15 [rating] => TVY ) )
And yes, 7 is missing. I'll have to look into that as well. Rows number 1-6 were loaded from a file, while [8] was added in their midst later with JavaScript.
You can't avoid this if your keys are stuck like this. That's just how PHP works. If you need to maintain the order then you'll need to prefix numbers with letter

PHP: Accessing child arrays when you don't know the parent's keys and you can't just search for the value needed

I have a PHP array containing movie descriptions ($descriptions) and another PHP array containing showtimes and ticket quantities ($stock) for those movies. For each unique movie ID in $descripctions, I need to get all of the showtimes and tickets from $stock.
Is there no built-in PHP function that will search for the unique ID and return the chain of keys needed to access that value? I can't find an elegant solution after a full day of Googling and reviewing every array function in the PHP manual twice.
If there's no more elegant solution, is my best approach a custom recursive function? If so, any guidance on best approach / control structure? Maybe counting array elements and using several levels of while loops to go through each level of the array?
Here's the beginning of the $stock array I'm trying to get the data from -- won't post the entire print_r() because the source array is over 67,000 lines long but this much should indicate the structure I'm working with.
Thank you very much!!!
Array (
[products] => Array (
[venue] => Array (
[0] => Array (
[title] => 3-D Digital Cinema
[length] => 25
[memberonly] => 0
[shows] => Array (
[show] => Array (
[0] => Array (
[title] => Planet You
[location] => 3-D Digital Cinema
[eventnumber] => 521
[date] => Array (
[0] => Array (
[eventdate] => 2012/01/01
[times] => Array (
[time] => Array (
[0] => Array (
[starttime] => 13:00:00
[instock] => 110
)
[1] => Array (
[starttime] => 16:00:00
[instock] => 110
)
)
)
)
[1] => Array (
[eventdate] => 2012/01/02
[times] => Array (
[time] => Array (
[0] => Array (
[starttime] => 13:00:00
[instock] => 110
)
[1] => Array (
[starttime] => 16:00:00
[instock] => 110
)
)
)
)

PHP Looping based on date

ok so i have this array of about 151 elements and there is a date field as one of the elements. The array is a two week range. I want to count how many elements are in the first week and how any are in the second week. Here is my example array.
[0] => Array
(
[0] => 4d50
[date] => 07-10-2010
[telephone] => something
[Sno] => 1
)
[1] => Array
(
[0] => 4g50
[date] => 07-03-2010
[telephone] => something
[Sno] => 1
)
[2] => Array
(
[0] => 4s50
[date] => 06-29-2010
[telephone] => something
[Sno] => 1
function getweek($a){
//altered code., m-d-Y is no a format strtotime likes):
return DateTime::createFromFormat('m-d-Y',$a['date'])->format('W');
}
var_dump(array_count_values(array_map('getweek',$inputarray)));

Categories