Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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_Loops_For Loop_Multidimensional Array_Merge - Fatal编程技术网

组合不同长度的多维数组的PHP

组合不同长度的多维数组的PHP,php,loops,for-loop,multidimensional-array,merge,Php,Loops,For Loop,Multidimensional Array,Merge,由于每个数组的长度未知,我需要重新排列以下多维数组以合并和填充索引。问题是,如果数组结构错误,我事先不知道需要多少行或列 我首先需要一个数组,其中包含字符串User Id、Study Id、session中每个研究的问题,然后是日期。我不一定关心这个数组,因为它创建起来非常简单 我遇到的问题是,我需要为每个用户提供一个数组,其中的长度与问题数量相同(这有意义吗?)。我最终想要的是创建一个CSV文件,其中标题由用户ID、StudyId、所有问题的组合列表以及日期组成。因此,我可以取下面的“期望输出

由于每个数组的长度未知,我需要重新排列以下多维数组以合并和填充索引。问题是,如果数组结构错误,我事先不知道需要多少行或列

我首先需要一个数组,其中包含字符串User Id、Study Id、session中每个研究的问题,然后是日期。我不一定关心这个数组,因为它创建起来非常简单

我遇到的问题是,我需要为每个用户提供一个数组,其中的长度与问题数量相同(这有意义吗?)。我最终想要的是创建一个CSV文件,其中标题由用户ID、StudyId、所有问题的组合列表以及日期组成。因此,我可以取下面的“期望输出”,简单地说,对于每个数组fcsvput。如果我像现在这样接受用户的预测,当水平排列时,例如CSV文件,它们不会与问题对齐

这里是一个示例数组:

[studies] => Array
(
    [0] => Array
        (
            [Study_ID] => 563
            [Questions] => Array
                (
                    [1] => S563 Q1
                    [2] => S563 Q2
                )
            [User_Predictions] => Array
                (
                    [0] => Array
                        (
                            [User_ID] => 24
                            [Answers] => Array
                                (
                                    [0] => 66
                                    [1] => Option 1
                                )
                        )
                )
        )

    [1] => Array
        (
            [Study_ID] => 510
            [Questions] => Array
                (
                    [1] => S510 Q1
                    [2] => S510 Q2
                    [3] => S510 Q3
                    [4] => S510 Q4
                    [5] => S510 Q5
                )

            [User_Predictions] => Array
                (
                    [0] => Array
                        (
                            [User_ID] => 76
                            [Answers] => Array
                                (
                                    [0] => 1
                                    [1] => 1
                                    [2] => Negative
                                    [3] => 10
                                    [4] => 2,2
                                )
                        )
                    [1] => Array
                        (
                            [User_ID] => 58
                            [Answers] => Array
                                (
                                    [0] => 25
                                    [1] => 20
                                    [2] => Positive
                                    [3] => 5000
                                    [4] => 1,2
                                )
                        )
                )
        )
)
Array
(
    [0] => User_ID
    [1] => Study_ID
    [2] => S563 Q1 
    [3] => S563 Q2 
    [4] => S510 Q1 
    [5] => S510 Q2 
    [6] => S510 Q3 
    [7] => S510 Q4 
    [8] => S510 Q5 
    [9] => Date
)


Array
(
    [0] => 24
    [1] => 563
    [2] => 66
    [3] => Option 1
    [4] => 
    [5] => 
    [6] => 
    [7] => 
    [8] => 
    [9] => Date
)


Array
(
    [0] => 76
    [1] => 510
    [2] => 
    [3] => 
    [4] => 1
    [5] => 1
    [6] => Negative
    [7] => 10
    [8] => 2,2
    [9] => Date
)   

Array
(
    [0] => 58
    [1] => 510
    [2] => 
    [3] => 
    [4] => 25
    [5] => 20
    [6] => Positive
    [7] => 5000
    [8] => 1,2
    [9] => Date
)
这就是我希望实现的目标:

