jQuery或javascript多维数组

时间:2016-02-27 18:54:28

标签: jquery arrays

我尝试将php代码转换为jQuery代码。

jQuery 尝试如下:

    db.transaction(function(tx) {
        tx.executeSql("select c.categories_id, c.parent_id, c.categories_image, cd.categories_name from categories c, categories_description cd where c.categories_id = cd.categories_id and cd.language_id = '1' order by c.parent_id, c.sort_order, cd.categories_name", [], function (tx, results) {     

            var categories = {};

            var ItemArray1 = [];
            var ItemArray2 = [];
            var ItemArray3 = [];
            var len = results.rows.length, i;
            for (i = 0; i < len; i++) {
                var r = results.rows.item(i);
                categories[r.categories_id] = r;                        

                ItemArray1.push(r.parent_id);   
                ItemArray2.push(r.categories_id);                           
                ItemArray3.push({ name : r.categories_name, image : r.categories_image });  


            }
            console.log(ItemArray1);
            console.log(ItemArray2);
            console.log(ItemArray3);
        });
    });

由类构造函数构建的php代码是:

    $categories_query = tep_db_query("select c.categories_id, c.parent_id, c.categories_image, cd.categories_name from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id. "' order by c.parent_id, c.sort_order, cd.categories_name");

    while ( $categories = tep_db_fetch_array($categories_query) ) {
      $this->_data[$categories['parent_id']][$categories['categories_id']] = array('name' => $categories['categories_name'],
                                                                                   'image' => $categories['categories_image']);
    }

包含对象名称和图像的生成数组如下:

  [0]=> {   //parent_id = 0
    [1]=> {   //categories_id
      ["name"]=> "Hardware"
      ["image"]=> "category_hardware.gif"
    }
    [2]=> {  //categories_id
      ["name"]=> "Software"
      ["image"]=> "category_software.gif"
    }
    [3]=> {   //categories_id
      ["name"]=> "DVD Movies"
      ["image"]=> "category_dvd_movies.gif"
    }
    [21]=> {   //categories_id
      ["name"]=> "Gadgets"
      ["image"]=> "category_gadgets.png"
    }
  }
  [1]=>  {    //parent_id = 1  equals categories_id
    [17]=> {  //categories_id etc, etc, etc
      ["name"]=> "CDROM Drives"
      ["image"]=> "subcategory_cdrom_drives.gif"
    }
    [4]=> {
      ["name"]=> "Graphics Cards"
      ["image"]=> "subcategory_graphic_cards.gif"
    }
    [8]=> {
      ["name"]=> "Keyboards"
      ["image"]=> "subcategory_keyboards.gif"
    }
    [16]=>
    array(2) {
      ["name"]=> "Memory"
      ["image"]=> "subcategory_memory.gif"
    }
    [9]=> {
      ["name"]=> "Mice"
      ["image"]=> "subcategory_mice.gif"
    }
    [6]=> {
      ["name"]=> "Monitors"
      ["image"]=> "subcategory_monitors.gif"
    }
    [5]=> {
      ["name"]=> "Printers"
      ["image"]=> "subcategory_printers.gif"
    }
    [7]=>
    array(2) {
      ["name"]=> "Speakers"
      ["image"]=> "subcategory_speakers.gif"
    }
  }
  [2]=> {
    [19]=> {
      ["name"]=> "Action"
      ["image"]=> "subcategory_action_games.gif"
    }
    [18]=> {
      ["name"]=> "Simulation"
      ["image"]=> "subcategory_simulation.gif"
    }
    [20]=> {
      ["name"]=> "Strategy"
      ["image"]=> "subcategory_strategy.gif"
    }
  }
  [3]=> {
    [10]=> {
      ["name"]=> "Action"
      ["image"]=> "subcategory_action.gif"
    }
    [13]=>
    array(2) {
      ["name"]=> "Cartoons"
      ["image"]=> "subcategory_cartoons.gif"
    }
    [12]=> {
      ["name"]=> "Comedy"
      ["image"]=> "subcategory_comedy.gif"
    }
    [15]=> {
      ["name"]=> "Drama"
      ["image"]=> "subcategory_drama.gif"
    }
    [11]=> {
      ["name"]=> "Science Fiction"
      ["image"]=> "subcategory_science_fiction.gif"
    }
    [14]=> {
      ["name"]=> "Thriller"
      ["image"]=> "subcategory_thriller.gif"
    }
  }

说实话,我有点完全迷失了。 我的想法是建立一个递归树菜单。 可以看到完整的php类here

0 个答案:

没有答案