user2837690
user2837690

Reputation: 5

PHP: Split foreach result

I've a scraper script:

foreach($html->find('td.Live') as $e)
echo $e->plaintext . '<br>';

The result will look like this:

name 1 status  <br>

And I really need to turn the result into 2 arrays like:

name_array = (name 1, name 2, name 3, name 4)
status_array = (status, status, status, status)

How can I do that?

Upvotes: 0

Views: 180

Answers (2)

Akhil
Akhil

Reputation: 967

$name_array = $status_array = array();
foreach($html->find('td.Live') as $e) {
    $segment= explode(' ',$e->plaintext);
    $status_array[] = $segment[2];
    $name_array[] = str_replace($segment[2],'',$e->plaintext);
}
echo 'Name: ';
print_r($name_array);
echo '<br> Status:';
print_r($status_array);

Upvotes: 0

Alex Kapustin
Alex Kapustin

Reputation: 2459

$name_array = $status_array = array();
foreach ($html->find('td.Live') as $e) {
    $parts = explode(" ", trim($e->plaintext));
    $status_array[] = array_pop($parts);
    $name_array[] = trim(implode(" ", $parts));
}
echo "<pre>Names:\n", print_r($name_array, 1), "\nStatuses:\n", print_r($status_array, 1), "</pre>";

Upvotes: 2

Related Questions