二维数组到多维数组

时间:2019-05-16 23:18:36

标签: php

这就是我所拥有的:

[courses] => array
(
  array([id]=>1,[name]=>"course_1",[age]=>16,[location]=>"Berlin"),
  array([id]=>2,[name]=>"course_1",[age]=>18,[location]=>"Berlin"),
  array([id]=>3,[name]=>"course_1",[age]=>20,[location]=>"Berlin"),
  array([id]=>4,[name]=>"course_1",[age]=>16,[location]=>"London"),
  array([id]=>5,[name]=>"course_1",[age]=>18,[location]=>"Rome"),
  array([id]=>6,[name]=>"course_2",[age]=>16,[location]=>"Berlin")

);

我需要将其转换为如下所示的多维数组:
 -每个名字可以有多个年龄段
 -每个年龄段可以有多个地点
 -不需要ID

因此,新的转换数组应该具有具有多个年龄且具有多个位置的课程。

我首先尝试为每个键创建一个数组:

// all courses, sorted, once
$x = 0;
$course_names = array();
foreach ($courses as $item) {
    $course_names[$x] = $item['name'];
    $x++;
}

$course_names = array_unique($course_names);
sort($course_names);

// all ages, sorted, once
$x = 0;
$ages = array();
foreach ($courses as $item) {
    $ages[$x] = $item['age'];
    $x++;
}

$ages = array_unique($ages);
sort($ages);

// all locations, sorted, once
$x = 0;
$locations = array();
foreach ($courses as $item) {
    $locations[$x] = $item['location'];
    $x++;
}

$location = array_unique($location);
sort($location);

然后我尝试根据请求创建另一个数组(别对我发誓,这是许多尝试之一):

$counter = 0;
foreach ($location as $loc_key=>$loc_val) {
    foreach ($ages as $age_key=>$age_val) {
        foreach ($course_names as $cou_key=>$cou_val) {
            foreach ($courses as $course) {
                if ($course['name']==$cou_val) {
                    if ($course['age']==$age_val) {
                        if ($course['location']==$loc_val) {
                            $final_json[$counter]['ages']['locations'][] = $loc_val;
                        }
                        $final_json[$counter]['ages'][] = $age_val;
                    }
                    $final_json[$counter]['id'] = $course['id'];
                    $final_json[$counter]['name'] = $cou_val;
                    $counter++;
                }
            }
        }
    }
}

结果数组应该是这样的:

[courses] => array
(
  [0] => array
      (
        [name] => "course_1",
        [ages] => array
               (
                 [0] => array
                     (
                        [age] => 16,
                        [locations] => array ([0]=>"Berlin",[1]=>"London")
                      ),
                 [1] => array
                     (
                        [age] => 18,
                        [locations] => array ([0]=>"Berlin",[1]=>"Rome")
                      ),
                 [2] => array
                     (
                        [age] => 20,
                        [locations] => array ([0]=>"Berlin")
                      )

                )
       )
  [1] => array
      (
        [name] => "course_2",
        [ages] => array
               (
                 [0] => array
                     (
                        [age] => 16,
                        [locations] => array ([0]=>"Berlin")
                      )

                )
       )
);

我为此奋斗了一段时间,我感到受阻,任何想法都值得欢迎。

3 个答案:

答案 0 :(得分:0)

$courses = array( 
    array("id"=>1,"name"=>"course_1","age"=>16,"location"=>"Berlin"),
    array("id"=>2,"name"=>"course_1","age"=>18,"location"=>"Berlin"),
    array("id"=>3,"name"=>"course_1","age"=>20,"location"=>"Berlin"),
    array("id"=>4,"name"=>"course_1","age"=>16,"location"=>"London"),
    array("id"=>5,"name"=>"course_1","age"=>18,"location"=>"Rome"), 
    array("id"=>6,"name"=>"course_2","age"=>16,"location"=>"Berlin"),
);

foreach ($courses as $item) {
    $course_age[]      = $item['age'];
    $course_names[]    = $item['name'];
    $course_location[] = $item['location'];
}
$course_age      = array_unique($course_age);
$course_names    = array_unique($course_names);
$course_location = array_unique($course_location);
$finalArray = array();
$finalArrayKey=0;
foreach ($course_names as $key => $courseValue) {
    foreach ($courses as $key => $actualValue) {
        if($actualValue['name']==$courseValue){
            $finalArray[$finalArrayKey]['name'] = $courseValue;
            $finalArray[$finalArrayKey]['ages']  = array();
            $finalArray[$finalArrayKey]['ages']['location'] = array();
        }
    }
    foreach ($courses as $key => $actualValue) {
        if($actualValue['name']==$courseValue){
            if(!in_array($actualValue['age'], $finalArray[$finalArrayKey]['ages']))
                $finalArray[$finalArrayKey]['ages'][]  = $actualValue['age'];
            if(!in_array($actualValue['location'], $finalArray[$finalArrayKey]['ages']['location']))
                $finalArray[$finalArrayKey]['ages']['location'][]  = $actualValue['location'];

        }
    }
    ++$finalArrayKey;
}
echo "<pre>";
print_r($finalArray);

样本输出:

Array
(
    [0] => Array
        (
            [name] => course_1
            [ages] => Array
                (
                    [location] => Array
                        (
                            [0] => Berlin
                            [1] => London
                            [2] => Rome
                        )

                    [0] => 16
                    [1] => 18
                    [2] => 20
                )

        )

    [1] => Array
        (
            [name] => course_2
            [ages] => Array
                (
                    [location] => Array
                        (
                            [0] => Berlin
                        )

                    [0] => 16
                )

        )

)

答案 1 :(得分:0)

为简单起见,决定修改finalArray的输出。

新的finalArray如下所示:

Array
(
[course_1] => Array
        (
            [ages] => Array
                (
                    [16] => Array
                        (
                            [locations] => Array
                                (
                                    [0] => Berlin
                                    [1] => London
                                )

                        ),
                    [18] => Array
                        (
                            [locations] => Array
                                (
                                    [0] => Berlin
                                    [1] => Rome
                                )

                        ),
                     [20] => Array
                        (
                            [locations] => Array
                                (
                                    [0] => Berlin
                                )

                        )

                )

        ),

[course_2] => Array
        (
            [ages] => Array
                (
                    [16] => Array
                        (
                            [locations] => Array
                                (
                                    [0] => Berlin
                                )

                        )

                )

        )
)

这是代码:

foreach ($courses as $item) {
    $course_names[] = $item['name'];
}
$course_names = array_unique($course_names);
sort($course_names);


$finalArray = array();


foreach ($course_names as $key => $courseValue) {
    foreach ($courses as $key => $actualValue) {
        if($actualValue['name']==$courseValue) {
            $finalArray[$courseValue] = array();
            $finalArray[$courseValue]['id'] = $actualValue['id'];
            $finalArray[$courseValue]['ages'] = array();
        }
    }
    foreach ($courses as $key => $actualValue) {
        if($actualValue['name']==$courseValue) {
            if(!array_key_exists($actualValue['age'],$finalArray[$courseValue]['ages'])) {
                $finalArray[$courseValue]['ages'][$actualValue['age']] = array ();
            }

        }

    }
}

foreach ($finalArray as $course_name=>$arr) {
   foreach ($arr['ages'] as $age=>$arr2) {
        foreach($courses as $item) {
            if($item['name'] == $course_name && $item['age'] == $age && !in_array($item['location'], $finalArray[$course_name]['ages'][$age]['locations'])) {
                $finalArray[$course_name]['ages'][$age]['locations'][] = $item['location'];
            }
        }
   }
}

答案 2 :(得分:-1)

不确定要查找的格式是什么,但是请看一下:

$courses = array
(
    array('id'=>1, 'name' =>"course_1",'age'=>16,'location'=>"Berlin"),
    array('id'=>2,'name'=>"course_1",'age'=>18,'location'=>"Berlin"),
    array('id'=>3,'name'=>"course_1",'age'=>20,'location'=>"Berlin"),
    array('id'=>4,'name'=>"course_1",'age'=>16,'location'=>"London"),
    array('id'=>5,'name'=>"course_1",'age'=>18,'location'=>"Rome"),
    array('id'=>6,'name'=>"course_2",'age'=>16,'location'=>"Berlin")

);

$newCourses = [];

foreach ($courses as $item){
    $newCourses[$item['name']]['ages'][$item['age']]['locations'][] = $item['location'];
}

$ new课程将是:

array (
  'course_1' => 
  array (
    'ages' => 
    array (
      16 => 
      array (
        'locations' => 
        array (
          0 => 'Berlin',
          1 => 'London',
        ),
      ),
      18 => 
      array (
        'locations' => 
        array (
          0 => 'Berlin',
          1 => 'Rome',
        ),
      ),
      20 => 
      array (
        'locations' => 
        array (
          0 => 'Berlin',
        ),
      ),
    ),
  ),
  'course_2' => 
  array (
    'ages' => 
    array (
      16 => 
      array (
        'locations' => 
        array (
          0 => 'Berlin',
        ),
      ),
    ),
  ),
)

即使这不是您想要的格式,您也可以使用它来获得所需的格式。