Merge php arrays

0

Hello

Is there the possibility of joining array's?

If yes, how can I do it?

What I have

Array
(
    [Dashboard_1] => 1
)
Array
(
    [Configuração_2] => 1
)
Array
(
    [Usuário_3] => 1
)
Array
(
    [Criar_4] => 1
)
Array
(
    [Editar_5] => 1
)
Array
(
    [Excluir_6] => 1
)
Array
(
    [Visualizar_7] => 1
)

What I want

Array
(
    [Dashboard_1] => 1
    [Configuração_2] => 1
    [Usuário_3] => 1
    [Criar_4] => 1
    [Editar_5] => 1
    [Excluir_6] => 1
    [Visualizar_7] => 1
)

Function

// edit_jstree
public function edit_jstree($dados, $permissao_id)
{
    //print_r($dados);
    $array = (array) $dados;
    foreach ($dados as $js)
    {
        $jstree = [
            'acesso'  => $js['state']->selected,
        ];
        $this->db->where('identificacao', $js['id']);
        $this->db->where('permissao_id', $permissao_id);
        $this->db->update('tb_jstree', $jstree);

        $array_chave = $js['text'].'_'.$js['id']; // concatena texto + identifiacao para montar a chave do array

        $array = [
            "$array_chave"  => $js['state']->selected,
        ];

        $array_serialize = serialize($array);           
        print_r($array_serialize);
    }           
}
    
asked by anonymous 03.04.2018 / 16:31

2 answers

1
The problem lies in the edit_jstree() function you are doing a loop and enveloping each item in a {Array} ... could simply declare {Array} before foreach() e add value key during loop , then "start" (or return):

// edit_jstree
public function edit_jstree($dados, $permissao_id)
{
    // declarar uma variável (Array)
    $base_array = [];
    //print_r($dados);
    $array = (array) $dados;
    foreach ($dados as $js)
    {
        $jstree = [
            'acesso'  => $js['state']->selected,
        ];
        $this->db->where('identificacao', $js['id']);
        $this->db->where('permissao_id', $permissao_id);
        $this->db->update('tb_jstree', $jstree);

        $array_chave = $js['text'].'_'.$js['id']; // concatena texto + identifiacao para montar a chave do array

        $base_array[$array_chave] = $js['state']->selected;
    }        
    print_r($base_array);   
}
    
03.04.2018 / 17:08
1

You can use the function array_merge (array $ array1 [ array $ ...])

Here's an example:

<?php
$a= array();
$a['xpto'] = 'aa';
$b= array();
$b['xyzz'] = 'bb';

$c = array_merge($a, $b);
print_r($c);
?>
    
03.04.2018 / 16:40