Save data in an array using foreach loop

0

Again I come to ask for help, this time I have to save the values of a variable inside an array and then show them, this variable is within a foreach cycle, I leave the code:

    $strProyecto = $conex->getIndCol('id', sqlProyectos());//proyectos válidos.
    if ($opcion3 == 'save') { 
        $proyecto_id = filter_input(INPUT_POST, 'proyecto_id', FILTER_VALIDATE_INT);//id del proyecto seleccionado
        if ($proyecto_id > 0) {
            $aProyecto = array("proyecto_id"=>$proyecto_id);//id del proyecto seleccionado convertido en array para que sea parámetro de getTareasPendientes
            $tareasP = Tarea::getTareasPendientes($conex, '', $aProyecto);//datos de tareas pendientes del proyecto seleccionado
            foreach ($tareasP as $ids) {
                $tarea_id = $ids['id'];
                $_Tarea = new Tarea($tarea_id, $conex);
                $_Tarea->cancelarTarea($sys_Usuario, $proyecto_id);//método para cancelar tareas pendientes
            }
        } 
    }

I have the variable $tarea_id that contains the id of the task, because I have to save a record in the history I have to do the task cancellation by task ( $_Tarea->cancelarTarea... ), but to show all id of the tasks that were canceled I want to save them in array to then make a echo json_encode .

One of the options I saw was something like this:

foreach($activities as $k => $v) {
    $a[] = $v['name'];
}

but since I use $tarea_id in $_Tarea = new Tarea($tarea_id, $conex); I can not pass it directly to array within the cycle, so I do not know how to do it.

Thank you in advance (:

    
asked by Paloma Alvarado 14.08.2017 в 22:56
source

1 answer

1

Well, I kept looking for some way to solve my doubt and I found a solution, the code was as follows:

$strProyecto = $conex->getIndCol('id', sqlProyectos());//proyectos válidos.
if ($opcion3 == 'save') { 
    $proyecto_id = filter_input(INPUT_POST, 'proyecto_id', FILTER_VALIDATE_INT);//id del proyecto seleccionado
    if ($proyecto_id > 0) {
        $aProyecto = array("proyecto_id"=>$proyecto_id);//id del proyecto seleccionado convertido en array para que sea parámetro de getTareasPendientes
        $aTarea = array();
        $tareasP = Tarea::getTareasPendientes($conex, '', $aProyecto);//datos de tareas pendientes del proyecto seleccionado
        foreach ($tareasP as $ids) {
            $tarea_id = $ids['id'];
            $_Tarea = new Tarea($tarea_id, $conex);
            $cancelar = $_Tarea->cancelarTarea($sys_Usuario, $proyecto_id);//método para cancelar tareas pendientes
/*Esta parte la agregué debido a otra necesidad:
            if ($cancelar == false) {
                echo "Tarea ID: $tarea_id no cancelada";
                echo "<br>";
                break;
            }
*/
            array_push($aTarea, $tarea_id);
        }
        echo 'Tareas canceladas ID: ',json_encode($aTarea);
    } 
}

I used a array_push so that for each round in the cycle I would keep the value of $tarea_id at the end of the array.

I hope this can help someone else with a similar problem.

    
answered by 15.08.2017 / 20:12
source