I am working with Symfony framework and I have a Controller whose job is to fetch the RSS feeds.
Once I have the fetched feeds in a set of array, I would like to remove those feeds whose date is greater than the date I provide.
So this is the array of object that I get when I fetch the feeds
Array
(
[0] => AppBundle\Entity\Rss Object
(
[id:AppBundle\Entity\Rss:private] =>
[feedItemTitle:AppBundle\Entity\Rss:private] => Project Manager, Investments Business Management - Prudential - Madison, NJ
[feedItemDescription:AppBundle\Entity\Rss:private] => Globally, PREI has offices in Munich, Frankfurt, London, Paris, Luxembourg, Singapore, Seoul, Tokyo, Sydney, and Mexico City....
From Prudential - 30 Jul 2015 20:00:31 GMT
- View all Madison jobs
[feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Project-Manager-at-Prudential-in-Madison,-NJ-27eedeae3104d1be
[feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
(
[date] => 2015-07-30 20:00:31.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[1] => AppBundle\Entity\Rss Object
(
[id:AppBundle\Entity\Rss:private] =>
[feedItemTitle:AppBundle\Entity\Rss:private] => Client Services Specialist - Prudential - Madison, NJ
[feedItemDescription:AppBundle\Entity\Rss:private] => Globally, PREI has offices in Munich, Frankfurt, London, Paris, Luxembourg, Singapore, Seoul, Tokyo, Sydney, and Mexico City....
From Prudential - 03 Aug 2015 19:59:34 GMT
- View all Madison jobs
[feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Client-Service-Specialist-at-Prudential-in-Madison,-NJ-51ad596876a01466
[feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
(
[date] => 2015-08-03 19:59:34.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[2] => AppBundle\Entity\Rss Object
(
[id:AppBundle\Entity\Rss:private] =>
[feedItemTitle:AppBundle\Entity\Rss:private] => Manager, Client Services - Prudential - Madison, NJ
[feedItemDescription:AppBundle\Entity\Rss:private] => Globally, PREI has offices in Munich, Frankfurt, London, Paris, Luxembourg, Singapore, Seoul, Tokyo, Sydney, and Mexico City....
From Prudential - 03 Aug 2015 19:59:32 GMT
- View all Madison jobs
[feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Manager-at-Prudential-in-Madison,-NJ-c0452d58384711e7
[feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
(
[date] => 2015-08-03 19:59:32.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[3] => AppBundle\Entity\Rss Object
(
[id:AppBundle\Entity\Rss:private] =>
[feedItemTitle:AppBundle\Entity\Rss:private] => Technical Sales Representative, Cell Culture Products - STEMCELL Technologies Inc - United States
[feedItemDescription:AppBundle\Entity\Rss:private] => We create novel, useful, standardized products of unfailing quality and deliver them to more than 70 countries via our many regional offices plus distribution...
From STEMCELL Technologies Inc - 01 Aug 2015 01:16:36 GMT
- View all jobs
[feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Technical-Sales-Representative-at-STEMCELL-Technologies-in-United-States-f4c10a0e10852686
[feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
(
[date] => 2015-08-01 01:16:36.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[4] => AppBundle\Entity\Rss Object
(
[id:AppBundle\Entity\Rss:private] =>
[feedItemTitle:AppBundle\Entity\Rss:private] => Technical Sales Representative, Cell Separation Products - STEMCELL Technologies Inc - United States
[feedItemDescription:AppBundle\Entity\Rss:private] => We create novel, useful, standardized products of unfailing quality and deliver them to more than 70 countries via our many regional offices plus distribution...
From STEMCELL Technologies Inc - 31 Jul 2015 01:13:35 GMT
- View all jobs
[feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Technical-Sales-Representative-at-STEMCELL-Technologies-in-United-States-b8c62120268afb55
[feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
(
[date] => 2015-07-31 01:13:35.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[5] => AppBundle\Entity\Rss Object
(
[id:AppBundle\Entity\Rss:private] =>
[feedItemTitle:AppBundle\Entity\Rss:private] => Accounting Intern - Code Corp - Draper, UT
[feedItemDescription:AppBundle\Entity\Rss:private] => Would love someone that is interested in International Accounting processes because this position will work with our China, Singapore & Europe office....
From Code Corp - 31 Jul 2015 21:13:43 GMT
- View all Draper jobs
[feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Accounting-Intern-at-Code-in-Draper,-UT-0e0aca45e988cc89
[feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
(
[date] => 2015-07-31 21:13:43.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
)
As you will notice date in above array.
I am passing the above array and date (not the date from an array above but the $date that i would like to match the array date with to exclude the feed or not) in a parameter to a function below
public function isValid($feed, $date)
{
foreach ($feed as $item) {
if ($item->getfeedItemPubDate()->date < $date) {
echo $item->getfeedItemPubDate()->date;
echo "<br>";
}
}
}
I am able to get the list of feeds whose date from array are smaller than $date but what I am stuck at is how to remove those feeds from the array ? and then pass back the new set of array back to the controller.
You can remove array item by unset function. To know key of that item change a little foreach loop. As so, for example
public function isValid($feed, $date)
{
foreach ($feed as $key => $item)
if ($item->getfeedItemPubDate()->date < $date)
unset ($feed[$key]);
}
You can also try the array_filter function to which you provide a callback function, in your case something like
$feed = array_filter($feed, function($item) use($date) {
return ($item->getfeedItemPubDate()->date < $date);
});
where $date is a variable you set before
Try:
public function isValid($feed, $date)
{
foreach ($feed as $item) {
if (strtotime($item->getfeedItemPubDate()->date) < strtotime($date)) {
echo $item->getfeedItemPubDate()->date;
echo "<br>";
}
}
}
Related
I have weekly international events saved with day, hour, minute, somewhat like this:
Array
(
[0] => Array
(
[event] => Pear festival
[city] => London
[country] => UK
[local-day] => Saturday
[local-time] => 09:00
)
[1] => Array
(
[event] => Apple festival
[city] => New York
[country] => US
[local-day] => Saturday
[local-time] => 10:00
)
[2] => Array
(
[event] => Kiwi festival
[city] => Wellington
[country] => NZ
[local-day] => Saturday
[local-time] => 11:00
)
)
I'm trying to figure out a way to order these chronologically by UTC. I guess this is easy enough if I add the current local time zone to each event before the data is saved. However, countries have summer time and standard time. For example, New Zealand is currently on NZST, which is UTC+13, but after March they return to DST UTC+12.
Anyhow, as things currently stand (January 2021), these events would be ordered:
Array
(
[0] => Array
(
[event] => Kiwi festival
[city] => Wellington
[country] => NZ
[utc-day] => Friday
[utc-time] => 22:00
)
[1] => Array
(
[event] => Pear festival
[city] => London
[country] => UK
[utc-day] => Saturday
[utc-time] => 09:00
)
[2] => Array
(
[event] => Apple festival
[city] => New York
[country] => US
[utc-day] => Saturday
[utc-time] => 14:00
)
)
But after March, the NZ time would be an hour out, and the same for the other countries when they switch to summer time.
Any ideas how to solve this?
I would suggest to add a column in the events table which will store timestamp of your event in UTC no matter this the region event is happening in.
You can use PHP's default DateTime class or widely accepted libraries like Carbon to get the UTC timestamp from a date. You already have the countries and the time in the table. So when you save the event, generate its UTC timestamp and save. And for already existing events, you can do that in bulk once.
This way you can just order your events directly by the UTC timestamps column. This will also help you a lot in longer run if you want to get all events which will happen on a specific date and time, you can check that directly using the UTC timestamp column.
On a side note, this is also helpful in a way that you would have no date logic in your MySQL query other than the one to order results.
hello i'm trying to build an application that plots a graph for the last 7 days beginning with today, and i'm using Laravel 5 - Carbon\Carbon date package..
so if today is wednesday it would end on tuesday
if today is friday it would end on thurdsay nxt week
$now = Carbon::now();
$ar = [];
for($i=1; $i<7; $i++){
array_push($ar, $now->subDays($i));
}
pr($ar, true);
where pr() is a helper function similar to dd() i wrote that just die-dumps in a simple way, but i get this output, with the first problem being that, keeping in mind that today is - Friday, 30th November, 2018, 2018-11-30
Array(
[0] => Carbon\Carbon Object
(
[date] => 2018-11-09 12:23:53.793501
[timezone_type] => 3
[timezone] => UTC
)
[1] => Carbon\Carbon Object
(
[date] => 2018-11-09 12:23:53.793501
[timezone_type] => 3
[timezone] => UTC
)
[2] => Carbon\Carbon Object
(
[date] => 2018-11-09 12:23:53.793501
[timezone_type] => 3
[timezone] => UTC
)
[3] => Carbon\Carbon Object
(
[date] => 2018-11-09 12:23:53.793501
[timezone_type] => 3
[timezone] => UTC
)
[4] => Carbon\Carbon Object
(
[date] => 2018-11-09 12:23:53.793501
[timezone_type] => 3
[timezone] => UTC
)
[5] => Carbon\Carbon Object
(
[date] => 2018-11-09 12:23:53.793501
[timezone_type] => 3
[timezone] => UTC
)
it should continue as 29th, 28th, 27th, but its going far back as 09th of november, and its not even sequencial, like 09, 08, 07, 06, just 09 of november and i dont know what i'm getting wrong, please i need assistance, thanks.
When you use a function on a carbon object, the object will update itself and return a reference to the object. So, your $ar array contains several references to the same object. Every time you use subDays you are actually updating every object in your array.
If you want to fill your array with different carbon objects, you have to create a new instance every time. For example, like this:
$ar = [];
for($i=1; $i<7; $i++){
array_push($ar, now()->subDays($i));
}
now() is a helper function in Laravel that returns a new instance of Carbon::now().
$day = Carbon::today();
$ar = [];
for($i=1; $i<=7; $i++){
array_push($ar, $day);
$day->subDay(1);
}
Here $day->subDay(1); method is modifying the object value itself subtracting by 1 instead of returning the modified value.
$ar = [];
for($i=1; $i<7; $i++){
$now = Carbon::now();
$ar[] = $now->subDays($i);
}
Carbon will update it's instance everytime you call subdays, so you need to reninitilize.
Following is my $newArr0 which is an array of objects.
Array
(
[0] => stdClass Object
(
[created_at] => Mon Dec 08 03:04:47 +0000 2014
[text] => How a individual man can adopt a village in andhra real guds needed hats off to you MASTER BLASTER #Sachin
[source] => Twitter Web Client
)
[1] => stdClass Object
(
[created_at] => Sun Dec 07 17:23:25 +0000 2014
[text] => #two #cool #peoples ..#Coolfieee ..#me nd #Sachin http://t.co/JU971nWAPo
[source] => Instagram
[2] => stdClass Object
(
[created_at] => Sun Dec 07 15:18:22 +0000 2014
[text] => Snga hit 90 odi international fifty...can he hit 6 more to break sachin s fifty record ..#sachin 96 odi fifty ...
kya lgta h tod dega record
[source] => Twitter Web Client
)
[3] => stdClass Object
[created_at] => Sun Dec 07 14:50:53 +0000 2014
[text] => #jeeturaaj Jeeturaaj want #sachin chi book pahje, please give #Sachin #playingitmyway ......:)
[source] => Twitter Web Client
)
[4] => stdClass Object
[created_at] => Sun Dec 07 14:33:49 +0000 2014
[text] => RT #UthMag: Old... http://t.co/b1HMzE3TZI #BCCI #cricket #featured #global #ICC #india #littlemaster #News #ODI #retire #sachin #sports #te…
[source] => Twitter for Android
)
)
I am trying to drop a key with source name from the array, so I tried this code -
foreach ($newArr0 as $nkey1 => $nval1) {
if($nkey1 == "source") {
unset($newArr0["source"]);
}
}
But it is not removing the key, from $newArr0 Let me know what I am doing wrong here.
First off, its a collection of objects inside the array, so you'd use the -> arrow operator on each object inside your loop. Then, alternatively, you could reference each copy of the object inside the foreach and make your unset.
foreach($newArr0 as &$nval1) {
// for each object inside `$newArr0` is in `$nval1`
unset($nval1->source);
// unset $nval1's source
}
<?php
$newArr = array();
foreach ($newArr0 as $key => $value) {
unset($value->source);
$newArr[$key] = $value;
}
$newArr0 = $newArr;
Demo
Here:
foreach ($array as $key => & $object) {
if ($object->source === 'source') {
unset($array[$key]);
}
}
I'm revisiting this as I'm working on another page that I want to use the same concept.
This page shows the output that I'm currently getting from my MSSQL server.
I have a table of venue information (name, address, etc...) that our events happen on. Separately, I have a table of the actual events that are scheduled (an event may happen multiple times in one day and/or over multiple days). I join those tables with a query (as seen below).
<?php
try {
$dbh = new PDO("sqlsrv:Server=localhost;Database=Sermons", "", "");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT TOP (100) PERCENT dbo.TblSermon.Day, dbo.TblSermon.Date, dbo.TblSermon.Time, dbo.TblSermon.Speaker, dbo.TblSermon.Series, dbo.TblSermon.Sarasota, dbo.TblSermon.NonFlc, dbo.TblJoinSermonLocation.MeetingName, dbo.TblLocation.Location, dbo.TblLocation.Pastors, dbo.TblLocation.Address, dbo.TblLocation.City, dbo.TblLocation.State, dbo.TblLocation.Zip, dbo.TblLocation.Country, dbo.TblLocation.Phone, dbo.TblLocation.Email, dbo.TblLocation.WebAddress
FROM dbo.TblLocation RIGHT OUTER JOIN dbo.TblJoinSermonLocation ON dbo.TblLocation.ID = dbo.TblJoinSermonLocation.Location RIGHT OUTER JOIN dbo.TblSermon ON dbo.TblJoinSermonLocation.Sermon = dbo.TblSermon.ID
WHERE (dbo.TblSermon.Date >= { fn NOW() })
ORDER BY dbo.TblSermon.Date, dbo.TblSermon.Time";
$stmt = $dbh->prepare($sql);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach ($stmt as $row) {
echo "<pre>";
print_r($row);
echo "</pre>";
}
unset($row);
$dbh = null;
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
So, as it loops through the query results, it creates an array for each record.
Array
(
[Day] => Tuesday
[Date] => 2012-10-30 00:00:00.000
[Time] => 07:00 PM
[Speaker] => Keith Moore
[Location] => The Ark Church
[Pastors] => Alan & Joy Clayton
[Address] => 450 Humble Tank Rd.
[City] => Conroe
[State] => TX
[Zip] => 77305.0
[Phone] => (936) 756-1988
[Email] => info#thearkchurch.com
[WebAddress] => http://www.thearkchurch.org
)
Array
(
[Day] => Wednesday
[Date] => 2012-10-31 00:00:00.000
[Time] => 07:00 PM
[Speaker] => Keith Moore
[Location] => The Ark Church
[Pastors] => Alan & Joy Clayton
[Address] => 450 Humble Tank Rd.
[City] => Conroe
[State] => TX
[Zip] => 77305.0
[Phone] => (936) 756-1988
[Email] => info#thearkchurch.com
[WebAddress] => http://www.thearkchurch.org
)
Array
(
[Day] => Tuesday
[Date] => 2012-11-06 00:00:00.000
[Time] => 07:00 PM
[Speaker] => Keith Moore
[Location] => Fellowship Of Faith Christian Center
[Pastors] => Michael & Joan Kalstrup
[Address] => 18999 Hwy. 59
[City] => Oakland
[State] => IA
[Zip] => 51560.0
[Phone] => (712) 482-3455
[Email] => ffcc#frontiernet.net
[WebAddress] => http://www.fellowshipoffaith.cc
)
Array
(
[Day] => Wednesday
[Date] => 2012-11-14 00:00:00.000
[Time] => 07:00 PM
[Speaker] => Keith Moore
[Location] => Faith Family Church
[Pastors] => Michael & Barbara Cameneti
[Address] => 8200 Freedom Ave NW
[City] => Canton
[State] => OH
[Zip] => 44720.0
[Phone] => (330) 492-0925
[Email] =>
[WebAddress] => http://www.myfaithfamily.com
)
What I'm wanting to do is combine those arrays, in some fashion, so that the venue information isn't repeated every time, but each date/time is show.
I tried typing it out in array style, but couldn't figure out the appropriate multi-dimensionality for it. I'm just pasting how I would like it to display, since that is how it will end up.
The Ark Church
Contact:
Alan & Joy Clayton
450 Humble Tank Rd.
Conroe, TX 77305
(936) 756-1988
info#thearkchurch.com
http://www.thearkchurch.org
Meetings:
Tuesday, 2012-10-30 07:00 PM
Wednesday, 2012-10-31 07:00 PM
Fellowship Of Faith Christian Center
Contact:
Michael & Joan Kalstrup
18999 Hwy. 59
Oakland, IA 51560
(712) 482-3455
ffcc#frontiernet.net
http://www.fellowshipoffaith.cc
Meetings:
Tuesday, 2012-11-06 07:00 PM
Faith Family Church
Contact:
Michael & Barbara Cameneti
8200 Freedom Ave NW
Canton, OH 44720
(330) 492-0925
http://www.myfaithfamily.com
Meetings:
Wednesday, 2012-11-14 07:00 PM
It doesn't necessarily have to end up like that, but it should give a good idea of what I'm looking for.
I don't necessarily have to create a new array. I just want to not show the same information over and over and over.
I was thinking that I could just do some form of compare, in foreach() that says something along the lines of "if Location is the same as previous Location", but I haven't figured out how to do it (is there a way to cache a previous variable with doing $location1 = [Location], $location2 = [Location], etc...?
One thing to note...
These examples don't have different speakers, but sometimes there are. I would like to be able to access the speaker, somehow. I assume that I would want it bound to the actual event.
JJ
One technique: if you have a loop like this:
while ($row = $query->fetch(PDO::FETCH_NUM) ) {
list($time,$place,$etc) = $row;
// display formatted data
}
try this instead:
$row = $query->fetch();
do {
list($time,$place,$etc) = $row;
$row = $query->fetch(PDO::FETCH_NUM);
if ($time != $row[0] && $place != $row[1] && $etc != $row[2]) {
// display formatted data
}
} while ($row);
I usually would approach a problem like this by creating a two-dimensinoal array, indexed by date then ID or in your case by ID then date.
Loop through events (the first dimension) and print out the contents of that event - while in there loop through the dates on that event and print those out as a list as in your example.
i use imdb api
$homepage = file_get_contents('http://www.imdbapi.com/?i='.$imdbID);
$arr = json_decode($homepage);
In $arr i have all data about related movie.
Maguire [Year] => 1996 [Rated] => R [Released] => 13 Dec 1996 [Genre] => Comedy, Drama, Romance, Sport [Director] => Cameron Crowe [Writer] => Cameron Crowe [Actors] => Tom Cruise, Cuba Gooding Jr., Renée Zellweger, Kelly Preston [Plot] => When a sports agent has a moral epiphany and is fired for expressing it, he decides to put his new philosophy to the test as an independent with the only athlete who stays with him. [Poster] => http://ia.media-imdb.com/images/M/MV5BMTkxNjc2NjQwOF5BMl5BanBnXkFtZTcwMDE2NDU2MQ##._V1_SX320.jpg [Runtime] => 2 hrs 19 mins [Rating] => 7.2 [Votes] => 97329 [ID] => tt0116695 [Response] => True )
what i want is that reaching specified key's value.
Is there a function like that? For example , i will give the key=> Actors and get all actors?
:/ Like this?
$homepage = file_get_contents('http://www.imdbapi.com/?i='.$imdbID);
$arr = json_decode($homepage, true);
print($arr['Actors']);