I have two array
K = {"a", "b", "b"};
V = {"1", "2", "3"};
from these two array I want to got the result like this
$display = "a: 1; b: 2; c: 3;"
echo $display;
output
"a: 1; b: 2; c: 3;"
Anybody here please give me the idea?
thanks
I have two array
K = {"a", "b", "b"};
V = {"1", "2", "3"};
from these two array I want to got the result like this
$display = "a: 1; b: 2; c: 3;"
echo $display;
output
"a: 1; b: 2; c: 3;"
Anybody here please give me the idea?
thanks
http://php.net/manual/en/function.array-map.php
Use this, there is an example for exactly what you need
You want to merge them?
$display = "";
for ($i = 0; $i < count($K); $i++)
{
$display .= $K[$i] . ": " . $V[$i] . "; ";
}
Something like that. I didn't use PHP in a while.
You can use array_map()
to do this:
<?php
function combine($k, $v)
{
return "$k: $v";
}
$K = array("a", "b", "c");
$V = array("1", "2", "3");
$display = implode(", ", array_map("combine", $K, $V));
why not just store the array as an associative array to begin with so you don't have to combine them later on. e.g.,
$arr = array('a' => 1, 'b' => 2, 'c' => 3);
foreach($arr as $k => $v)
echo "$k: $v; ";
or if you have to have two separate arrays and combine them use php's internal array_combine function. e.g.,
$k = array('a','b','c');
$v = array(1,2,3);
$result = array_combine($k,$v);
foreach($result as $k => $v)
echo "$k: $v; ";