So I have an array like the following:

Array
(
    [0] => Array
        (
            [user_id] => 684
            [sec_id] => 2
            [rank_id] => 1
            [rank] => usr

        )

    [1] => Array
        (
            [user_id] => 693
            [sec_id] => 3
            [rank_id] => 5
            [rank] => usr

        )
)

And I have another array like this

Array
(
    [0] => 2
    [1] => 7
    [2] => 27
)

I want the value of the second array to be added at the end of each arrays of the 1st array, and it should be multiplied. I mean, if I have 100 arrays in the first array, and 3 elements in the second array, I should have 300 in the resulting array.

Taking example of the above, I would like to have something as follows:

user_id | sec_id | rank_id | rank | menu_id
684 |        2 |       1 |    usr |    2
684 |        2 |       1 |    usr |    7
684 |        2 |       1 |    usr |   27
693 |        3 |       5 |    usr |    2
693 |        3 |       5 |    usr |    7
693 |        3 |       5 |    usr |   27

I tried with the following function, but it's not working.

function getR($arr_one,$arr_two) {
    foreach ($arr_one as $k=>&$v) {
        foreach ($arr_two as $x=>&$y) { $v['menu_id'] = $y;  }
    }
    return $arr_one; 
}

This is just making an array like this:

user_id | sec_id | rank_id | rank | menu_id
684 |        2 |       1 |    usr |   27
693 |        3 |       5 |    usr |   27

Means, it's just adding menu_id at the end of each element of the first array, but not multiplying. Any idea, I'm surely missing something.

Thanks guys.

share|improve this question

feedback

3 Answers

up vote 2 down vote accepted
function getR($arr_one,$arr_two) {
    $new_arr = array();
    foreach ($arr_one as $k=>$v) {
        foreach ($arr_two as $x=>$y) {
            $this_item = $v;
            $this_item['menu_id'] = $y;
            $new_arr[] = $this_item;
        }
    }
    return $new_arr; 
}
share|improve this answer
Thanks, this is working perfectly ... I just had to add $new_arr[] = $v; after initiating the $new_arr at the top. Thanks Spudley. – Jeremy Roy Feb 18 '11 at 20:36
feedback

I'm not going to ask... but try this:

<?php 

function crazy ($arr1,$arr2) {
    foreach ($arr1 as $key=>$value) {
        foreach ($arr2 as $value2) {
        $nvalue=$value;
        $nvalue[]=$value2;
        $new[]=$nvalue;
        }
    }
return $new;
}

$arr1=array(array('user'=>1,'dude'=>2),array('user'=>2,'dude'=>3));
$arr2=array(2,7,27);

print_r(crazy($arr1,$arr2));

this is tested too, http://www.ideone.com/Of126

share|improve this answer
Oops, I just tried with Spudley's solution and it worked, but yours is fine too. Thanks for the help. – Jeremy Roy Feb 18 '11 at 20:36
feedback

Without testing (eek!) I imagine something like this:

function getR( $arr_one, $arr_two )
{
    $second_key = 0;
    foreach ( $arr_one as $k => &$v )
    {
        $v['menu_id'] = $second_key++;
        if ( 3 == $second_key ) $second_key = 0;
    }
    return $arr; 
}

Presumably, you're passing the first array by reference? Not sure what $arr is that you're returning though...

share|improve this answer
edited, sorry for the typo, I'm returning $arr_one. – Jeremy Roy Feb 18 '11 at 20:29
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.