Join 2 arrays

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 put the Name together with the Phone the array of people is the id + name and the phone is array + phone field.

    
asked by anonymous 24.04.2018 / 19:18

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 it exists it concatenates the string.

    
24.04.2018 / 19:30
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);
    
26.04.2018 / 02:03