Jump to content

[SOLVED] extract html table cells and put to an array


Recommended Posts

i have a table like:

<tr><td>headA</td><td>headB</td><td>headC</td><td>headD</td></tr>
<tr><td>1a</td><td>1b</td><td>1c</td><td>1d</td></tr>
<tr><td>2a</td><td>2b</td><td>2c</td><td>2d</td></tr>
<tr><td>3a</td><td>3b</td><td>3c</td><td>3d</td></tr>
<tr><td>4a</td><td>4b</td><td>4c</td><td>4d</td></tr>

 

where there can be any number of rows and there can be any number of columns.

 

how can i read through this and create an array for each row, and use the header row as the keys.

 

ie have it something like:

 

myArray[0] = array(

    'headA' = '1a',

    'headB' = '1b',

    'headC' = '1c',

    'headD' = '1c',

);

   

myArray[1] = array(

    'headA' = '2a',

    'headB' = '2b',

    'headC' = '2c',

    'headD' = '2c',

);

 

etc....

 

thanks

chris

 

 

 

I have two problems. One, the array you want it in... is not the general format for any array really. I would better like it if it was something like this:

 

$table = array("column1"=>array("headA","1a","2a","3a","4a"),"column2","column3","column4");

 

And so on. This way, you can easily reference row numbers and column numbers much more easily by going...

 

echo $table['column1'][0];

 

Would output headA as that would be the 'zeroth' row in the first column, etc...

 

My second problem is.. why? If this was produced by echoing it out in PHP, can't you just use that loop and automatically have all of this?

try this (php 5)

<?php
$str = '<table>
<tr><td>headA</td><td>headB</td><td>headC</td><td>headD</td></tr>
<tr><td>1a</td><td>1b</td><td>1c</td><td>1d</td></tr>
<tr><td>2a</td><td>2b</td><td>2c</td><td>2d</td></tr>
<tr><td>3a</td><td>3b</td><td>3c</td><td>3d</td></tr>
<tr><td>4a</td><td>4b</td><td>4c</td><td>4d</td></tr>
</table>';

$xml = simplexml_load_string($str);
$keys = array();
foreach ($xml->tr[0] as $key)
{
    $keys[] = (string)$key;
}


$k=0;
foreach ($xml->tr as $row)
{
    if ($k>0)
    {
        $data[$k-1] = array();
        $k2 = 0;
        foreach ($row as $cell)
        {
            $data[$k-1][$keys[$k2]] = (string)$cell;
            $k2++;
        }
    }
    $k++;
}
echo '<pre>', print_r($data, true), '</pre>';
?>

-->
Array
(
    [0] => Array
        (
            [headA] => 1a
            [headB] => 1b
            [headC] => 1c
            [headD] => 1d
        )

    [1] => Array
        (
            [headA] => 2a
            [headB] => 2b
            [headC] => 2c
            [headD] => 2d
        )

    [2] => Array
        (
            [headA] => 3a
            [headB] => 3b
            [headC] => 3c
            [headD] => 3d
        )

    [3] => Array
        (
            [headA] => 4a
            [headB] => 4b
            [headC] => 4c
            [headD] => 4d
        )

)

thanks berand, But is there a way to do this in php4 - i get an error message:

Call to undefined function: simplexml_load_string()

 

try this (php 5)

<?php
$str = '<table>
<tr><td>headA</td><td>headB</td><td>headC</td><td>headD</td></tr>
<tr><td>1a</td><td>1b</td><td>1c</td><td>1d</td></tr>
<tr><td>2a</td><td>2b</td><td>2c</td><td>2d</td></tr>
<tr><td>3a</td><td>3b</td><td>3c</td><td>3d</td></tr>
<tr><td>4a</td><td>4b</td><td>4c</td><td>4d</td></tr>
</table>';

$xml = simplexml_load_string($str);
$keys = array();
foreach ($xml->tr[0] as $key)
{
    $keys[] = (string)$key;
}


$k=0;
foreach ($xml->tr as $row)
{
    if ($k>0)
    {
        $data[$k-1] = array();
        $k2 = 0;
        foreach ($row as $cell)
        {
            $data[$k-1][$keys[$k2]] = (string)$cell;
            $k2++;
        }
    }
    $k++;
}
echo '<pre>', print_r($data, true), '</pre>';
?>

-->
Array
(
    [0] => Array
        (
            [headA] => 1a
            [headB] => 1b
            [headC] => 1c
            [headD] => 1d
        )

    [1] => Array
        (
            [headA] => 2a
            [headB] => 2b
            [headC] => 2c
            [headD] => 2d
        )

    [2] => Array
        (
            [headA] => 3a
            [headB] => 3b
            [headC] => 3c
            [headD] => 3d
        )

    [3] => Array
        (
            [headA] => 4a
            [headB] => 4b
            [headC] => 4c
            [headD] => 4d
        )

)

thanks... i must be an idiot 'cuz i still cant get it to go.. i did find something else and am able to get a result that looks like

Array

(

    [0] => Array

        (

            [0] => headA

            [1] => headB

            [2] => headC

            [3] => headD

        )

 

    [1] => Array

        (

            [0] => 1a

            [1] => 1b

            [2] => 1c

            [3] => 1d

        )

 

    [2] => Array

        (

            [0] => 2a

            [1] => 2b

            [2] => 2c

            [3] => 2d

        )

 

    [3] => Array

        (

            [0] => 3a

            [1] => 3b

            [2] => 3c

            [3] => 3d

        )

 

    [4] => Array

        (

            [0] => 4a

            [1] => 4b

            [2] => 4c

            [3] => 4d

        )

 

)

 

i just cant figure how to get the keys to be named 'headA', headB etc...

My code shows you how to do that.

 

Put cells of first row in array $keys. Giving :

 

$keys = array (

0 => "HeadA",

1 => "HeadB",

2 => "HeadC",

3 => "HeadD"

);

 

When you get the other cells, instead of placing in $data[0], place in $data[$keys[0]] etc

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.