Massaging an array to create a cakephp popup

this is my problem i, ve create a drop-down list from table states ('id', 'state_name'), which is not my default model (which has many fields, one of the fields is the state in which I store states ('id '). so I used loadModel to populate the dropdown list. in my controller I used

$this->loadModel('State');
$this->set('states',$this->State->find('all'));

on the side of viewing

$form->select('State_id',$states);

the output displays the table name, the identifier and name are displayed.

when I printed $ state using pr (); I got

Array
(
    [0] => Array
        (
            [State] => Array
                (
                    [id] => 1
                    [state_name] => state1
               )

        )

    [1] => Array
        (
            [State] => Array
                (
                    [id] => 2
                    [state_name] => state2
                )

        )

etc.

how to create an array similar to an array (1 => state1, 2 => state2) from the specified array or is there another way to create a drop-down list

kind help

+3
2

:

$fields = array('id','state_name');
$states = $this->State->find('list',array('fields'=>$fields));
$this->set(compact('states'));

:

$this->set('states',$this->State->find('list',array('fields'=>array('id','state_name'))));
+1

,

$newstates = array();

foreach($states as $state) {
    $state = $state['State']
    $newstates[$state['id']] = $state['state_name'];
}

print_r($newstates);

:

Array
(
    [1] => state1
    [2] => state2
)
+1

Source: https://habr.com/ru/post/1751734/


All Articles