0

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
  ...

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

3
  • 1
    ur array is not proper Commented Feb 10, 2017 at 14:52
  • Please visit How to Ask : stackoverflow.com/help/how-to-ask Commented Feb 10, 2017 at 14:53
  • Strictly speaking, that isn't really combining the arrays since there is no real correlation between them. (there's nothing linking the BBBBBBBBB and the 550). But you could just loop through one of the arrays and fetch the matching counterpart from the other array (since the first array has sequential, numerical keys, it would be easier to loop the second one, increasing a counter each time, and fetch the content from the first array based on that counter) Commented Feb 10, 2017 at 14:54

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.