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
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
Share on other sites

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.