Jess McKenzie
Jess McKenzie

Reputation: 8385

Matching strings between 2 arrays

In my PHP example below I have Item data and Amount data - How could I get it to output

Item 1 23 etc

PHP:

array(3) {
  [0]=>
  string(6) "Item 1"
  [1]=>
  string(6) "item 2"
  [2]=>
  string(6) "item 3"
}
array(3) {
  [0]=>
  string(2) "23"
  [1]=>
  string(2) "90"
  [2]=>
  string(2) "23"
}

Upvotes: 0

Views: 39

Answers (2)

Smuuf
Smuuf

Reputation: 6524

Either using array_combine() if you need to have one array of it.

array_combine($array1, $array2) will create something like:

array(3) {
  [23]=>
  string(6) "Item 1"
  [90]=>
  string(6) "item 2"
  [23]=>
  string(6) "item 3"
}

Or, if you want just to echo the values, foreach() it!

foreach ($array1 as $key => $value) {
  echo $array2[$key] . ": " . $value;
}

Upvotes: 0

SpencerX
SpencerX

Reputation: 5723

Try something like this:

$item = array("Item 1","item 2","item 3") ;
$amount = array("23","90","23") ;

foreach ($item as $key => $value) {
    $result_array[] = $value." ".$amount[$key];
}
print_r($result_array);

OUTPUT

Array
(
    [0] => Item 1 23
    [1] => item 2 90
    [2] => item 3 23
)

Upvotes: 2

Related Questions