How to format this array?

6

I have the following array:

$dados = array(
    '0' => array(
        'id_assinante' => $id,  
        'nome' => 'Aluguel'  
    ),
    '1' => array(
        'id_assinante' => $id,  
        'nome' => 'Água'  
    ),
    '2' => array(
        'id_assinante' => $id,  
        'nome' => 'Contabilidade'  
    ),
    '3' => array(
        'id_assinante' => $id,  
        'nome' => 'Energia'  
    ),
    '4' => array(
        'id_assinante' => $id,  
        'nome' => 'Imposto'  
    ),
    '5' => array(
        'id_assinante' => $id,  
        'nome' => 'IPTU'  
    ),
    '6' => array(
        'id_assinante' => $id,  
        'nome' => 'Marketing'  
    ),
    '7' => array(
        'id_assinante' => $id,  
        'nome' => 'Telefone'  
    )               
);

In this case it works perfectly, but is there any way to do this more simply? Since $id will always be equal, and each value in the name field will have a different name?

    
asked by anonymous 21.12.2016 / 12:14

4 answers

5

Do you want anything like this?

<?php

$array = array();

$id = "meu_id";

$nomes = ["Aluguel", "Água", "Contabilidade", "Energia",
          "Imposto", "IPTU",  "Marketing"   , "Telefone"]; 

$max = count( $nomes );

for( $count = 0; $count < $max; $count++ ) 
{
    $array[$count] = array(
        'id_assinante' => $id,  
        'nome'         => $nomes[$count] );  
}

See it working: link

    
21.12.2016 / 12:20
4

One way to do this is:

The $id should be global in this case. If it is it can be used like this:

function map_nomes($m){
    $id = 1;
    return ['nome' => $m, 'id_assinante' => $id];
}

$nomes = ['Aluguel', 
          'Água', 
          'Contabilidade', 
          'Energia', 
          'Imposto', 
          'IPTU', 
          'Marketing', 
          'Telefone'
          ];
$response   = array_map("map_nomes", $nomes);

Running SandBox PHP

Manual version:

$dados = array(
    'id_assinante' => $id, 
    '0' => array(
        'nome' => 'Aluguel'  
    ),
    '1' => array(
        'nome' => 'Água'  
    ),
    '2' => array(
        'nome' => 'Contabilidade'  
    ),
    '3' => array(
        'nome' => 'Energia'  
    ),
    '4' => array(
        'nome' => 'Imposto'  
    ),
    '5' => array(
        'nome' => 'IPTU'  
    ),
    '6' => array(
        'nome' => 'Marketing'  
    ),
    '7' => array(
        'nome' => 'Telefone'  
    )               
);
    
21.12.2016 / 12:20
3

The question is vague because it does not know where the data comes from and where it will be used or if there are other conditions such as a second subscriber ID, for example.

For the time being, I would kick something like this:

$arr = array(
    'id_assinante' => $id,
    'servicos' => array('Aluguel', 'Água', 'Contabilidade', 'Energia', 'Imposto', 'IPTU',  'Marketing', 'Telefone');
);
    
21.12.2016 / 12:47
2

I think the easiest and most organized way would be to use array_map

$id = 10;
$arr = array_map(function($index) use ($id) {
    $index['id_assinante'] = $id;
    return $index;
}, $dados);

Performing the test with only 3 elements in the array;

var_dump($arr);
array(4) {
  [0]=>
  array(2) {
    ["nome"]=>
    string(7) "Aluguel"
    ["id"]=>
    int(10)
 }
 [1]=>
  array(2) {
    ["nome"]=>
    string(5) "Água"
    ["id"]=>
    int(10)
 }
 [2]=>
  array(2) {
    ["nome"]=>
    string(13) "Contabilidade"
    ["id"]=>
    int(10)
 }
  [3]=>
  array(2) {
    ["nome"]=>
    string(7) "Energia"
    ["id"]=>
    int(10)
 }
}

In this way you do not have to do loops for or foreach nor do clunky logic with array indexes.

    
21.12.2016 / 13:42