-2

欢迎我有一个问题,这可能很简单,但我找不到解决问题的方法。我有几个看起来像这样的数组:

  'contract' => 
  0 => '6'
  1 => '6'
  2 => '6'
 'folder_id' => 
  0 => '1'
  1 => '1'
  2 => '1'
 'service' => 
  0 => '2'
  1 => '2'
  2 => '2'
 'value' => 
  0 => '12'
  1 => '12'
  2 => '66'
 'currency_id' => 
  0 => '6' 
  1 => '9' 
  2 => '6' 

我想操纵它们,这样我就可以得到类似的东西:

'0' =>
'contract' => '6'
'folder_id' => '1'
'service' => '2'
'value' => '12'
'currency_id' => '6'
'1' =>
'contract' => '6'
'folder_id' => '1'
'service' => '2'
'value' => '12'
'currency_id' => '9'
'2' =>
'contract' => '6'
'folder_id' => '1'
'service' => '2'
'value' => '66'
'currency_id' => '6'

试了很多次,都失败了

4

4 回答 4

1
$source=array(/*...*/);
$result=array();
array_walk($source,function($val,$key)use(&$result){
    foreach($val as $k=>$v){
        $result[$k][$key]=$v;
    }
});

在线恶魔

在线演示 2

需要 PHP>=5.3

于 2013-06-25T08:16:12.780 回答
0

我想你可能正在寻找类似的东西。希望这有帮助

if(isset($_POST['folder_id']) && isset($_POST['contract']) && isset($_POST['service']) && isset($_POST['value'])){   
    $folder_id= $_POST['folder_id'];
    $value= $_POST["value"];
    $service= $_POST['service'];
    $contract= $_POST['contract'];

if (!isset($_SESSION["array"]) || count($_SESSION["array"]) < 1) { 
$_SESSION["array"] = array(0 => array("contract" => $contract, "folder_id" => $folder_id, "service" => $service, "value" => $value, "currency_id" => $currency_id));
else{
//do something
}
}
}
?>
于 2013-06-25T07:41:33.697 回答
0

尝试类似:

<?php

$arr = array(
    'contract' => array(
        0 => '6',
        1 => '6',
        2 => '6'
    ),
    'folder_id' => array(
        0 => '1',
        1 => '1',
        2 => '1'
    )
);

$res = array();
foreach($arr as $name => $subarr) {
    foreach($subarr as $id => $value) {
        if (! isset($res[$id])) $res[$id] = array();
        $res[$id][$name] = $value;
    }
}

print_r($res);
于 2013-06-25T07:43:53.007 回答
0

如果这对您有帮助,请尝试运行此代码。我只是假设你有这些价值观。

$contract = array('6','6','6');
$folder_id = array('1','1','1');
$service = array('2','2','2');
$value = array('12','12','66');
$currency_id = array('6','9','6');

$l = count($contract);

$final_array = '';

for($x=0; $x<$l; $x++){

$final_array[$x][contract]=$contract[$x];
$final_array[$x][folder_id]=$folder_id[$x];
$final_array[$x][service]=$service[$x];
$final_array[$x][value]=$value[$x];
$final_array[$x][currency_id]=$currency_id[$x];

}

print_r($final_array);
于 2013-06-25T07:51:10.323 回答