2014-12-02 74 views
0

我一直困扰到格式阵列正确 我有这样的代码:PHP数组翻转值作为密钥,并使其简单

:使用simple_html_dom以上我可以HTML表格转换为数组如下

require('simple_html_dom.php'); 

    $table = array(); 
    $html = file_get_html('apc.html'); 
    foreach($html->find('tr') as $row) { 

     $rack = ltrim($row->find('td',0)->plaintext); 
     $location = ltrim($row->find('td',1)->plaintext); 
     $usage = ltrim($row->find('td',2)->plaintext); 
     $auk = ltrim($row->find('td',3)->plaintext); 
     $cost = ltrim($row->find('td',4)->plaintext); 


    $rack = rtrim($rack); 
    $location = rtrim($location); 

    $usage = rtrim($usage); 
    $auk = rtrim($auk); 
    $cost = rtrim($cost); 

     $table[$rack][$usage][$auk][$cost] = true; 

    } 
echo '<pre>'; 
print_r($table); 
echo '</pre>'; 

[Rack01] => Array 
     (
      [741,60] => Array 
       (
        [1.409,04] => Array 
         (
          [267,72] => 1 
         ) 

       ) 

      [110,88] => Array 
       (
        [210,67] => Array 
         (
          [40,03] => 1 
         ) 

       ) 

     ) 

    [Rack 09] => Array 
     (
      [843,84] => Array 
       (
        [1.603,30] => Array 
         (
          [304,63] => 1 
         ) 

       ) 

     ) 

我想有如下结果:

array(
[0] => array (
    [usage] => 'Rack 01', 
    [usage] => '741,60', 
    [auk] => '1.409.04', 
    [cost] => '267,72') 
[1] => array (
    [usage] => 'Rack 02', 
    [usage] => 'value???', 
    [auk] => 'value???', 
    [cost] => 'value???') 

任何帮助,将A预aciate

回答

1

就像这样。另外请注意,trim会左右两边:

foreach($html->find('tr') as $row) { 
    $rack  = trim($row->find('td',0)->plaintext); 
    $location = trim($row->find('td',1)->plaintext); 
    $usage = trim($row->find('td',2)->plaintext); 
    $auk  = trim($row->find('td',3)->plaintext); 
    $cost  = trim($row->find('td',4)->plaintext); 

    $table[] = array('rack' => $rack, 
        'usage' => $usage, 
        'auk' => $auk, 
        'cost' => $cost); 
} 
+0

AbraCadaver根据需要完美地,你是我的英雄 – lainard 2014-12-02 01:07:50

相关问题