簡體   English   中英

二維數組到多維數組

[英]Two-dimensional array to multidimensional array

這就是我所擁有的:

[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")
                      )

                )
       )
);

我為此奮斗了一段時間,感到受阻,任何想法都值得歡迎。

$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
                )

        )

)

為了簡單起見,決定修改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'];
            }
        }
   }
}

不確定要查找的格式是什么,但請看以下內容:

$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',
        ),
      ),
    ),
  ),
)

即使這不是您想要的格式,您也可以嘗試使用它來獲得所需的格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM