I have data in the following form
Array
(
[0] => Array
(
[event_id] => 2042632
[event_name] => Georgia Tech Yellow Jackets vs. North Carolina Tar Heels
[event_payment] => 156
[payment_status] => 1
[event_date] => 2014-01-29
)
[1] => Array
(
[event_id] => 2042632
[event_name] => Georgia Tech Yellow Jackets vs. North Carolina Tar Heels
[event_payment] => 89
[payment_status] => 1
[event_date] => 2014-01-29
)
[2] => Array
(
[event_id] => 2042632
[event_name] => Georgia Tech Yellow Jackets vs. North Carolina Tar Heels
[event_payment] => 772
[payment_status] => 1
[event_date] => 2014-01-29
)
[3] => Array
(
[event_id] => 2042633
[event_name] => Georgia Tech Yellow Jackets vs. North Carolina Tar Heels
[event_payment] => 256
[payment_status] => 0
[event_date] => 2013-12-29
)
)
All I want now to show data in the following format after getting from this array:
January
Event-name, payment, status
georgia.. 1234, 0
georgia.. 3456, 1
December
Event-name, payment, status
georgia.. 1234, 0
georgia.. 3456, 1
and so on and so forth.
please guide me how to do that..
Try
$result = array();
foreach($arr as $ar){
$date = DateTime::createFromFormat("Y-m-d", $ar['event_date']);
$month = $date->format('F');
$year = $date->format('Y');
$result[$year][$month][] = $ar;
}
See demo here
$res= array();
foreach($arr as $key => $val) // $arr is your actual array
{
$month = date('F-Y', strtotime($val['event_date']));
$res[$month][] = $val;
}
Now you get will result as
[January-2014] = array(a1,a2...)
[December-2014] = array(a1,a2...)
Here I only suggest how to sort an array by date. I hope you can do the rest of the work yourself.
function cmp($a, $b) {
return (new DateTime($a['event_date']))->getTimestamp() < (new DateTime($b['event_date']))->getTimestamp()? -1 : 1;
}
usort($array, 'cmp');
$curr_month = 0;
foreach ($array as $elem) {
// output elements of array based on month
// ...
}
Related
Actually my date is $Date= '03/02/2015';
I want to increment this day with array of days .My array is
Array ( [0] => 1 [1] => 42 [2] => 70 [3] => 98 [4] => 186 [5] => 279 [6] => 372 [7] => 465 [8] => 558 [9] => 730 [10] => 1460 [11] => 4380 [12] => 1825 ).
I stored this array in a variable called $data.I want to increment my date with each of this days and print all result dates.How can i do it???
Try this with this code you can print dates
<?php
$Date = "2015-02-03";
$dataArray = Array ( 1 ,42 , 70 , 98 , 186 , 279 ,372);
foreach($dataArray as $val){
echo date('Y-m-d', strtotime($Date. " + $val days"))."</br>";
}
?>
Use this code
<?php
$Date= '03/02/2015';
$stamp= strtotime($Date);
$days=array(42,70,98,186,279,372,465,558,730,1460,4380,1825); //change these values
$values=array();
foreach($days as $day){
$newstamp=$stamp+($day*24*60*60);
$values[]=date("m/d/Y",$newstamp);
}
//now add to database
mysql_connect('localhost','user','pass'); //change user pass
mysql_select_db('yourdatabase'); //change here
foreach($values as $value) {
$sql="insert into yourtable values('".$value."',xxx,xxxx..))"; //change here
mysql_query($sql);
}
mysql_close();
?>
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
Im trying to figure out how to sort the array below so that past array items are sent to the end of the array staying in start_date descending order.
Edit. Id probably include a time array key value item in all arrays to sort by start_date.
[216] => Array (
[title] => Production 1
[start_date] => 20th Feb
[end_date] => 23rd Feb 2015
[ticket_link] => http://www.google.co.uk
[writer] => Sarah Ruhl
[thumb_image] => /files/3514/1762/4350/Biz-Bio-Pic.jpg
[past] => 1
)
[218] => Array(
[title] => Production 3
[start_date] => 27th Feb
[end_date] => 2nd Mar 2015
[ticket_link] => www.google.co.uk
[writer] => Sarah Ruhl
[thumb_image] => /files/9414/1762/4351/Dan-Bio-Pic.jpg
[past] => 1
)
[219] => Array (
[title] => Production 4
[start_date] => 3rd Mar
[end_date] => 5th Mar 2015
[ticket_link] => www.google.co.uk
[writer] => Sarah Ruhl
[thumb_image] => /files/4314/1762/4351/Kate-Bio-Pic.jpg
[past] => 0
)
Try this -
function checkdate($a, $b)
{
$a = strtotime($a['start_date']);
$b = strtotime($b['start_date']);
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
function checkpast($a, $b)
{
$a_start = strtotime($a['start_date']);
$b_start = strtotime($b['start_date']);
if ($a_start == b_start ) {
return ($a['past'] > $b['past']) ? -1 : 1;
}
}
$array = //your array
usort($array, "checkdate");
usort($array, "checkpast");
I have this array:
Array
(
[1] => 20130701 4 4 3060 1
[2] => 20130702 270 757 13812810 4
[3] => 20130703 5 123 3894971 2
[4] => 20130704 290 478 5119617 1
[5] => 20130705 88 98 189791 2
[6] => 20130708 9 73 564627 1
[7] => 20130722 6102 11992 41974701 1
[8] => 20130723 6397 11021 40522224 1
[9] => 20130725 4644 9336 49167728 2
[10] => 20130726 4891 10157 33516844 3
[11] => 20130727 123 319 2538226 3
[12] => 20130728 451 801 1078705 2
[13] => 20130729 13609 30407 95551827 5
[14] => 20130730 6354 17550 272794650 158
[15] => 20130731 6270 18456 269468599 174
)
I'm trying to change the output in order to show it in a chart, do i change it into a json:
foreach ($day as $key => $value) {
$value = explode(" ", $value) ;
$day[$key] = $value ;
$charts[] = array(substr($value[0],0,4).'-'.substr($value[0],4,2).'-'.substr($value[0],6,2),$value[4]) ;
}
$charts = json_encode($charts, JSON_NUMERIC_CHECK) ;
But it displays me this :
[["2013-07-01","1\r"],["2013-07-02","4\r"],["2013-07-03","2\r"],["2013-07-04","1\r"],["2013-07-05","2\r"],["2013-07-08","1\r"],["2013-07-22","1\r"],["2013-07-23","1\r"],["2013-07-25","2\r"],["2013-07-26","3\r"],["2013-07-27","3\r"],["2013-07-28","2\r"],["2013-07-29","5\r"],["2013-07-30","158\r"],["2013-07-31","174\r"]]
why \r does show? any way I can prevent this ?
try
$charts[] = array(substr($value[0],0,4).'-'.substr($value[0],4,2).'-'.substr($value[0],6,2),substr($value[4],0, -1)) ;
You have return characters at the end of your array
[3] => 20130703 5 123 3894971 2\r
They are not visible because \n = new line \r returns the pointer to the beginning of the line.
Your best bet is to use trim() on every element:
foreach ($day as $key => $value) {
$value = explode(" ", trim($value));
$day[$key] = trim($value) ;
$charts[] = array(trim(substr($value[0],0,4).'-'.substr($value[0],4,2).'-'.substr($value[0],6,2),$value[4])) ;
}
It's even better on numbers to use intval(), which ensures datatype to be integer and removes blankspaces, returns, ... too.
I've got a php script with following array (date,task,actor,hh:mm).
Array
(
[0] => 2013-01-29|Making movies|Laurel|07:30
[1] => 2013-01-29|Making movies|Hardy|00:30
[2] => 2013-01-29|Learning PHP|Hardy|07:00
[3] => 2013-01-29|Singing autographs|Keaton|07:30
[4] => 2013-01-29|Making movies|Lloyd|07:30
[5] => 2013-01-28|Learning PHP|Laurel|07:30
[6] => 2013-01-28|Making movies|Hardy|07:30
[7] => 2013-01-28|Learning PHP|Keaton|07:30
[8] => 2013-01-28|Making movies|Lloyd|07:30
[9] => 2013-01-27|Learning PHP|Laurel|05:30
[10] => 2013-01-27|Making movies|Laurel|02:30
[11] => 2013-01-27|Learning PHP|Hardy|07:30
[12] => 2013-01-27|Making movies|Keaton|07:30
[13] => 2013-01-27|Making movies|Lloyd|07:30
)
I'd like to create a filter that lists the tasks and sums the time values of each task, for example:
Learning PHP (<-selected option)
2013-01-29 Hardy 07:00
2013-01-28 Laurel 07:30
2013-01-28 Keaton 07:30
2013-01-27 Laurel 05:30
2013-01-27 Hardy 07:30
=======================
TOTAL 35:00
What would be the way to make this happen?
Any suggestions or next steps are more than welcome.
This would be easier to filter if you had a multidimensional array. Something like this would work for the current structure though:
$task = "Learning PHP";
foreach($arr as $k=>$v) {
$pieces = explode("|", $v);
if($pieces[1]==$task) {
$total += (int) str_replace(':','',$pieces[3]);
echo $pieces[0] . " " . $pieces[2] . " " . $pieces[3];
}
}
echo "Total: " . $total;
I'm trying to get the contact information from this site http://www.internic.net/registrars/registrar-967.html using PHP.. I was able to get the e-email ad by using the href links by doing this:
$contactStr = "http://www.internic.net/registrars/registrar-967.html";
$contact_string = file_get_contents("$contactStr");
preg_match_all('/<a href="(.*)">(.*)<\/a>/i', $contact_string, $contactInfo);
$email = str_replace("mailto:", "", $contactInfo[1][6]);
However, I'm having a hard time getting the address and the phone # since there's no html element I can use like < p > maybe.. I just need 1800 SW First Ave., Suite 440 Portland OR 97201 United States and 310-467-2549 from this site.. Please enlighten me on how to do this
using preg_match_all or some other ways possible.. Thanks!
Instead of using regex try DOMDocument as others have said in comment.
Here is an example (bit hacky tho) hope it helps:
function get_register_by_id($id){
$site = file_get_contents('http://www.internic.net/registrars/registrar-'.$id.'.html');
$dom = new DOMDocument();
#$dom->loadHTML($site);
$result = array();
foreach($dom->getElementsByTagName('td') as $td) {
if($td->getAttribute('width')=='420'){
$innerHTML= '';
$children = $td->childNodes;
foreach ($children as $child) {
$innerHTML .= trim($child->ownerDocument->saveXML($child));
}
$fixed = array_map('strip_tags', array_map('trim', explode("<br/>",trim($innerHTML))));
foreach($fixed as $val){
if(empty($val)){continue;}
$result[] = str_replace(array('! '),'',$val);
}
}
}
return $result;
}
print_r(get_register_by_id(965));
/*Array
(
[0] => Domain Central Australia Pty Ltd.
[1] => Level 27
[2] => 101 Collins Street
[3] => Melbourne Victoria 3000
[4] => Australia
[5] => +64 300 4192
[6] => robert.rolls#domaincentral.com.au
)*/
print_r(get_register_by_id(966));
/*
Array
(
[0] => Web Business, LLC
[1] => PO Box 1417
[2] => Golden CO 80402
[3] => United States
[4] => +1.303.524.3469
[5] => support#webbusiness.biz
)*/
print_r(get_register_by_id(967));
/*
Array
(
[0] => #1 Host Australia, Inc.
[1] => 1800 SW First Ave., Suite 440
[2] => Portland OR 97201
[3] => United States
[4] => 310-467-2549
[5] => registry-operations#moniker.com
)*/