Bài toán kinh điển lồng mảng của mảng (ok)

C:\xampp\htdocs\injagroup\wp-content\themes\houzez-child\template-parts\common-label.php

Xử lý code 👍)

<?php
$termchildrens          = get_terms($taxonomy_name, array('hide_empty' => false));
$ress = array();
foreach ($termchildrens as $_key => $_termchildrens) {
  if($_termchildrens->parent == $term_id) {
    $ress[$term_id][$_termchildrens->term_id][] = $_termchildrens;
    foreach ($termchildrens as $__key => $__arrr) {
      if($__arrr->parent == $_termchildrens->term_id) {
        $ress[$term_id][$_termchildrens->term_id]['child'][] = $__arrr;    
      }
    }
  }
  // if($_termchildrens->term_id == $_key) {
  //   $ress[$term_id][$_termchildrens->term_id]['child'][] = $_termchildrens;
  // }
}
echo '<pre>';
  var_export($ress);
echo '</pre>';
?>

C:\xampp\htdocs\code\test.php

<?php
echo '<pre>';
  var_export($termchildrens);
echo '</pre>';
// view file code.php
// --- Description for file code.php
  123 => 
  WP_Term::__set_state(array(
     'term_id' => 315,
     'name' => 'Hokuso Railway',
     'slug' => 'hokuso',
     'term_group' => 0,
     'term_taxonomy_id' => 315,
     'taxonomy' => 'property_label',
     'description' => '',
     'parent' => 0,
     'count' => 0,
     'filter' => 'raw',
  )),
  124 => 
  WP_Term::__set_state(array(
     'term_id' => 316,
     'name' => 'Hokuso Railway',
     'slug' => 'hokuso-hokuso',
     'term_group' => 0,
     'term_taxonomy_id' => 316,
     'taxonomy' => 'property_label',
     'description' => '',
     'parent' => 315,
     'count' => 0,
     'filter' => 'raw',
  )),
// --
?>
<?php  
echo '<pre>';
  var_export($ress);
echo '</pre>';
array (
  315 => 
  array (
    316 => 
    array (
      0 => 
      WP_Term::__set_state(array(
         'term_id' => 316,
         'name' => 'Hokuso Railway',
         'slug' => 'hokuso-hokuso',
         'term_group' => 0,
         'term_taxonomy_id' => 316,
         'taxonomy' => 'property_label',
         'description' => '',
         'parent' => 315,
         'count' => 0,
         'filter' => 'raw',
      )),
      'child' => 
      array (
        0 => 
        WP_Term::__set_state(array(
           'term_id' => 892,
           'name' => 'Test 1',
           'slug' => 'test-1',
           'term_group' => 0,
           'term_taxonomy_id' => 892,
           'taxonomy' => 'property_label',
           'description' => '',
           'parent' => 316,
           'count' => 0,
           'filter' => 'raw',
        )),
      ),
    ),
    893 => 
    array (
      0 => 
      WP_Term::__set_state(array(
         'term_id' => 893,
         'name' => 'Hokuso Railway 2',
         'slug' => 'hokuso-railway-2',
         'term_group' => 0,
         'term_taxonomy_id' => 893,
         'taxonomy' => 'property_label',
         'description' => '',
         'parent' => 315,
         'count' => 0,
         'filter' => 'raw',
      )),
      'child' => 
      array (
        0 => 
        WP_Term::__set_state(array(
           'term_id' => 894,
           'name' => 'Hokuso Railway 3',
           'slug' => 'hokuso-railway-3',
           'term_group' => 0,
           'term_taxonomy_id' => 894,
           'taxonomy' => 'property_label',
           'description' => '',
           'parent' => 893,
           'count' => 0,
           'filter' => 'raw',
        )),
      ),
    ),
  ),
);
?>

Last updated