Reputation: 657
Currently I want to output a data from a database. I have two separate arrays. May I know is there a way to combine those arrays. The code is as below,
$array_1 = [
'memo' => 'title string',
'break_down' => 'title string',
'images' => 'title string',
'email_content' => 'title string'
];
// content from db
$array_2 = [$memo, $break_down, $images, $email_content];
// I want it to display like this
<?php foreach ($array_1 as $key=> $name) : ?>
<p>
<?= $name; ?> = <?php //content on $array_2 ?>
<?php //eg: title string = $memo and so on.. ?>
</p>
<?php endforeach ?>
Upvotes: 0
Views: 49
Reputation: 89557
With array_combine and array_keys:
$keys = array_keys($array_1);
$array_2 = array_combine($keys, $array_2);
foreach ($array_1 as $key => $name): ?>
<p>
<?= $name; ?> = <?php echo $array_2[$key]; ...
Note that you can also have the keys you want directly from your SQL query using the as
SQL keyword:
select field1 as memo, field2 as break_down ...
and using mysqli_fetch_assoc or PDO::FETCH_ASSOC
Upvotes: 3