lainard
lainard

Reputation: 13

php array flip value as key and make it simple

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

Upvotes: 0

Views: 212

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 79014

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);
}

Upvotes: 1

Related Questions