Change array structure in php

2

.tabela-grid{
	margin: 0;
	display: table;
	width: 100%;	
}
.tabela-grid *{
	vertical-align: top;
	text-align: center;
}
.tabela-grid img{
	width: 100%;
	display: inline-block;	
}
.tabela-columm{
	display: table-cell;
}
.tabela-columm:nth-child(even) .tabela-cell{
	display: table;
}

.tabela-cell{
	display: table-cell;
}
<div class="tabela-grid">
  <div class="tabela-columm" style="background-color: red">
    item1 </div>
  <div class="tabela-columm" style="background-color: green">
    <div class="tabela-cell">item2</div>
    <div class="tabela-cell">item3</div>
  </div>
  <div class="tabela-columm" style="background-color: red">
    item4 </div>
  <div class="tabela-columm" style="background-color: green">
    <div class="tabela-cell">item5</div>
    <div class="tabela-cell">item6</div>
  </div>
</div>

I have array that comes with the following content:

    array (size=6)
      0 => 
        array (size=6)
          'href' => string 'href' 
          'likes' => int 2
          'img' => string 'href'
          'text' => string 'comentario' 
          'width' => int 370
          'height' => int 370
      1 => 
        array (size=6)
          'href' => string 'href' 
          'likes' => int 12
          'img' => string 'href' 
          'text' => string 'comentario' 
          'width' => int 175
          'height' => int 175
      2 => 
        array (size=6)
          'href' => string 'href'
          'likes' => int 7
          'img' => string 'href'
          'text' => string 'comentario'
          'width' => int 175
          'height' => int 175
      3 => 
        array (size=6)
          'href' => string 'href'
          'likes' => int 1
          'img' => string 'href'
          'text' => string 'comentario'
          'width' => int 370
          'height' => int 370
      4 => 
        array (size=6)
          'href' => string 'href'
          'likes' => int 6
          'img' => string 'href'
          'text' => string 'comentario'
          'width' => int 175
          'height' => int 175
      5 => 
        array (size=6)
          'href' => string 'href'
          'likes' => int 5
          'img' => string 'href'
          'text' => string 'comentario'
          'width' => int 175
          'height' => int 175

I need to convert it to this format:

    array (size=2)
        0 => 
            array (size=2)
              0 => 
                array (size=6)
                      'href' => string 'href' 
                      'likes' => int 2
                      'img' => string 'href'
                      'text' => string 'comentario' 
                      'width' => int 370
                      'height' => int 370
              1 => 
                array (size=2)
                  0 => 
                  array (size=6)
                      'href' => string 'href' 
                      'likes' => int 12
                      'img' => string 'href' 
                      'text' => string 'comentario' 
                      'width' => int 175
                      'height' => int 175
                  1 => 
                    array (size=6)
                      'href' => string 'href'
                      'likes' => int 7
                      'img' => string 'href'
                      'text' => string 'comentario'
                      'width' => int 175
                      'height' => int 175
        1 => 
        array (size=2)
          0 => 
            array (size=6)
                  'href' => string 'href' 
                  'likes' => int 2
                  'img' => string 'href'
                  'text' => string 'comentario' 
                  'width' => int 370
                  'height' => int 370
          1 => 
            array (size=2)
              0 => 
              array (size=6)
                  'href' => string 'href' 
                  'likes' => int 12
                  'img' => string 'href' 
                  'text' => string 'comentario' 
                  'width' => int 175
                  'height' => int 175
              1 => 
                array (size=6)
                  'href' => string 'href'
                  'likes' => int 7
                  'img' => string 'href'
                  'text' => string 'comentario'
                  'width' => int 175
                  'height' => int 175

Is it possible to do this? I'm doing some testing so I've simplified the data to make it easier, but I still have not been able to do it:

    $entrada = array( 'item1', 'item2', 'item3', 'item4', 'item5', 'item6');

    $saida= array(
        0 => array( 0 => 'item1', 1 => array(0 => 'item2', 1 => 'item3')  ),    
        1 => array( 0 => 'item4', 1 => array(0 => 'item5', 1 => 'item6')  ),
    );

this array will populate this table:

    
asked by anonymous 31.03.2018 / 14:44

2 answers

2

I think I have a better way of organizing this, because I think the way you want it will be very complex to work with the array, but as the preference is yours, here is the code:

$entrada = array( 'item1' => array('href' => 'href', 
                                  'likes' => 2,
                                  'img' => 'href',
                                  'text' => 'comentario',
                                  'width' => 370,
                                  'height' => 370), 
                'item2' =>  array('href' => 'href', 
                                  'likes' => 2,
                                  'img' => 'href',
                                  'text' => 'comentario',
                                  'width' => 370,
                                  'height' => 370), 
                'item3' => array('href' => 'href', 
                                  'likes' => 2,
                                  'img' => 'href',
                                  'text' => 'comentario',
                                  'width' => 370,
                                  'height' => 370), 
                'item4' => array('href' => 'href', 
                                  'likes' => 2,
                                  'img' => 'href',
                                  'text' => 'comentario',
                                  'width' => 370,
                                  'height' => 370), 
                'item5' => array('href' => 'href', 
                                  'likes' => 2,
                                  'img' => 'href',
                                  'text' => 'comentario',
                                  'width' => 370,
                                  'height' => 370), 
                'item6' => array('href' => 'href', 
                                  'likes' => 2,
                                  'img' => 'href',
                                  'text' => 'comentario',
                                  'width' => 370,
                                  'height' => 370));

