你好任何人都可以帮助我,我有2个参数数组比我想要匹配,如果索引数组1&&索引数组2,值索引数组2推送到索引数组1 你可以给我一些提示或技巧吗?
Array
(
[0] => 4151
[1] => 4952
)
Array
(
[0] => Array
(
[0] => BandaAceh
[1] => Padang
[2] => Jambi
[3] => Palangkaraya
)
[1] => Array
(
[0] => BandaAceh
[1] => Jambi
[2] => Padang
[3] => Palangkaraya
)
)
比我希望可以使用的那样:
Array
(
[0] => Array
(
[0] => BandaAceh
[1] => Padang
[2] => Jambi
[3] => Palangkaraya
[4] => 4151
)
[1] => Array
(
[0] => BandaAceh
[1] => Jambi
[2] => Padang
[3] => Palangkaraya
[4] => 4952
)
)
我的代码到目前为止
public function logRute($ruteoptimal , $simpanrute , $i)
{
echo "<pre>".print_r($ruteoptimal,true)."</pre>";
echo "<pre>".print_r($simpanrute,true)."</pre>";
$new=array();
foreach ($ruteoptimal as $ruteoptimalkey => $ruteoptimalvalue)
{
foreach ($ruteoptimalvalue as $num_key => $content)
{
$$ruteoptimal[$ruteoptimalkey][$num_key] = 'another string';
}
}
rute optimal is
Array
(
[0] => 4151
[1] => 4952
)
simpan rute是
Array
(
[0] => Array
(
[0] => BandaAceh
[1] => Padang
[2] => Jambi
[3] => Palangkaraya
)
[1] => Array
(
[0] => BandaAceh
[1] => Jambi
[2] => Padang
[3] => Palangkaraya
)
)
答案 0 :(得分:0)
您可以使用array_map http://php.net/manual/en/function.array-map.php。
$data = array(
4151,
4952
);
$array = array(
array(
"Banda",
"Padang",
"Jambi",
"Palan"
),
array(
"Banda",
"Jambi",
"Padang",
"Pala"
),
);
function combine ($array, $data) {
$array[] = $data;
return $array;
}
$new = array_map('combine', $array, $data);
var_dump($new);
这应该产生
array(2) {
[0] =>
array(5) {
[0] =>
string(5) "Banda"
[1] =>
string(6) "Padang"
[2] =>
string(5) "Jambi"
[3] =>
string(5) "Palan"
[4] =>
int(4151)
}
[1] =>
array(5) {
[0] =>
string(5) "Banda"
[1] =>
string(5) "Jambi"
[2] =>
string(6) "Padang"
[3] =>
string(4) "Pala"
[4] =>
int(4952)
}
}
答案 1 :(得分:0)
如果你想使用“foreach”和“array_push”而不是array_map:
$data = array(
4151,
4952
);
$array = array(
array(
"Banda",
"Padang",
"Jambi",
"Palan"
),
array(
"Banda",
"Jambi",
"Padang",
"Pala"
),
);
foreach( $data as $data_key => $data_value )
{
foreach( $array as $array_key => $array_value )
{
if ( $data_key == $array_key )
{
array_push( $array[ $array_key ] , $data_value );
break;
}
}
}
var_dump( $array );
答案 2 :(得分:0)
简单:
$arr2
现在应该具有您想要的值<强> PHP:强>
$arr1 = array(4151, 4952);
$arr2 = array(
array("BandaAce", "Panda", "Jambi", "Pala"),
array("BandaAce", "Panda", "Jambi", "Pala"),
);
foreach ($arr1 as $k => $v) {
if (isset($arr2[$k])) {
$arr2[$k][] = $v;
}
}