tags:

views:

109

answers:

6

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>';
}
A: 
foreach ($events as $event) {
   echo $event['day'];
}

Or to get one specific day:

echo $events[0]['day'];
WishCow
this will not work because its a nested array
streetparade
+1  A: 

Try this:

$eventsByDay = array();
foreach ($events as $key => $event) {
    if (!isset($eventsByDay[$event['day']])) {
        $eventsByDay[$event['day']] = array();
    }
    $eventsByDay[$event['day']][] = $event;
}

$eventsByDay is now an array where the key is the day and the value is an array of the corresponding events.

Gumbo
I updated/added a view code. Can you tell me where the good place to add this?
shin
@shin: As I think that you get your data from a database, put it right after fetching the data or even in the process of fetching the data.
Gumbo
I get an error; Fatal error: Cannot use [] for reading in C:\xampp\htdocs\ci\system\application\controllers\admin\calendar_one.php on line 32
shin
@shin: The syntax `$array[]` is for appending a new item to the array. You cannot use it to read the array.
Gumbo
Tried in model, still get the same error.$query = $this->db->query("SELECT DATE_FORMAT(eventDate,'%d') AS day,eventContent,eventTitle FROM eventcal WHERE eventDate BETWEEN '$current_year/$current_month/01' AND '$current_year/$current_month/$total_days_of_current_month'");foreach ($query->result_array() as $row_event){$events[] = $row_event;$eventsByDay = array();foreach ($events as $key => $event) {if (!isset($eventsByDay[$event['day']]){$eventsByDay[$event['day']] = array();$data =$eventsByDay[$event['day']];}$eventsByDay[$event['day']][] = $event;$data = $eventsByDay[$event['day']][];}}
shin
@shin: You’re using this expression that is not valid `$data = $eventsByDay[$event['day']][];`. Try this: `$eventsByDay = array(); foreach ($query->result_array() as $event) { /* … */ }` and put the body of my `foreach` loop in it.
Gumbo
A: 

try this if you use > php 5

$iterator = new RecursiveArrayIterator($array);

for($i=0;$<count($iterator);$i++)
{
// code here or 
echo $iterator[$i]["day"];

}
streetparade
A: 

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']."\n";
}

or create anew array holding jsut these values with array_map:

function map_func($dat) {
    return $dat['day'];
}
$days = array_map('map_func', $array);
johannes
A: 
$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

Crozin
A: 
<?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));
Itay Moav