[英]php Multidimensional array question
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. 我的意思是,如果我在第一个数组中有100个数组,而在第二个数组中有3个元素,那么结果数组中应该有300个。
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. 意思是,它只是在第一个数组的每个元素的末尾添加menu_id,而不是相乘。 Any idea, I'm surely missing something. 任何想法,我肯定会丢失一些东西。
Thanks guys. 多谢你们。
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;
}
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 这也经过测试, http://www.ideone.com/Of126
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... 不确定您返回的是什么$arr
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.