0

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"
}
0

2 Answers 2

2

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

Comments

0

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

1 Comment

Would fail as two items would attempt to share the same key.

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.