i have two arrays and i need to extract the values of the 2nd array depending on the value of $arr[0]["num"]
我有两个数组,我需要提取第二个数组的值取决于$ arr [0] [“num”]的值
$arr = array(
0 => array(
"id" => 24,
"num" => 2
),
1 => array(
"id" => 25,
"num" => 5
)
2 => array(
"id" => 26,
"num" => 3
)
);
$array = array('1','2','3','4','5','6','7','8','9','10');
$new = array();
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[$i];
}
}
is it possible to remove the values of the 2nd array and transfer it into a new array?
是否可以删除第二个数组的值并将其传输到一个新数组?
what my loop does is just copying the values from the start after each loop. i want to remove the copied values from the 2nd array.
我的循环所做的只是在每次循环后从开始复制值。我想从第二个数组中删除复制的值。
The output should be like this:
输出应该是这样的:
Array
(
[24] => Array
(
[0] => 1
[1] => 2
)
[25] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
[26] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)
2 个解决方案
#1
1
I'd suggest using array_shift
我建议使用array_shift
$arr = array(
array(
"id" => 24,
"num" => 2
),
array(
"id" => 25,
"num" => 5
),
array(
"id" => 26,
"num" => 3
)
);
$array = array('1','2','3','4','5','6','7','8','9','10');
$new = array();
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[0]; // *1
array_shift($array);
}
}
echo '<pre>';
print_r($new);
*1 You have to change this line as well. Since array_shift removes the first array entry, each iteration should access array[0].
* 1您还必须更改此行。由于array_shift删除了第一个数组条目,因此每次迭代都应该访问array [0]。
Output:
Array
(
[24] => Array
(
[0] => 1
[1] => 2
)
[25] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
[26] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)
#2
1
Try this
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[$i];
// unset previous values, in first iteration it will remove 0, 1
unset($array[$i]);
}
// reset the array keys, so for loop $i will start from 0
$array = array_values($array);
}
Output:
array (size=3)
24 =>
array (size=2)
0 => string '1' (length=1)
1 => string '2' (length=1)
25 =>
array (size=5)
0 => string '3' (length=1)
1 => string '4' (length=1)
2 => string '5' (length=1)
3 => string '6' (length=1)
4 => string '7' (length=1)
26 =>
array (size=3)
0 => string '8' (length=1)
1 => string '9' (length=1)
2 => string '10' (length=2)
#1
1
I'd suggest using array_shift
我建议使用array_shift
$arr = array(
array(
"id" => 24,
"num" => 2
),
array(
"id" => 25,
"num" => 5
),
array(
"id" => 26,
"num" => 3
)
);
$array = array('1','2','3','4','5','6','7','8','9','10');
$new = array();
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[0]; // *1
array_shift($array);
}
}
echo '<pre>';
print_r($new);
*1 You have to change this line as well. Since array_shift removes the first array entry, each iteration should access array[0].
* 1您还必须更改此行。由于array_shift删除了第一个数组条目,因此每次迭代都应该访问array [0]。
Output:
Array
(
[24] => Array
(
[0] => 1
[1] => 2
)
[25] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
[26] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)
#2
1
Try this
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[$i];
// unset previous values, in first iteration it will remove 0, 1
unset($array[$i]);
}
// reset the array keys, so for loop $i will start from 0
$array = array_values($array);
}
Output:
array (size=3)
24 =>
array (size=2)
0 => string '1' (length=1)
1 => string '2' (length=1)
25 =>
array (size=5)
0 => string '3' (length=1)
1 => string '4' (length=1)
2 => string '5' (length=1)
3 => string '6' (length=1)
4 => string '7' (length=1)
26 =>
array (size=3)
0 => string '8' (length=1)
1 => string '9' (length=1)
2 => string '10' (length=2)