PHP使用CSV文件中的索引创建嵌套数组

PHP使用CSV文件中的索引创建嵌套数组,php,arrays,loops,multidimensional-array,associative-array,Php,Arrays,Loops,Multidimensional Array,Associative Array,我有一个特别的问题。我有一个CSV文件,其中包含逗号分隔的值,我需要上传这些值,然后根据以下3个条件获取嵌套的值数组 数组将首先遍历所有值,并获得前4个唯一值 人物。(代码) 将每列值与的数量匹配 列,并给出与4位数字匹配的行数 代码。(单列和双列的单_设备,各列计数的三重和四重设备) 将每列值与代码匹配,并列出所有 设备下的列。(数字) CSV文件 123429000000000 123429000000001 123429000000010,123429000000011 123429000

我有一个特别的问题。我有一个CSV文件,其中包含逗号分隔的值,我需要上传这些值,然后根据以下3个条件获取嵌套的值数组

  • 数组将首先遍历所有值,并获得前4个唯一值 人物。(代码)
  • 将每列值与的数量匹配 列,并给出与4位数字匹配的行数 代码。(单列和双列的单_设备,各列计数的三重和四重设备)
  • 将每列值与代码匹配,并列出所有 设备下的列。(数字)
  • CSV文件

    123429000000000
    123429000000001
    123429000000010,123429000000011
    123429000000040,123429000000041
    
    我所渴望的是

    Array
    (
        [Code] => 1234
            (
                [single_devices] => 2
                    (
                        [0] => Array
                            (
                                [0] => 123429000000000
                            )
                        [1] => Array
                            (
                                [0] => 123429000000001
                            )
                    )
                [dual_devices] => 2
                    (
                        [0] => Array
                            (
                                [0] => 123429000000010
                                [1] => 123429000000011
                            )
                        [1] => Array
                            (
                                [0] => 123429000000040
                                [1] => 123429000000041
                            )
                    )
            )
    
    )
    
    可能吗? 我可以管理JSON转换的数据或对象,也可以只管理关联嵌套数组

    编辑:这是我为之编写的代码,它只显示了我想要的值,而不是索引

    // Get all numbers in array
    for ($j = 0; $j < count($csv_file[$i]); $j++){
        $numbers[] = $csv_file[$i][$j];
    }
    
    // Get codes from numbers
    for ($i = 0; $i < count($csv_file); $i++){
        for ($j = 0; $j < count($csv_file[$i]); $j++){
            $codes[] = substr($csv_file[$i][$j], 0, 4);
        }
    }
    
    // Get unique codes from codes array
    $codes = array_unique($codes);
    
    // Get numbers and sort them codes and device count wise.
    for ($i = 0; $i < count($csv_file); $i++){
        for ($j = 0; $j < count($csv_file[$i]); $j++){
            $q = count($csv_file[$i]); // set device count based on column count
            if (count($csv_file[$i]) == $q){ // if device count is equal to column count
                foreach ($codes as $code){ // loop through unique codes
                    if ($code == substr($csv_file[$i][$j], 0, 4)){ // if number's first 4 char matches code
                        // create array with code and then device count and add numbers
                        $devices[$code][$q.'_device_numbers'][$i][$j] = preg_replace('/\s+/', '', $csv_file[$i][$j]);
                    }
                }
            }
        }
    }
    

    这是基于将文件作为csv读取(使用
    fgetcsv()
    )并提取每行第一个值的前4位数字。然后,它使用另一个数组为
    'single_devices'
    等提供键-使用行上元素数的计数(-1,因为数组基于0)

    根据测试数据得出

    Array
    (
        [1234] => Array
            (
                [single_devices] => Array
                    (
                        [0] => Array
                            (
                                [0] => 123429000000000
                            )
    
                        [1] => Array
                            (
                                [0] => 123429000000001
                            )
    
                    )
    
                [dual_devices] => Array
                    (
                        [0] => Array
                            (
                                [0] => 123429000000010
                                [1] => 123429000000011
                            )
    
                        [1] => Array
                            (
                                [0] => 123429000000040
                                [1] => 123429000000041
                    )
    
            )
    
    )
    

    你可以得到一个输出

    Array
    (
        [1234] => Array
            (
                [code] => 1234
                [single_devices] => Array
                    (
                        [count] => 2
                        [0] => Array
                            (
                                [0] => 123429000000000
                            )
    
                        [1] => Array
                            (
                                [0] => 123429000000001
                            )
    
                    )
    

    可能吗是的,所以请自己尝试一下,并将代码添加到您的问题中。请不要只是要求我们为您解决问题。向我们展示你是如何试图自己解决问题的,然后向我们展示结果是什么,并告诉我们为什么你觉得它不起作用。给我们一个明确的解释,什么是不工作的,并提供。读一个好问题。一定要阅读。你需要逐行阅读,用explode放入一个数组,计算索引数,然后用你的逻辑放入一个数组。祝你好运当你有一个真正的代码时,我们可以帮助你。是否有可能在同一行中有多个项目,但这些项目有不同的起始4号?是的@NigelRen我编辑了我的问题。这是正确的。这就是我目前已经拥有的代码。但这不是我计划的理想阵列。如果你是说想要
    [code]=>1234(
    [single\u devices]=>2(
    ,这不是有效的数组格式。你可以为它们添加额外的字段。@Xia这就是我最后一条评论的意思。你说你想要的结果是不可能的。这就是为什么我要求你显示我们可以生成的结果,或者你是如何得出你想要的结果的。@NigelRen是的,这有点像我是aiming for.好的,所以它可能不是有效的数组格式,但正如我提到的,有可能以任何其他方式获得这样的输出吗?@PatrickQ是的,我编辑了我的答案并添加了我得到的输出。
    Array
    (
        [1234] => Array
            (
                [single_devices] => Array
                    (
                        [0] => Array
                            (
                                [0] => 123429000000000
                            )
    
                        [1] => Array
                            (
                                [0] => 123429000000001
                            )
    
                    )
    
                [dual_devices] => Array
                    (
                        [0] => Array
                            (
                                [0] => 123429000000010
                                [1] => 123429000000011
                            )
    
                        [1] => Array
                            (
                                [0] => 123429000000040
                                [1] => 123429000000041
                    )
    
            )
    
    )
    
    while ( ($data = fgetcsv($fh)) !== false )  {
        $code = substr($data[0], 0, 4);
        if ( !isset($output[$code]))    {
            $output[$code] = ["code" => $code];
        }
        $deviceLabel = $baseData[count($data)-1];
        $output[$code][$deviceLabel]['count'] =
            ($output[$code][$deviceLabel]['count'] ?? 0) + 1;
        $output[$code][$deviceLabel][] = $data;
    }
    
    Array
    (
        [1234] => Array
            (
                [code] => 1234
                [single_devices] => Array
                    (
                        [count] => 2
                        [0] => Array
                            (
                                [0] => 123429000000000
                            )
    
                        [1] => Array
                            (
                                [0] => 123429000000001
                            )
    
                    )