I am trying to combine two arrays with their keys and values, and I have the following PHP code:
我试图将两个数组与它们的键和值组合在一起,我有以下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();
and its out put is:
它的出局是:
Array
(
[12] => id
[15] => id
[13] => id
[16] => id
)
Array
(
[0] => ruleid
[1] => ruleid
[2] => ruleid
[3] => ruleid
)
I want it like this:
我想这样:
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
)
)
I tried it like in the commented code but it is displaying an error. What am I doing wrong?
我在评论代码中尝试过,但它显示错误。我究竟做错了什么?
5 个解决方案
#1
3
use foreach
to merge as your expected result
使用foreach合并为您的预期结果
update 1 : simplified version of your code
更新1:代码的简化版本
if(count($_POST['id'])==count($_POST['hruleid']))
{
foreach ($_POST['id'] as $key => $value)
{
$new_array[] = array('id'=>$value,'rule_id'=>$_POST['hruleid'][$key]);
}
}
old update:
旧更新:
<?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);
?>
note : above code only work if both array have same key and same count of array
注意:上述代码仅在两个数组具有相同的键和相同的数组计数时才起作用
#2
2
hey please check this solution. This is the proper solution there are two array one is data and other is final data. final data is stored the value of data array. This logic is used in the webservices.
嘿,请检查这个解决方案。这是正确的解决方案,有两个数组,一个是数据,另一个是最终数据。最终数据存储数据数组的值。此逻辑用于Web服务。
<?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>';
?>
#3
1
Just Try this
试试吧
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]:''];
}
If you array is like
如果你的数组是这样的
$id_array=[11,21,13,14];
$rule_array=[2,3,5,6];
The above code will return value like
上面的代码将返回值
#4
1
Change your php script with this one, no need of array_fill()
用这个更改你的php脚本,不需要array_fill()
$id_array=[];
$rule_array=[];
foreach ($_POST['id'] as $key => $value) {
$id_array[] = $value;//it will be $id_array=[11,21,13,14];
}
foreach ($_POST['hruleid'] as $rkey => $rvalue) {
$rule_array[] = $rvalue;////it will be $rule_array=[2,3,5,6];
}
$arr=[];
for($i=0;$i<4;$i++)
{
$temp=[];
$temp['id']=$id_array[$i];
$temp['ruleid']=$rule_array[$i];
array_push($arr,$temp);
}
echo "<pre>";print_r($arr);
A working fiddle is here php fiddle
一个工作小提琴在这里PHP小提琴
#5
0
Script
脚本
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)
)
);
?>
Output
产量
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
)
)
#1
3
use foreach
to merge as your expected result
使用foreach合并为您的预期结果
update 1 : simplified version of your code
更新1:代码的简化版本
if(count($_POST['id'])==count($_POST['hruleid']))
{
foreach ($_POST['id'] as $key => $value)
{
$new_array[] = array('id'=>$value,'rule_id'=>$_POST['hruleid'][$key]);
}
}
old update:
旧更新:
<?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);
?>
note : above code only work if both array have same key and same count of array
注意:上述代码仅在两个数组具有相同的键和相同的数组计数时才起作用
#2
2
hey please check this solution. This is the proper solution there are two array one is data and other is final data. final data is stored the value of data array. This logic is used in the webservices.
嘿,请检查这个解决方案。这是正确的解决方案,有两个数组,一个是数据,另一个是最终数据。最终数据存储数据数组的值。此逻辑用于Web服务。
<?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>';
?>
#3
1
Just Try this
试试吧
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]:''];
}
If you array is like
如果你的数组是这样的
$id_array=[11,21,13,14];
$rule_array=[2,3,5,6];
The above code will return value like
上面的代码将返回值
#4
1
Change your php script with this one, no need of array_fill()
用这个更改你的php脚本,不需要array_fill()
$id_array=[];
$rule_array=[];
foreach ($_POST['id'] as $key => $value) {
$id_array[] = $value;//it will be $id_array=[11,21,13,14];
}
foreach ($_POST['hruleid'] as $rkey => $rvalue) {
$rule_array[] = $rvalue;////it will be $rule_array=[2,3,5,6];
}
$arr=[];
for($i=0;$i<4;$i++)
{
$temp=[];
$temp['id']=$id_array[$i];
$temp['ruleid']=$rule_array[$i];
array_push($arr,$temp);
}
echo "<pre>";print_r($arr);
A working fiddle is here php fiddle
一个工作小提琴在这里PHP小提琴
#5
0
Script
脚本
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)
)
);
?>
Output
产量
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
)
)