Mount a two-dimensional array from another array

Asked

Viewed 88 times

3

I have the array $dados down below:

array(5) {
   [0]=> "2018-03-28"
   [1]=> "jantar"
   [2]=> "lanche"
   [3]=> "2018-03-29"
   [4]=> "lanche"
}

From this array, how could I mount another two-dimensional array to look like this:

$dados1 = array(
   '2018-03-28' => array('jantar','lanche'),
   '2018-03-29' => array('lanche')
);

Note that each item in the form of date create a new array within $dados1 and this array contains the subsequent items until the next date.

Since I don’t know how to mount this type of array with PHP, I couldn’t think of a way to do it.

1 answer

3


It is possible to do what you want in the following way:

<?php
// Seu array inicial
$array = array("2018-03-28", "jantar", "lanche","2018-03-29", "lanche");

// Declaração do array final e variável auxiliar de data
$dados1 = array();
$data_atual = null;

foreach ($array as $key => $value) {
    // Verifica se o $value é uma data
    if (date('Y-m-d', strtotime($value)) == $value) {
        // Salva qual a data atual para o array bidimensional
        $data_atual = $value;
    }else{
        // Adiciona o tipo de refeição na data atual
        $dados1[$data_atual][] = $value;
    }
}

var_dump($dados1);
?>

var_dump will come up with a reply like:

array(2) { ["2018-03-28"]=> array(2) { [0]=> string(6) "dinner" [1]=> string(6) "snack" } ["2018-03-29"]=> array(1) { [0]=> string(6) "snack" } }

Edit for date cases in "2018-03-1 format":

<?php 
$array = array("2018-03-1", "jantar", "lanche","2018-03-29", "lanche");

$dados1 = array();
$data_atual = null;

foreach ($array as $key => $value) {
    $data1 = date('Y-m-d', strtotime($value));
    $data2 = $value;

    // Verifica se o $value é uma data
    if (strtotime($data1) == strtotime($data2)) {
        $data_atual = $value;
    }else{
        $dados1[$data_atual][] = $value;
    }
}

var_dump($dados1);
?>

The var_dump there will be an answer like:

array(2) { ["2018-03-1"]=> array(2) { [0]=> string(6) "dinner" [1]=> string(6) "snack" } ["2018-03-29"]=> array(1) { [0]=> string(6) "snack" } }

  • Strange, on this site Exit really finished before var_dump. On my localhost this problem did not occur. I will delete from my example :)

  • It worked right. Obg!

  • @DVD in real or var_dump is necessary hehe. It was just an example to show the output of the array.

  • Namesake, let me ask you a question: when the date is type 2018-03-1 (the day with only 1 digit) does not work... Any hint how to get around this?

  • The result looks like this: array(2) { [""]=> array(3) { [0]=> string(9) "2018-03-1" [1]=> string(6) "Jantar" [2]=> string(6) "Lanche" } ["2018-03-29"]=> array(1) { [0]=> string(6) "Lanche" } }

  • See that the first item is empty

  • If it is mt complicated I put in the bank with 2 digits same, same as the month

  • @dvd made an edition in answer that solves your problem

  • Thanks dude Obg!

Show 4 more comments

Browser other questions tagged

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