Add array at the end of another array in a foreach

Asked

Viewed 189 times

-2

$celulas = array (

  0 => array ("idCelula" => 17,"nomeCelula" => "Célula 1" ),
  1 => array ("idCelula" => 18,"nomeCelula" => "Célula 2" )

);

foreach ( $celulas as $celula ) {

        $celulasPesquisa = null;

        $celulasReunioes = array (

          0 => array ("idCelula" => 17,"diaReuniao" => 5)

        );
        array_push ( $celula, $celulasReunioes );
}

print_r($celulas);

Man goal is that for each $celula of array $celulas be it added one array $celulasReunioes who brings the meetings that cell.

Namely that the array final $celulas stay that way:

$celulas = array (

  0 => array (
               "idCelula" => 17,
                "nomeCelula" => "Célula 1", 
                array ( 0 => array ("idCelula" => 17,"diaReuniao" => 5)

  1 => array (
               "idCelula" => 18,
                "nomeCelula" => "Célula 2", 
                array ( 0 => array ("idCelula" => 18,"diaReuniao" => 12)

);

What needs to be done?

'Cause the way I’m doing it’s not working!

Detail, I tried to create a minimal example according to orientation.

The way it is,

print_ ( $celulas );

prints:

$celulas = array (

  0 => array ("idCelula" => 17,"nomeCelula" => "Célula 1" ),
  1 => array ("idCelula" => 18,"nomeCelula" => "Célula 2" )

);

That is, without the addition of foreach

  • Man, what a mess! Your goal final all right desstructured...

  • all right, kkk, but I’m already taking care of this mess... thanks

2 answers

1


If you need to change the element of array, needs to maintain the reference of the same. Remember that the foreach naturally iterates over a copy of array.

So you need to put the character & indicating the reference:

foreach ($celulas as &$celula) {

}

Doing this will get the desired result:

Array
(
    [0] => Array
        (
            [idCelula] => 17
            [nomeCelula] => Célula 1
            [0] => Array
                (
                    [0] => Array
                        (
                            [idCelula] => 17
                            [diaReuniao] => 5
                        )

                )

        )

    [1] => Array
        (
            [idCelula] => 18
            [nomeCelula] => Célula 2
            [0] => Array
                (
                    [0] => Array
                        (
                            [idCelula] => 17
                            [diaReuniao] => 5
                        )

                )

        )

)

But I feel obliged to comment that it doesn’t make much sense to have a array associative with numeric indexes. It would make much more sense in this case the data is in the index "reunioes" or something equivalent.

  • that strange, created the incident but did not bring the value of the added array. brought empty.

  • So you did something wrong, by taking your code and just adding the & should already work: https://repl.it/@acwoss/Severedistorteddrupal.

  • @Carlosrocha man, I’ve noticed that you’re having a hard time reproducing the codes of the answers here on the site. Which version of PHP are you using?! In that other question I asked an answer you were not able to reproduce. By the way, I tried to GENERATE your problem and I couldn’t... There’s something wrong there! Suddenly you’re running a PHP 1.0 and you don’t know.(kkk) It ends up being difficult to reproduce the codes...

  • has yes brother, the goal was to create as the other colleague spoke another index called "meetings" and popular it with the result of the query that searches the meetings in the bank, ie, $celula ["renunioes"] = array()

  • dí tried to do array_push ( $celula["reunioes"], $celulasReunions ); exits empty again. Creates the inidce, but with empty value. Whether numeric input output value

  • Put your example working in repl.it and send us the link. Only in the speculation of what you did wrong we will not be able to help.

  • Done, https://repl.it/@Carlosrocha1/Worthwhilesillysolidstatedrive? language=php&folderId=

  • @Carlosrocha seems to be working normally, where is the problem?

  • I cannot replace the index [0] that is next to the words, switch to "meetings" without adding more items to the meetings array

  • i.e., I would like them to be, [idCelula], [nomeCelula], [meetings] instead of [idCelula], [nomeCelula], [0]

  • But in the question you requested that the index be numerical, not "meetings". Your question is getting more and more confused...

  • Yeah, sorry, that’s the appeal I had at first since I wasn’t getting it, so I figured, if I can get numbers with letters I can get them.. Again: apologies for failure

  • I just tried to ask a question as dry as possible and in that attempt I ended up rectifying a foreach. Just here in the system add the joker & to the 2 foreach that worked. Thank you all

Show 8 more comments

0

I put the &$celula so that it actually changes the array by passing it by reference and prints the result below, which is what you want:

<?php

$celulas = array(
    0 => array("idCelula" => 17, "nomeCelula" => "Célula 1"),
    1 => array("idCelula" => 18, "nomeCelula" => "Célula 2")
);

$org_celula = array();

$org_celula[] = [
    'idCelula' => 17,
    'diaReuniao' => 5
];

$org_celula[] = [
    'idCelula' => 18,
    'diaReuniao' => 12
];

foreach ($celulas as $index => &$celula) {
    foreach ($org_celula as $key => $cell) {

        if ($key === $index) {
            $celula['celula'] = $cell;
        }
    }
}

print_r($celulas);

Browser other questions tagged

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