Decrease a multidimencional array

Asked

Viewed 61 times

-1

Good evening, I am with a dilemma, I have tried in many ways but my knowledge with array is still vague, I would like help in the following, I have two arrays, one of them I already solved with a foreach because it was a simple array just like this model

$data_content = Array
    (
        0 => null,
        1 => 'Fye Flourigh',
        2 => '+5521888',
        3 => null,
        4 => null,
        5 => null,
        6 => null
    );

but the second array is pretty weird, I need to make this array2

Array
(
    [0] => Array
        (
            [COLUMN_COMMENT] => 
            [0] => 
        )

    [1] => Array
        (
            [COLUMN_COMMENT] => Nome do contato
            [0] => Nome do contato
        )

    [2] => Array
        (
            [COLUMN_COMMENT] => Whatsapp
            [0] => Whatsapp
        )

    [3] => Array
        (
            [COLUMN_COMMENT] => Email
            [0] => Email
        )

    [4] => Array
        (
            [COLUMN_COMMENT] => Site
            [0] => Site
        )

    [5] => Array
        (
            [COLUMN_COMMENT] => Facebook
            [0] => Facebook
        )

    [6] => Array
        (
            [COLUMN_COMMENT] => Messenger
            [0] => Messenger
        )

)

equals array 1 so I can use

follows two links to try to understand. this link 1 shows how it should look http://ideone.com/1jMvaj

and this link 2 shows how it is http://ideone.com/FrHa2K

1 answer

1


You should do what you did before, but only capture what you want, in case $array[0] inside a loop, or not.

Apparently already has knowledge about the for, so I’ll use it.

// Sua array:
$array = array(
    array
        (
            'COLUMN_COMMENT' => '',
            '0' => ''
        ),
    array
        (
            'COLUMN_COMMENT' => 'Nome do contato',
            '0' => 'Nome do contato'
        ),
    array
        (
            'COLUMN_COMMENT' => 'Whatsapp',
            '0' => 'Whatsapp'
        ),
    array
        (
            'COLUMN_COMMENT' => 'Email',
            '0' => 'Email'
        ),
    array
        (
            'COLUMN_COMMENT' => 'Site',
            '0' => 'Site'
        ),
    array
        (
            'COLUMN_COMMENT' => 'Facebook',
            '0' => 'Facebook'
        ),
    array
        (
            'COLUMN_COMMENT' => 'Messenger',
            '0' => 'Messenger'
        )
);

$contagemArray = count($array);
// Quantidade de array existentes : 7

for($idArray=0; $idArray < $contagemArray; $idArray++) { 

  $atualArray = $array[$idArray];  
  // Array selecionada (0, 1, 2, 3)...

  $data_content[] = $atualArray[0];
  // Seleciona o valor "0" da array selecionada anteriormente. 

}

$data_content will contain:

array(7) {
  [0]=>
  string(0) ""
  [1]=>
  string(15) "Nome do contato"
  [2]=>
  string(8) "Whatsapp"
  [3]=>
  string(5) "Email"
  [4]=>
  string(4) "Site"
  [5]=>
  string(8) "Facebook"
  [6]=>
  string(9) "Messenger"
}
  • worked perfectly, but you commented on doing with for, would there be another way? would it be what? foreach or to crazy? just out of curiosity, but it worked right here, thankful.

  • There is the foreach and there is the while, within each situation can do in various ways.

  • if it’s not abuse, and when it’s time, could you put one in foreach for me to see? is what I’m learning then it would help.

Browser other questions tagged

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