[studies] => Array
(
    [0] => Array
        (
            [Study_ID] => 563
            [Questions] => Array
                (
                    [1] => S563 Q1
                    [2] => S563 Q2
                )
            [User_Predictions] => Array
                (
                    [0] => Array
                        (
                            [User_ID] => 24
                            [Answers] => Array
                                (
                                    [0] => 66
                                    [1] => Option 1
                                )
                        )
                )
        )

    [1] => Array
        (
            [Study_ID] => 510
            [Questions] => Array
                (
                    [1] => S510 Q1
                    [2] => S510 Q2
                    [3] => S510 Q3
                    [4] => S510 Q4
                    [5] => S510 Q5
                )

            [User_Predictions] => Array
                (
                    [0] => Array
                        (
                            [User_ID] => 76
                            [Answers] => Array
                                (
                                    [0] => 1
                                    [1] => 1
                                    [2] => Negative
                                    [3] => 10
                                    [4] => 2,2
                                )
                        )
                    [1] => Array
                        (
                            [User_ID] => 58
                            [Answers] => Array
                                (
                                    [0] => 25
                                    [1] => 20
                                    [2] => Positive
                                    [3] => 5000
                                    [4] => 1,2
                                )
                        )
                )
        )
)
Array
(
    [0] => User_ID
    [1] => Study_ID
    [2] => S563 Q1 
    [3] => S563 Q2 
    [4] => S510 Q1 
    [5] => S510 Q2 
    [6] => S510 Q3 
    [7] => S510 Q4 
    [8] => S510 Q5 
    [9] => Date
)


Array
(
    [0] => 24
    [1] => 563
    [2] => 66
    [3] => Option 1
    [4] => 
    [5] => 
    [6] => 
    [7] => 
    [8] => 
    [9] => Date
)


Array
(
    [0] => 76
    [1] => 510
    [2] => 
    [3] => 
    [4] => 1
    [5] => 1
    [6] => Negative
    [7] => 10
    [8] => 2,2
    [9] => Date
)   

Array
(
    [0] => 58
    [1] => 510
    [2] => 
    [3] => 
    [4] => 25
    [5] => 20
    [6] => Positive
    [7] => 5000
    [8] => 1,2
    [9] => Date
)
注意:不确定从哪里获取日期

首先,这是你提问的模式

  • studies
    中的每个数组都有一个
    Study\u ID
    和一组来自许多用户的问题
    questions
    和用户预测
    user\u Predictions
  • 每个预测都有一个用户
    用户ID
    和对上一步问题的回答
  • 问题索引=答案索引+1
您的问题:您希望每个用户预测都作为一个单独的数组,但它应该包括所有问题列表及其答案(如果在特定研究中出现)

解决方案

$arrReindexed   =   [];
foreach($arrUserStudy as $intKey =>  $userStudy){
    $arrTemp    =   [];
    foreach($arrIndex as $intKey =>  $strIndex){
        $arrTemp[$intKey]   =   array_key_exists($strIndex, $userStudy) ?  $userStudy[$strIndex] : null;
    }

    $arrReindexed[]     =   $arrTemp;
}

print_r($arrReindexed);

Output:
Array
(
    [0] => Array
        (
            [0] => 563
            [1] => 24
            [2] => 66
            [3] => Option 1
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
        )

    [1] => Array
        (
            [0] => 510
            [1] => 76
            [2] => 
            [3] => 
            [4] => 1
            [5] => 1
            [6] => Negative
            [7] => 10
            [8] => 2,2
        )

    [2] => Array
        (
            [0] => 510
            [1] => 58
            [2] => 
            [3] => 
            [4] => 25
            [5] => 20
            [6] => Positive
            [7] => 5000
            [8] => 1,2
        )

)
$output = [array_keys($fields)];
foreach ( $entries as $entry )   {
    // Use array_replace - to create an entry with all $fields values 
    // from the current entry, use array_values to remove keys
    $output[] = array_values(array_replace($fields, $entry));
}
对于预测,通常会有一个
研究ID
和一个对其做出响应的用户
用户ID

// This array will contain all indexes should be present in final array.
$arrIndex   =   ['Study_ID', 'User_ID'];      // Assume these two would always be present, so added in advance.

// This will be temporary array holds data for each user prediction with relevant information i.e. it would not have data for questions which are not in this study.
$arrUserStudy    =   [];

foreach($arr['studies'] as $arrStudy){

    foreach($arrStudy['Questions'] as $strQuestion){
        // To add a new question to the index.
        if(!in_array($strQuestion,  $arrIndex)){
            $arrIndex[] =   $strQuestion;
        }
    }

    foreach($arrStudy['User_Predictions'] as $userStudy){
        $arrTemp  =   [
            'Study_ID'  =>  $arrStudy['Study_ID'],
            'User_ID'   =>  $userStudy['User_ID'],
        ];

        foreach($userStudy['Answers'] as $intKey => $strAnswer){
            $arrTemp[$arrStudy['Questions'][$intKey + 1]]   =   $strAnswer;
        }
        $arrUserStudy[]     =    $arrTemp;
    }
}
输出:

print_r($arrIndex);

Array
(
    [0] => Study_ID
    [1] => User_ID
    [2] => S563 Q1
    [3] => S563 Q2
    [4] => S510 Q1
    [5] => S510 Q2
    [6] => S510 Q3
    [7] => S510 Q4
    [8] => S510 Q5
)


