Save fix in PHP database

0

I have an arrangement that I sent by AJAX to PHP to insert it into the database, the arrangement arrives to me in the following way:

$datas = 
    Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [name] => fecha
                        [value] => 2016-08-10
                    )

            )

[1] => Array
    (
        [0] => Array
            (
                [name] => identity
                [value] => 11434058
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [name] => company
                [value] => 1
            )

    )

[3] => Array
    (
        [0] => Array
            (
                [name] => cc
                [value] => 2
            )

    )

[4] => Array
    (
        [0] => Array
            (
                [name] => value
                [value] => 6
            )

    )

[5] => Array
    (
        [0] => Array
            (
                [name] => qov
                [value] => 1
            )

    )

[6] => Array
    (
        [0] => Array
            (
                [name] => concept
                [value] => 3
            )

    )

[7] => Array
    (
        [0] => Array
            (
                [name] => land
                [value] => 1
            )

    )

)

I have tried to go through it in order to save it in the database, the values [name] are the names of the fields in the database with their respective values [value]

$datas   = $request->getPost('datas');
foreach ($datas as $indices) {
  $indice[$i] = $indices;
  foreach ($indices as $identities) {
    $identity[$j] = $identities;
    $j++;
  }
  $this->saveAction($indice[$i], $i);
  $i++;
}

//No he podido obtener esos valores en esta función hago la inserción
public function saveAction($val, $i) {
  $fecha = $val[0]['name]['value'];

  $new->fecha = $fecha;

  if($new->save()){
    $this->jsonResponseSuccess(
                array(
                    'id' => $new->id), 201, 'Created'
                );
  }
}

I use phalcon framework, I do not know if I'm doing something wrong, I hope the question is understandable.

    
asked by Fabian Sierra 11.08.2016 в 01:31
source

1 answer

2

You are not touring the arrangement well, it is a 3-dimensional arrangement, so you have a level to go, although you can not use $i if you can go through the entire arrangement directly with foreach , try it next to go through your arrangement:

$aux = array(
                0 => array(0 => 
                           array('name' => 'fecha',
                                 'value' => '2016-08-10'
                                )
                          )
                );

foreach($aux as $key => $value){
    foreach($value as $clave => $valor){
        foreach($valor as $columna => $celda){
            echo "clave ".$columna." valor ".$celda."<br/>";
        }

    }
}

EDITING BASED ON COMMENTS

If you want to get the key of the last fix that value is in $columna , if you want to get the value of the arrangement by accessing the key you could do $valor["$columna"]

    
answered by 11.08.2016 / 02:42
source