Merge php arrays

Asked

Viewed 131 times

0

Hello!

There is the possibility of joining array’s ?

If yes how can I do ?

What I got

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 desire

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);
    }           
}
  • 1

    Use the array_merge

  • I’ve tried, but from what I understand, by following the manual, array_merge needs two parameters, and I only have one $array.

  • The array_merge function only has a "required" parameter (Combines one or more arrays).

2 answers

1

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

Follow an example:

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

$c = array_merge($a, $b);
print_r($c);
?>
  • I don’t have the parameter $a e $b, I just got the $a, in this case $array. I’ve tried with array_merge and array_unique

  • Can you send an example? of the call to your function?

  • The array_unique function removes duplicates. You do not want to join arrays?

  • Yes, I want to join arrays

  • I tried that way and it didn’t work. $array = [&#xA; "$array_chave" => $js['state']->selected,&#xA; ];&#xA; &#xA; print_r(array_merge($array));

  • your array is not well defined

  • <?php $js = array(); $js['state'] = array(); $js['state']['Selected'] = 'AAA'; $array_key = 'key'; $array = [$array_key => $js['state']['Selected']]; print_r(array_merge($array));

  • the problem is not the array_merge function, see the array that is passing to it

  • make a print_r of $js, $js['state'] and $js['state']['Selected'] to see the values

  • attention that also of being $array_key => and not ""$array_key".... $array_key is a variable and is not a string

  • See the print of $js and $js['state']->Selected: https://pastebin.com/0pecNAfz

  • I tested its function inside the loop and looked like the array of the question. Array&#xA;(&#xA; [chave] => AAA&#xA;)

  • what is the purpose of the loop?

  • see Lauro’s answer, it seems to be correct, I didn’t know it was that you wanted to return an array at the end.... by which, I asked the purpose of the loop

  • It did, thank you..

Show 10 more comments

1


The problem is the function edit_jstree() you’re making a "loop" and enveloping each item into one {Array} ... could simply declare a {Array} before the foreach() and add key value during the "loop", later "printar" (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);   
}

Browser other questions tagged

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