print_r($arrUserStudy);

Array
(
    [0] => Array
        (
            [Study_ID] => 563
            [User_ID] => 24
            [S563 Q1] => 66
            [S563 Q2] => Option 1
        )

    [1] => Array
        (
            [Study_ID] => 510
            [User_ID] => 76
            [S510 Q1] => 1
            [S510 Q2] => 1
            [S510 Q3] => Negative
            [S510 Q4] => 10
            [S510 Q5] => 2,2
        )

    [2] => Array
        (
            [Study_ID] => 510
            [User_ID] => 58
            [S510 Q1] => 25
            [S510 Q2] => 20
            [S510 Q3] => Positive
            [S510 Q4] => 5000
            [S510 Q5] => 1,2
        )

)
如果将此输出与输出进行比较,则只有以下差异:

  • 每个用户谓词都有
    作为问题/Study\u Id/user\u Id,但在您的输出中,它是一个整数
  • 它缺少其他研究的问题
我们可以很容易地包括遗漏的问题,因为我们在
$arrdindex
中有所有这些问题,相应的值将是
null

将字符串键更改为整数键:我更喜欢使用字符串键,因为即使它们没有排序,您也可以使用它们的键名轻松识别和访问它们

到所需输出的最终转换

$arrReindexed   =   [];
foreach($arrUserStudy as $intKey =>  $userStudy){
    $arrTemp    =   [];
    foreach($arrIndex as $intKey =>  $strIndex){
        $arrTemp[$intKey]   =   array_key_exists($strIndex, $userStudy) ?  $userStudy[$strIndex] : null;
    }

    $arrReindexed[]     =   $arrTemp;
}

print_r($arrReindexed);

Output:
Array
(
    [0] => Array
        (
            [0] => 563
            [1] => 24
            [2] => 66
            [3] => Option 1
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
        )

    [1] => Array
        (
            [0] => 510
            [1] => 76
            [2] => 
            [3] => 
            [4] => 1
            [5] => 1
            [6] => Negative
            [7] => 10
            [8] => 2,2
        )

    [2] => Array
        (
            [0] => 510
            [1] => 58
            [2] => 
            [3] => 
            [4] => 25
            [5] => 20
            [6] => Positive
            [7] => 5000
            [8] => 1,2
        )

)
$output = [array_keys($fields)];
foreach ( $entries as $entry )   {
    // Use array_replace - to create an entry with all $fields values 
    // from the current entry, use array_values to remove keys
    $output[] = array_values(array_replace($fields, $entry));
}
保留字符串键并添加缺少的(首选):

注意:在这里,您可以看到密钥没有排序,但您可以在
$arrdindex
上循环,并且可以通过
$arrUserStudy[][]
在任何位置访问密钥

转换为整数键并添加缺少的

$arrReindexed   =   [];
foreach($arrUserStudy as $intKey =>  $userStudy){
    $arrTemp    =   [];
    foreach($arrIndex as $intKey =>  $strIndex){
        $arrTemp[$intKey]   =   array_key_exists($strIndex, $userStudy) ?  $userStudy[$strIndex] : null;
    }

    $arrReindexed[]     =   $arrTemp;
}

print_r($arrReindexed);

Output:
Array
(
    [0] => Array
        (
            [0] => 563
            [1] => 24
            [2] => 66
            [3] => Option 1
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
        )

    [1] => Array
        (
            [0] => 510
            [1] => 76
            [2] => 
            [3] => 
            [4] => 1
            [5] => 1
            [6] => Negative
            [7] => 10
            [8] => 2,2
        )

    [2] => Array
        (
            [0] => 510
            [1] => 58
            [2] => 
            [3] => 
            [4] => 25
            [5] => 20
            [6] => Positive
            [7] => 5000
            [8] => 1,2
        )

)
$output = [array_keys($fields)];
foreach ( $entries as $entry )   {
    // Use array_replace - to create an entry with all $fields values 
    // from the current entry, use array_values to remove keys
    $output[] = array_values(array_replace($fields, $entry));
}

这是一个相对简单的解决方案,可以获得您想要的结果

一些澄清在这篇文章的末尾

这是输入数据的数组:

