This is a follow up question from here. But question is different.
I have the following array outputs.
Array $events
Array
(
[0] => Array
(
[day] => 17
[eventContent] => event 1 of 17th
[eventTitle] => 17th event 1
)
[1] => Array
(
[day] => 19
[eventContent] => event 1 of 19th
[eventTitle] => 19th event 1
)
[2] => Array
(
[day] => 05
[eventContent] => event 1 of 5th
[eventTitle] => 5th event 1
)
[3] => Array
(
[day] => 17
[eventContent] => event 2 of 17th
[eventTitle] => 17th event 2
)
[4] => Array
(
[day] => 19
[eventContent] => event 2 of 19th
[eventTitle] => 19th event 2
)
[5] => Array
(
[day] => 19
[eventContent] => event 3 of 19th
[eventTitle] => 19th event 3
)
)
And I want to pull outs day.
If I use this, it will pick up 1,2,3 etc. But I want 17, 19 etc.
if(array_key_exists($day,$events)){...
Could anyon tell me how to do it please?
Thanks in advance.
--UPDATE--
Original view which I need to modify.
if(array_key_exists($day,$events))
{
//adding the date_has_event class to the <td> and close it
echo ' class="date_has_event"> '.$day;
//adding the eventTitle and eventContent wrapped inside <span> & <li> to <ul>
echo '<div class="events"><ul>';?>
<?php foreach ($events as $event) : ?>
<li>
<span class="title"><?php echo $event['eventTitle']; ?></span>
<span class="desc"><?php echo $event['eventContent']; ?></span>
</li>
<?php endforeach; ?>
<?php
echo '</ul></div>';
}
foreach ($events as $event) {
echo $event['day'];
}
Or to get one specific day:
echo $events[0]['day'];
try this if you use > php 5
$iterator = new RecursiveArrayIterator($array);
for($i=0;$<count($iterator);$i++)
{
// code here or
echo $iterator[$i]["day"];
}
the question is hat you want to do .. some different approaches:
for eaxmple simply printing them using a foreach:
foreach($array as $element) {
echo $element['day']."
";
}
or create anew array holding jsut these values with array_map:
function map_func($dat) {
return $dat['day'];
}
$days = array_map('map_func', $array);
$events = array(...);
$days = array();
foreach($events as $event) {
$day = $event['day'];
$count = isset($days[$day]) ? $days[$day]++ : 1;
$days[$day] = $count;
}
print_r($days);
The above code creates a new array: [day] => Events count
<?php
class Event{
protected $day;
protected $eventContent;
protected $eventTitle;
public function __construct($day,$eventContent,$eventTitle){
$this->day=$day;
$this->eventContent=$eventContent;
$this->eventTitle=$eventTitle;
}
public function getDay(){
return $this->day;
}
}
class EventContainer{
protected $container=array();
public function add(Event $NewEvent){
$this->container[$NewEvent->getDay]=$NewEvent;
return $this;
}
/**
* You can make this one as comples as you want, and even add later a more complex index
*/
public function search($day){
return (isset($this->container[$day]))?$this->container[$day]:null;
}
}
//IN THE BL CODE!
$Cont=new EventContainer;
$Conat->add(new Event(12,'aaa','bbb')
->add(new Event(18,'ccc','xxx')
->add(new Event(22,'fg','xerwrxx');
var_dump($Cont->search(18));