Reputation: 11
after looking through loads of questions on here i still carnt find an answer that suits my situation.
im trying to join 2 fields from array #2 into array #1
Array #1
Array ( [0] => Array ( [id] => 1 [position] => top_banner_1 [name] => Top Banner 1 [order] => 1 ) [1] => Array ( [id] => 2 [position] => left_banner_1 [name] => Left Banner 1 [order] => 2 ) )
Array #2
Array ( [status] => 0 [countries] => [module_status] => 1 [top_banner_1_status] => 1 [top_banner_1_display] => 0 [left_banner_1_status] => 1 [left_banner_1_display] => 0 [left_banner_2_status] => 1 [left_banner_2_display] => 0 [left_banner_3_status] => 1 [left_banner_3_display] => 0 [left_banner_4_status] => [left_banner_4_display] => 0 [left_banner_5_status] => [left_banner_5_display] => 0 [center_banner_1_status] => [center_banner_1_display] => 0 [center_banner_2_status] => [center_banner_2_display] => 0 [right_banner_1_status] => [right_banner_1_display] => 0 [right_banner_2_status] => [right_banner_2_display] => 0 [right_banner_3_status] => [right_banner_3_display] => 0 [right_banner_4_status] => [right_banner_4_display] => 0 [right_banner_5_status] => [right_banner_5_display] => 0 [bottom_banner_1_status] => [bottom_banner_1_display] => 0 )
what i am trying to achive is:
Array ( [0] => Array ( [id] => 1 [position] => top_banner_1 [name] => Top Banner 1 [order] => 1 [top_banner_1_status] => 1 [top_banner_1_display] => 0 ) )
both of these arrays are comming from a database. there are 13 areas in array #1 so everything ive done so far is with foreach loops as array #2 data is fetched from a function that ideally i can't edit.
i've tried quite a few array_*
functions but i'm not getting very far fast.
Upvotes: 1
Views: 68
Reputation: 15230
The structure of Array #2 is not quite apprpriate for this task. If you have the possibility, you should change it to smething like this:
Array
(
[top_banner_1] => Array (
[status] => 1
[display] => 0
)
[left_banner_1] => Array (
[status] => 1
[display] => 0
)
)
//and so on
But if this is not possible, this should work:
foreach($array1 as &$info) {
$status = $info['position'] . '_status';
$display = $info['position'] . '_display';
$info[$status] = $array2[$status];
$info[$display] = $array2[$display];
}
Otherwise, this seems to be a more elegant way:
foreach($array1 as &$info) {
$info['status'] = $array2[$info['position']]['status'];
$info['display'] = $array2[$info['position']]['display'];
}
Upvotes: 0
Reputation: 31498
Assuming the following reasoning:
Array
(
[0] => Array
(
[id] => 1
[position] => top_banner_1
[name] => Top Banner 1
[order] => 1
[top_banner_1_status] => 1 // Added because of key is [position]_status.
[top_banner_1_display] => 0 // Added because of key is [position]_display.
)
)
I would do:
<?php
$array1 = // Array #1 from question.
$array2 = // Array #2 from question.
foreach ($array1 as $key => $item) {
$position = $item['position'];
$keySuffixes = array('_status', '_display');
foreach ($keySuffixes as $suff) {
if (array_key_exists($position . $suff, $array2)) {
$array1[$key][$position . $suff] = $array2[$position . $suff];
}
}
}
?>
Not elegant, I know :(
Upvotes: 2
Reputation: 3721
The trick is to construct a key according to ID to fetch data from $arr2.
foreach($arr1 as $key=>$val){
$id = $val['id'];
$tb_status_key = "top_banner_{$id}_status";
$tb_display_key = "top_banner_{$id}_display";
$arr1[$key][$tb_status_key] = $arr2[$tb_status_key];
$arr1[$key][$tb_display_key] = $arr2[$tb_display_key];
}
Upvotes: 0