Jump to content

output to an array


leke

Recommended Posts

Hi,

The foreach loop below looks like it is assigning some key => value pairs to some html output. Is there a way I can assign the output to an array so I can access it by key outside the loop?

 

Thanks...code follows...

 

<?php
include 'simple_html_dom.php';

// source: http://www.bitrepository.com/php-simple-html-dom-parser.html
$url = 'http://www.amica.fi/Ravintolat/Amica-ravintolat/Ravintolat-kaupungeittain/Tampere/Technopolis-Hermia--Hermia';
$html = file_get_html($url);

foreach($html->find('div[class=ContentArea]') as $key => $info)
{
echo $key.$info."<br><br>\n";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/198117-output-to-an-array/
Share on other sites

if you need the html output keep it the way it is, to store it for later usage while the script is still running try adding

 

$myArray = array();

just above the foreach statement under $html

then in the foreach loop

$myArray[] = $info;

if you need the array your making at a later time, on another page or else where I might suggest storing it as a session which you would just add

$_SESSION['myArray'] = $myArray;

after the foreach loop

 

This is just concept I havent tested this theroy. It should work but may require a little play to get it right you can also look into array_push(), or atleast i think thats the name of it.

Link to comment
https://forums.phpfreaks.com/topic/198117-output-to-an-array/#findComment-1039477
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.