简体   繁体   中英

PHP combine key value array and regular array

I have 2 arrays:

array1 :

[0]=>
string(10) "AAAAAAAAAAA"
[1]=>
string(10) "BBBBBBBBBBB"
...

and array2:

 [0]=>
 float(0)
 [550]=>
 float(55)
 ...

I need a result like this:

 "AAAAAAAAAAA"  : 0 : 0
 "BBBBBBBBBBB" : 550: 55
  ...

ie how to combine the arrays. How do i get that?

suppose you two arrays have the same length,

$keys = array_keys($array1);
$values = [];
foreach($array2 as $k=>$v)
{
  $values[] = $k.':'.$v;
}
$result = array_combine($keys, $values);

The result you want is not clear... if each rows are just a string, this should work :

$a = [
    0 => "AAAAAAAAAAA",
    1 => "BBBBBBBBBBB"
];

$b = [
    0 => (float) 0,
    550 => (float) 55
];

$result = array_map(
    function($v1, $v2, $v3) {
        return "$v1 : $v2 : $v3";
    },
    $a, array_keys($b), $b
);

var_dump($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM