Join 2 arrays

Asked

Viewed 88 times

0

I have two arrays

$pessoas =  1 => "ERICA GRELLERT"
    2 => "IARA BEATRIZ"
    3 => "DANIELLE"
    4 => "MARIANA"
    5 => "ALESSANDRA"
    6 => "EDUARDO"
    7 => "DIEGO"
    8 => "ROBERTA"
    9 => "THIAGO"
    10 => "LIGIA"
    11 => "LUCIANA"
    12 => "CRIS"
    13 => "JEORGIA"
    14 => "FLORA"
    15 => "MAUREN MOTTA"
    16 => "VERUSKA"
    17 => "SHANA"
    18 => "ADRIANA"
    19 => "TATIANA"
    20 => "CAROLINA ROUBBER"
    21 => "ALESSANDRA"
    22 => "JOSIANE"
    23 => "GRACIELE"
    24 => "ELIZABETH"

and a Second Array

$telefones= 0 => "(51)33379375"
    1 => "(51)998044826"
    2 => "(51)981843308"
    3 => "(51)99865160"
    4 => "(51)92318000"
    5 => "(51)3319202"
    6 => "(51)981055535"
    7 => "(51)999333660"
    8 => "(51)980151052"
    9 => "(51)34072405"
    10 => "(51)99490412"
    11 => "(51)81518416"
    12 => "(51)992912340"
    13 => "(51)999531108"
    14 => "(51)30726875"
    15 => "(51)965562707"
    16 => "(51)992524736"
    17 => "(51)998210586"

what I need is to join the Name with the Telephone the people array is id + name and the phone is array + phone field.

  • The 2 on the same array line or just print on the same line ?

  • The person begins in the 1 and the phone on 0 ? There are repetitions in people’s names ? If so, how will it proceed?

2 answers

1


You can do it this way:

<?php

$pessoas =  
    array(1 => "ERICA GRELLERT",
    2 => "IARA BEATRIZ",
    3 => "DANIELLE",
    4 => "MARIANA",
    5 => "ALESSANDRA",
    6 => "EDUARDO",
    7 => "DIEGO",
    8 => "ROBERTA",
    9 => "THIAGO",
    10 => "LIGIA",
    11 => "LUCIANA",
    12 => "CRIS",
    13 => "JEORGIA",
    14 => "FLORA",
    15 => "MAUREN MOTTA",
    16 => "VERUSKA",
    17 => "SHANA",
    18 => "ADRIANA",
    19 => "TATIANA",
    20 => "CAROLINA ROUBBER",
    21 => "ALESSANDRA",
    22 => "JOSIANE",
    23 => "GRACIELE",
    24 => "ELIZABETH");

$telefones = array(0 => "(51)33379375",
    1 => "(51)998044826",
    2 => "(51)981843308",
    3 => "(51)99865160",
    4 => "(51)92318000",
    5 => "(51)3319202",
    6 => "(51)981055535",
    7 => "(51)999333660",
    8 => "(51)980151052",
    9 => "(51)34072405",
    10 => "(51)99490412",
    11 => "(51)81518416",
    12 => "(51)992912340",
    13 => "(51)999531108",
    14 => "(51)30726875",
    15 => "(51)965562707",
    16 => "(51)992524736",
    17 => "(51)998210586");

$completo = array();
foreach($pessoas as $key => $pessoa){
    if(isset($telefones[$key])){
        $completo[$key] = $pessoa . " " . $telefones[$key];
    }else{
        $completo[$key] = $pessoa;
    }

}
echo "<pre>";
var_export($completo);

It checks if there is index in the other array, if there is it concatenates the string.

0

I hope this helps:

$pessoas =  array(
    0 =>"NOME",
    1 => "ERICA GRELLERT",
    2 => "IARA BEATRIZ",
    3 => "DANIELLE",
    4 => "MARIANA",
    5 => "ALESSANDRA",
    6 => "EDUARDO",
    7 => "DIEGO",
    24 => "ELIZABETH");

$telefones= array(
    0 => "(51)33379375",
    1 => "(51)998044826",
    2 => "(51)981843308",
    3 => "(51)99865160",
    4 => "(51)92318000",
    5 => "(51)3319202",
    6 => "(51)981055535",
    7 => "(51)999333660",
    17 => "(51)998210586");


$result = array_combine($pessoas, $telefones);
print_r($result);

Browser other questions tagged

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