Map 2 Arrays to produce a desired string
Map 2 Arrays to produce a desired string
So i have two arrays and i want to map 0th key of first array with 0th key of second array and both array could have n number of keys ,so that the final Result which would be a string should look something like mentioned below
Array
(
[0] => 1
[1] => 2
)
Array
(
[0] => 5
[1] => 10
)
Result--
1:5,2:10
i tried this--
which gives output
:1:5,1:10,2:5,2:10,
Post your code as plain text, not an image. See formatting for code formatting help.
– Barmar
Sep 14 '18 at 15:22
You mean map 0th *value* of first array with 0th *value* of second array right?
– ryantxr
Sep 14 '18 at 15:33
4 Answers
4
A bit verbose but will handle if both input arrays are 2 different lengths.
<?php
$array_one = [1,2];
$array_two = [5,10];
// Array we work with to push the merged data too,
$array_result = ;
foreach($array_one as $index => $value)
$array_result[$index] = $value;
foreach($array_two as $index => $value)
// If the first number was set from the $array_one iteration then concatenate ':' and this $array_two_value, else simply add this $array_two_value.
if(isset($array_result[$index]))
$array_result[$index] .= ':' . $value;
else
$array_result[$index] .= $value;
echo implode(',',$array_result);
Use array_map and implode:
// Function which concats two given strings with ':' and returns
function concatValues($v1,$v2)
return($v1.':'.$v2);
// Input arrays
$first_input_array = Array ( [0] => 1 [1] => 2 );
$second_input_array = Array ( [0] => 5 [1] => 10 );
// array_map to concat corresponding values from the two input arrays
$output = array_map('concatValues',
array_values($first_input_array),
array_values($second_input_array)
);
// Implode to get comma separated string
$output = implode(',', $output);
To map it into another array, you can do it like this:
<?php
$a1 = [1,2,3,4,5];
$a2 = [5,4,3,2,1];
$a3 = ;
foreach($a1 as $k => $v)
$a3[$v] = null;
if(array_key_exists($k, $a2))
$a3[$v] = $a2[$k];
print_r($a3);
it get the requested string you may do it like this:
<?php
$a1 = [1,2,3,4,5];
$a2 = [5,4,3,2,1];
$a3 = ;
foreach($a1 as $k => $v)
if(array_key_exists($k, $a2))
$a3 = $v . ':' . $a2[$k];
echo implode(',', $a3);
You are nesting loops which is not what you want to do here.
I will assume that both arrays have the same number of elements so that we can make a simple solution.
$output = '';
if ( count($a1) == count($a2) )
foreach ( $a1 as $i => $val1 )
// put the comma if this is not the first one
if ( $i > 0 ) $output .= ',';
$output .= "$val:$a2[$i]";
echo $output;
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Have you tried anything? Post your attempts
– Rafael
Sep 14 '18 at 14:53