Add index to associative array in php

3

I am creating an associative array through php after doing a data validation, this code is to make the "fullcalendar" library work. However, at the end of that validation I need to do a second validation to add another index, but for some reason this index is left out. What am I doing wrong ?, I can not find the inconvenience.

My code:

foreach ($this->eventos as $key => $value) {
    if($value['allDay']==1){
        $evts[]= array('id' => base64_encode(tools::my_encrypt($value['id'])),
            'title' => $value['titulo'],
            'start' => date('Y-m-d', strtotime($value['inicio_fecha'])),
            'end' => date('Y-m-d', strtotime($value['fin_fecha'])),
            'allDay' => true,
            'color' => self::getColor($value['tipo_evento']),

        );
    }
    if($this->tipo == $value['tipo_usuario']){
        array_push($evts, array ('tipo' =>'check-circle-o') );
    }
}

I am basing myself on these two examples: link

Upload data with Ajax to fullcalendar

In summary: The code creates the json so that the fullcalendar plugin reads it correctly. When an event in the calendar corresponds to the user who is viewing it, it adds an icon. (Or at least that's the idea.)

The problem: It is adding me as if it were another index of this menera (see the end of the code):

{"id": "WVRoV2EzaEhkVlF6ZVUwOWZIeDhoVWZPaEtzRGUvaz0=", "title": "neftali 2", "start": "2018-11-23", "end": "1969-12-31", "allDay": true, "color": "# ff6258"}, {"type": "check-circle-o"} ,

I need you to believe in this way:

{"id": "WVRoV2EzaEhkVlF6ZVUwOWZIeDhoVWZPaEtzRGUvaz0=", "title": "neftali 2", "start": "2018-11-23", "end": "1969-12-31", "allDay": true, "color": "# ff6258", "type": "check-circle-o" ,

As it is (separated by keys):

As it should be (separated by "," inside the same key):

I hope you can share a clue with me, I'm really tied up.

Thanks, regards.

    
asked by Neftali Acosta 19.11.2018 в 14:51
source

1 answer

1

If you want to add a new key within the first if , you can add it as you do with the other :

if($value['allDay']==1){
    $evts[]= array('id' => base64_encode(tools::my_encrypt($value['id'])),
        'title' => $value['titulo'],
        'start' => date('Y-m-d', strtotime($value['inicio_fecha'])),
        'end' => date('Y-m-d', strtotime($value['fin_fecha'])),
        'allDay' => true,
        'color' => self::getColor($value['tipo_evento']),
        'tipo' => 'check-circle-o'

    );
}

On the other hand, if you want to add it only when the second if is fulfilled, then you can implement a counter to know in which position of the array to add the new key:

$i=0;
foreach ($this->eventos as $key => $value) {
    if($value['allDay']==1){
        $evts[]= array('id' => base64_encode(tools::my_encrypt($value['id'])),
            'title' => $value['titulo'],
            'start' => date('Y-m-d', strtotime($value['inicio_fecha'])),
            'end' => date('Y-m-d', strtotime($value['fin_fecha'])),
            'allDay' => true,
            'color' => self::getColor($value['tipo_evento']),

        );
    }
    if($this->tipo == $value['tipo_usuario']){
        $evts[$i]['tipo']='check-circle-o';
    }
    $i++;
}

That's how it should work.

    
answered by 19.11.2018 / 16:04
source