0

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 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

AbraCadaver Perfectly as needed, you are my hero

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.