tags:

views:

100

answers:

4

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

+2  A: 

http://php.net/manual/en/function.array-map.php

Use this, there is an example for exactly what you need

Mike
Example #3 to be precise
Mike
+3  A: 

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.

LukeN
yes,LukeN,just simple , that I could not found , thanks for your help.
python
+1  A: 

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));
Bill Karwin
+1  A: 

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; ";
Azeem Michael