Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 操纵多维阵列_Php_Arrays_Multidimensional Array - Fatal编程技术网

Php 操纵多维阵列

Php 操纵多维阵列,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,欢迎我有一个问题,这可能很简单,但我找不到一个孤独的问题。我有两个阵列,看起来像: 'contract' => 0 => '6' 1 => '6' 2 => '6' 'folder_id' => 0 => '1' 1 => '1' 2 => '1' 'service' => 0 => '2' 1 => '2' 2 => '2' 'value' => 0 =>

欢迎我有一个问题,这可能很简单,但我找不到一个孤独的问题。我有两个阵列,看起来像:

  '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'
<?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);

尝试了很多次,但都失败了

我想你可能在寻找类似的东西。希望这有帮助
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
}
}
}
?>
if(isset($\u POST['folder\u id'])和isset($\u POST['contract'])和isset($\u POST['service'])和isset($\u POST['value']){
$folder\u id=$\u POST['folder\u id'];
$value=$_POST[“value”];
$service=$_POST['service'];
$contract=$_POST['contract'];
如果(!isset($_会话[“数组])| |计数($_会话[“数组]))<1{
$\会话[“数组”]=数组(0=>数组(“合同”=>$contract,“文件夹id”=>$folder\u id,“服务”=>$service,“值”=>$value,“货币id”=>$currency\u id));
否则{
//做点什么
}
}
}
?>
尝试以下方法:

'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'
<?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);

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

$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];


需要PHP>=5.3

两件事:首先,您需要的不是有效的PHP。我认为您需要为每组联系人、文件夹id等设置第二个数组,但这不是您的代码片段所显示的。第二,你提到你已经尝试了很多次,但是你尝试了什么?