1

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.

3
  • There is no built-in function for this, you have to write your own Commented Nov 11, 2011 at 11:44
  • thats the bit im struggling with as i have never had to join 2 arrays like this before Commented Nov 11, 2011 at 11:48
  • Well, you have shown us input and desired output, no code that you have been struggling with. Commented Nov 11, 2011 at 11:55

3 Answers 3

2

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 :(

Sign up to request clarification or add additional context in comments.

1 Comment

its elegant enough but works perfectly its far far more readable than what i was comming up with. Thanks
0

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

Comments

0

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

Comments

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.