$saida = array();

array_push($saida, array( 0 => $entrada['item1'], 1 => array(0 => $entrada['item2'], 1 => $entrada['item3'])));
array_push($saida, array( 0 => $entrada['item4'], 1 => array(0 => $entrada['item5'], 1 => $entrada['item6'])));
var_dump($saida);

The output will be:

array (size=2)
  0 => 
    array (size=2)
      0 => 
        array (size=6)
          'href' => string 'href' (length=4)
          'likes' => int 2
          'img' => string 'href' (length=4)
          'text' => string 'comentario' (length=10)
          'width' => int 370
          'height' => int 370
      1 => 
        array (size=2)
          0 => 
            array (size=6)
              ...
          1 => 
            array (size=6)
              ...
  1 => 
    array (size=2)
      0 => 
        array (size=6)
          'href' => string 'href' (length=4)
          'likes' => int 2
          'img' => string 'href' (length=4)
          'text' => string 'comentario' (length=10)
          'width' => int 370
          'height' => int 370
      1 => 
        array (size=2)
          0 => 
            array (size=6)
              ...
          1 => 
            array (size=6)
              ...

The ellipsis is by the large size, to access just do:

$saida[0][0] , $saida[0][1] , $saida[0][1][0] ou $saida[0][1][1]
$saida[1][0] , $saida[1][1] , $saida[1][1][0] ou  $saida[1][1][1]
    
31.03.2018 / 15:13
3

Although Woton's answer was already satisfactory to you, I took the trouble to do something a little more 'automated'.

Since you commented on the possibility of not knowing the size of the array, I did so that this structure is created in a programmatic way. Dividing the array into subArrays where in an example [1,2,3,4,5,6,7,8] would give the answer [[1,[2,3]],[4,[5,6]],[7,[8]]] .

$entrada = [
  '0' => [
    'href' => 'href',
    'likes' => 2,
    'img' => 'item1',
    'text' => 'comentario',
    'width' => 370,
    'height' => 370
  ], 
  '1' =>  [
    'href' => 'href', 
      'likes' => 2,
      'img' => 'item2',
      'text' => 'comentario',
      'width' => 370,
      'height' => 370
    ], 
  '2' => [
    'href' => 'href', 
      'likes' => 2,
      'img' => 'item3',
      'text' => 'comentario',
      'width' => 370,
      'height' => 370
    ], 
  '3' => [
    'href' => 'href', 
      'likes' => 2,
      'img' => 'item4',
      'text' => 'comentario',
      'width' => 370,
      'height' => 370
    ], 
  '4' => [
    'href' => 'href', 
      'likes' => 2,
      'img' => 'item5',
      'text' => 'comentario',
      'width' => 370,
      'height' => 370
    ],
  '5' => [
    'href' => 'href', 
      'likes' => 2,
      'img' => 'item6',
      'text' => 'comentario',
      'width' => 370,
      'height' => 370
    ],
  '6' => [
    'href' => 'href', 
      'likes' => 2,
      'img' => 'item7',
      'text' => 'comentario',
      'width' => 370,
      'height' => 370
    ]
];
$i = $j = 0;
$saida = [];

for($i = 0; $i &lt count($entrada); $i+=3) {
  $outer = [];
  $outer[] = $entrada[$i];

  $inner = [];
  for($j = $i + 1; $j &lt count($entrada) && $j &lt (($i + 1) + 2); $j++) {
    $inner[] = $entrada[$j];
  }
  $outer[] = $inner;
  $saida[] = $outer;
}

print_r($saida);

Output:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [href] => href
                    [likes] => 2
                    [img] => item1
                    [text] => comentario
                    [width] => 370
                    [height] => 370
                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [href] => href
                            [likes] => 2
                            [img] => item2
                            [text] => comentario
                            [width] => 370
                            [height] => 370
                        )

                    [1] => Array
                        (
                            [href] => href
                            [likes] => 2
                            [img] => item3
                            [text] => comentario
                            [width] => 370
                            [height] => 370
                        )

                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [href] => href
                    [likes] => 2
                    [img] => item4
                    [text] => comentario
                    [width] => 370
                    [height] => 370
                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [href] => href
                            [likes] => 2
                            [img] => item5
                            [text] => comentario
                            [width] => 370
                            [height] => 370
                        )

                    [1] => Array
                        (
                            [href] => href
                            [likes] => 2
                            [img] => item6
                            [text] => comentario
                            [width] => 370
                            [height] => 370
                        )

                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [href] => href
                    [likes] => 2
                    [img] => item7
                    [text] => comentario
                    [width] => 370
                    [height] => 370
                )

            [1] => Array
                (
                )

        )

)
    
31.03.2018 / 15:56