Save the data from the array, not its key

0

Galera blz ...,

I am editing a field to save in db, I send pro edit.ctp the following array

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

// Result:

[
(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 to the bank it saves the index 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. Thank you in advance.

    
asked by anonymous 25.07.2018 / 18:35

2 answers

0

To access the value of an array in a given position you can use the example below:

$valor = $result[0];

In case I'm trying to get value from the first index in my array.

More information about arrays: link

    
25.07.2018 / 20:03
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
)
    
25.07.2018 / 20:56