Save the data from the array and not your key

Asked

Viewed 100 times

0

Galera blz...,

I am editing a field to save to db, I send to Edit.ctp the following array

$result = array_merge($cones->toArray(), $cda->toArray());
debug($result);

//Upshot:

[
(int) 0 => 'Valor 1',
(int) 1 => 'Valor 2',
(int) 2 => 'Valor 3'
]

//Edit.ctp

<?php
        echo $this->Form->control('name',
        [
            'empty' => 'Selecione um cone',
            'label' => false,
            'type' => 'select',
            'options' => $result
        ]);
?>

But when I run to save in the bank it saves the content and not the value of it. Result of $this->request->data;

[
'name' => '0'
]

I’m new to php, tried some things like array_values, and some unsuccessful searches. Thanks in advance.

2 answers

0

  • I think it was not well explained, in the case of imput its type is a select, the user will select one of those 3 and save the form, but in the database when it saves instead of saving "value 1" it saves the Dice 0

0

You need the array keys to be equal to the values

$result = array_combine($cda->toArray(), $cda->toArray());


//debug
Array
(
    [valor 1] => valor 1
    [valor 2] => valor 2
    [valor 3] => valor 3
)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.