doukuipei9938 2014-12-02 00:04
浏览 19
已采纳

php数组翻转值作为键,使其变得简单

i have been troubling to format array correctly i have this code:

    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>';

using simple_html_dom above i can convert html table to an array as follow:

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

                )

        )

I would like to have result as below:

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???')

any help would be apreaciate

  • 写回答

1条回答 默认 最新

  • dongma7796 2014-12-02 00:18
    关注

    Something like this. Also note that trim will do both left and right:

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测