Change array structure in php

Asked

Viewed 54 times

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 a array which 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 that 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 it this way? I’m doing some tests so I simplified the data to make it easier, but I haven’t yet:

    $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 popular this table: inserir a descrição da imagem aqui

  • Could you explain the because wishes to do this.. and if you have any logic in the process, or it would simply be to make the structure more complex?

  • because of a table in the application frontend, I will put the image:

  • I didn’t really understand why that, for me it only makes her more complex to work with

  • the table that will display this array was already like this

  • Can you post the input array code? I’ll do an example here and I don’t want to reassemble all kkkk

  • Right.. It seems to me that there should be some logic in the structuring of this matrix, which you’re not making very clear. Without understanding the logic of this structure there is not much to do but only divide the array into arbitrary parts within another.

  • the input is that of the variable $entrada = array(“item1”,”item2”,”item3”, “item4”,”item5”,”item6”); what is complicating is that the input is a simple array, if it is possible to popular the table with that same array helps a lot!

  • @Israelmerljak added the table structure!

  • @Hebertdelima "item1", "item2" and "item3" have any relation?? If not, the answer from Woton is for you.. but I still think that if this array increases, it will become unviable this approach.

  • @Israelmerljak I will test the answer of woton but I think it will work, it makes perfect sense his logic

Show 5 more comments

2 answers

3

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

Since you commented on the possibility of not knowing the size of the array, I did so so that this structure is created in a programmatic way. Dividing the array into subArrays and 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 < count($entrada); $i+=3) {
  $outer = [];
  $outer[] = $entrada[$i];

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

print_r($saida);

Exit:

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
                (
                )

        )

)
  • Worst guy I just solved this: for ($i=0; $i < count($entrada[$i]); $i++) { array_push($saida, array( 0 => $entrada[$i], 1 => array(0 => isset($entrada[$i++])?$entrada[$i++]: null , 1 => isset($entrada[$i])?$entrada[$i]:null)));} but thanks for the answer =D

2


I think I have a better way to organize 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 exit 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 reticence is for the big size, to access it is only to 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]
  • Vlw! worked out, just a doubt, in this case you used to map the array pq knew the size of it, but in an environment you don’t know the size of the array? for example if there is another 1 item in this array, I would need to put more: array_push($saida, array( 0 => $entrada['item7'])); etc.

  • found the solution: for ($i=0; $i < Count($input); $i++) { array_push($output, array( 0 => $input[$i], 1 => array(0 => $input[$i], 1 => $input[$i])); }

  • @Hebertdelima yes this is a solution, however, it is good to do a treatment because, it waits 3 index for each push, if you have until the item7 for example, at the time of the 3 push it should give error for not existing the item8 and the item9

  • @Hebertdelima vc can use an if(isset($entry[position]))

  • vlw has now completed the solution, thank you very much!

Browser other questions tagged

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