How to format this array?

Asked

Viewed 130 times

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 it more simply? Being that the $id will always be the same, and each name field value will have a different name?

  • This array is mounted fixed, same as in the question?

  • There is the code... Yes it is mounted fixed, I will include the items manually...

4 answers

5


You want something like that?

<?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 working: http://ideone.com/2wZVut

4

One way to do it is like this:

The $id should be global in this case. If it is 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'  
    )               
);
  • My idea would be to make it a little more customized, so I can do it with less repetitive code

  • 1

    Got it. So Jorge’s way is better. I thought you wanted it manually.

  • I put another way to do it too. I don’t know if you’d wish it that way.

  • no... the return needs to be as I showed, because I use insert_batch (codeigniter), to include these records in the database

  • See now if this is it. I mean, if you want why you already have the solution

  • I see it, but it is not yet, it has to return in the way of the multi-dimensional array otherwise I can not insert, but I have interest in having a third option, for me to study really, I think it’s cool. : ) Thank you for your commitment.

  • 1

    I understand what you need. But the way I did it may not be the case then, using the array_map.

  • I made a change, if you want to see how it is.

  • Cool, you can post in PHP there to see if it works? :)

  • 1

    Exactly, it’s a good option yours too!

Show 6 more comments

3

The question is vague because it is not known 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 now, I’d kick something like that:

$arr = array(
    'id_assinante' => $id,
    'servicos' => array('Aluguel', 'Água', 'Contabilidade', 'Energia', 'Imposto', 'IPTU',  'Marketing', 'Telefone');
);

2

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

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

Performing the test with only 3 array elements;

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

This way you don’t need to make loops for or foreach or make messy logics with array indices.

  • But where are the names? Also put so I see a better way to use the array_map.

  • 1

    @Gumball the names you don’t need to mess with, as we just want to add the "id" Indice into each array.

  • @Rafaelacioly by your conversation... did you give -1? What is the problem of having loop or indexes? You know how is implemented the array_map?

  • By the way, AP doesn’t just want to automate the id, it also wants the rest.

  • @Rafaelacioly where are the names for inclusion? : ) Obg for his answer.

  • @Andrébaill you want to assemble a two-dimensional array of 0? is that it? What I understood in your question is that Voce needs to add the "id" field in all arrays...

  • @Jorgeb. his question is still a little confusing, what he really wants is to create several arrays within an array with dynamic names, please disconnect my answer

Show 2 more comments

Browser other questions tagged

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