$studiesData = Array
(
    "0" => Array
    (
        "Study_ID" => "563",
        "Questions" => Array
        (
            "1" => "S563 Q1",
            "2" => "S563 Q2"
        ),
        "User_Predictions" => Array
        (
            "0" => Array
            (
                "User_ID" => "24",
                "Answers" => Array
                (
                    "0" => "66",
                    "1" => "Option 1"
                )
            )
        )
    ),
    "1" => Array
    (
        "Study_ID" => "510",
        "Questions" => Array
        (
            "1" => "S510 Q1",
            "2" => "S510 Q2",
            "3" => "S510 Q3",
            "4" => "S510 Q4",
            "5" => "S510 Q5"
        ),
        "User_Predictions" => Array
        (
            "0" => Array
            (
                "User_ID" => "76",
                "Answers" => Array
                (
                    "0" => "1",
                    "1" => "1",
                    "2" => "Negative",
                    "3" => "10",
                    "4" => "2,2"
                )
            ),
            "1" => Array
            (
                "User_ID" => "58",
                "Answers" => Array
                (
                    "0" => "25",
                    "1" => "20",
                    "2" => "Positive",
                    "3" => "5000",
                    "4" => "1,2"
                )
            )
        )
    )
);
修复数组的索引,以使用有序键获取所有索引:

foreach ($studiesData as $k => &$studyData)
{
    $studyData['Questions'] = array_values($studyData['Questions']);
    foreach ($studyData['User_Predictions'] as $k2 => &$responses)
    {
        $responses['Answers'] = array_values($responses['Answers']);
    }
    unset($responses); // break the reference with the last element
}
unset($studyData); // break the reference with the last element
现在数组
$studiesData
是:

echo "<pre>";
print_r($studiesData);
echo "</pre>";

Array
(
    [0] => Array
        (
            [Study_ID] => 563
            [Questions] => Array
                (
                    [0] => S563 Q1
                    [1] => S563 Q2
                )

            [User_Predictions] => Array
                (
                    [0] => Array
                        (
                            [User_ID] => 24
                            [Answers] => Array
                                (
                                    [0] => 66
                                    [1] => Option 1
                                )
                        )
                )
        )

    [1] => Array
        (
            [Study_ID] => 510
            [Questions] => Array
                (
                    [0] => S510 Q1
                    [1] => S510 Q2
                    [2] => S510 Q3
                    [3] => S510 Q4
                    [4] => S510 Q5
                )

            [User_Predictions] => Array
                (
                    [0] => Array
                        (
                            [User_ID] => 76
                            [Answers] => Array
                                (
                                    [0] => 1
                                    [1] => 1
                                    [2] => Negative
                                    [3] => 10
                                    [4] => 2,2
                                )
                        )

                    [1] => Array
                        (
                            [User_ID] => 58
                            [Answers] => Array
                                (
                                    [0] => 25
                                    [1] => 20
                                    [2] => Positive
                                    [3] => 5000
                                    [4] => 1,2
                                )
                        )
                )
        )
)
一些澄清:

  • 返回数组中的所有值,并对数组进行数字索引

  • 在foreach之后:如文档所述:

$value和最后一个数组元素的引用即使在 foreach循环。建议通过unset()将其销毁

  • 在数组中搜索给定的值,如果成功,则返回第一个对应的键
==检查
false
,因为此函数可能返回布尔值false,但也可能返回计算结果为false的非布尔值


日期
:在问题的最终输出中有
日期
条目,但在数组输入中没有。如果你更新了你的问题,我会把它包括在我的答案中。

我已经试着减少这一点,所以有尽可能少的步骤

这将其分为两个阶段,收集数据,然后格式化输出。我在代码中添加了注释,因为它有助于解释代码上下文中的内容

Array
(
    [0] => Array
        (
            [0] => User_ID
            [1] => Study_ID
            [2] => S563 Q1
            [3] => S563 Q2
            [4] => S510 Q1
            [5] => S510 Q2
            [6] => S510 Q3
            [7] => S510 Q4
            [8] => S510 Q5
        )

    [1] => Array
        (
            [0] => 24
            [1] => 563
            [2] => 66
            [3] => Option 1
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
        )
....
要显示到目前为止的一些数据

最后格式化输出


你可以阅读我实现的广义多维数组的代码,这可能会帮助你弄清楚:为什么这个被否决了?@Jeffreykaborowski“为什么这个被否决了?”。。。。因为你的问题不清楚(至少对我来说)。我不明白你想要什么样的结果?什么是
User\u ID
Study\u ID
?常数?串?我在输入数据中没有看到它们。所需阵列之间的差异是什么?如果你能弄清楚这个问题,或者解释一下这些数字是什么,你就能得到更好的结果。我没有否决这篇帖子BTWThank you@accountary我编辑了样本数组,希望能澄清这些问题。样本数组和所需输出之间的区别在于,所需输出是答案与缺失索引的空值的复合(如果有意义的话)م不会进行向下投票,但这看起来不可理解(一点也不)。您应该清楚地解释如何从输入生成输出。通过代码注释或任何其他可理解的描述。