我正在尝试将两个数组与它们的键和值组合在一起,我有以下PHP代码:
foreach ($_POST['id'] as $key => $value) {
$id_array[] = $value;
//$array1[] = $update_array;
}
$id_array = array_fill_keys($id_array, 'id');
// var_dump($array1);
// exit();
foreach ($_POST['hruleid'] as $rkey => $rvalue) {
$rule_array[] = $rvalue;
}
$rule_array = array_fill_keys($rule_array, 'ruleid');
// $result = array();
// foreach ($id_array as $i => $val) {
// $result[] = array($val, $rule_array[$i]);
// }
echo "<pre>";
print_r($id_array);
echo "<pre>";
print_r($rule_array);
exit();
及其出局是:
Array
(
[12] => id
[15] => id
[13] => id
[16] => id
)
Array
(
[0] => ruleid
[1] => ruleid
[2] => ruleid
[3] => ruleid
)
我想这样:
Array
(
[0] => Array
(
[id] => 12
[ruleid] => 0
)
[1] => Array
(
[id] => 15
[ruleid] => 1
)
[2] => Array
(
[id] => 13
[ruleid] => 2
)
[3] => Array
(
[id] => 16
[ruleid] => 3
)
)
我在注释代码中尝试过,但它显示错误。我做错了什么?
答案 0 :(得分:3)
使用foreach
合并为预期结果
更新1 :代码的简化版
if(count($_POST['id'])==count($_POST['hruleid']))
{
foreach ($_POST['id'] as $key => $value)
{
$new_array[] = array('id'=>$value,'rule_id'=>$_POST['hruleid'][$key]);
}
}
旧更新:
<?php
$id_array = Array(12,15,13,16);
$rule_array = Array (0,1,2,3);
foreach($id_array as $key=>$row)
{
$new_array[]= array('id'=>$row,'rule_id'=>$rule_array[$key]);
}
print_r($new_array);
?>
注意:上述代码仅在两个数组具有相同的键和相同的数组计数时才有效
答案 1 :(得分:2)
<?php
$data = array();
$final_data = array();
extract($_POST);
for ($i=0; $i <sizeof($id) ; $i++) {
$data['id'] = $id[$i];
$data['ruleid'] = $hruleid[$i];
$final_data[] = $data;
}
echo '<pre>';
print_r($final_data);
echo '</pre>';
?>
答案 2 :(得分:1)
试试这个
foreach ($_POST['id'] as $key => $value) {
$id_array[] = $value;
}
foreach ($_POST['hruleid'] as $rkey => $rvalue) {
$rule_array[] = $rvalue;
}
$final=[];
foreach ($id_array as $key=>$id){
$final[]=['id'=>$id,'ruleid'=>(isset($rule_array[$key]))?$rule_array[$key]:''];
}
如果你的数组是
$id_array=[11,21,13,14];
$rule_array=[2,3,5,6];
上面的代码将返回值
答案 3 :(得分:1)
使用此脚本更改您的php脚本,不需要 #include <iostream>
using namespace std;
int main() {
int n, k = 0, x, middle, ok, i, up;
cout << "n=";
cin >> n;
x = n;
while (x) {
k++;
x /= 10;
}
if (k % 2 == 0) middle = k / 2;
else middle = k / 2 + 1;
ok = 1;
up = n % 10;
for (i = 0; i <= up && ok != 1; i++) {
n = n / 10;
if (up > n % 10) ok = 0;
}
if (ok == 1) {
up = n % 10;
for (i = k - up; i < 0 && ok == 1; i--) {
n = n / 10;
if (up < n % 10)
ok = 0;
}
if (ok == 1)
cout << "Evrika! Your number has a mountain appearance";
else
cout << "No mountain appearance";
} else
cout << "No mountain appearance";
}
array_fill()
这里有一个工作小提琴php fiddle
答案 4 :(得分:0)
<强>脚本强>
akshay@db-3325:/tmp$ cat test.php
<?php
$arr1=array(
12 => 'id',
15 => 'id',
13 => 'id',
16 => 'id',
);
$arr2 =array(
0 => 'ruleid',
1 => 'ruleid',
2 => 'ruleid',
3 => 'ruleid',
);
print_r(
array_map(
function($a,$b){ return array('id'=>$a,'ruleid'=>$b); },
array_keys($arr1),
array_keys($arr2)
)
);
?>
<强>输出强>
akshay@db-3325:/tmp$ php test.php
Array
(
[0] => Array
(
[id] => 12
[ruleid] => 0
)
[1] => Array
(
[id] => 15
[ruleid] => 1
)
[2] => Array
(
[id] => 13
[ruleid] => 2
)
[3] => Array
(
[id] => 16
[ruleid] => 3
)
)