Putting simple_html_dom variables into an array - php

I am trying to extract some info from a website using simple_html_dom.
Currently I am using:
foreach ($html->find('div.product') as $results) {
foreach ($results->find('div.image') as $img) {
echo $img;
}
foreach ($results->find('a.title') as $title) {
echo $title->plaintext;
}
foreach ($results->find('div.price') as $price) {
echo $price;
}
}
Which works fine. However I need to be able to echo each variable outside of the foreach loop. If I do that using the above code, only the final result will be displayed, i.e. out of the 10 products I am trying to extract only the 10th will be displayed.
Is there a way I can use an array to store all the results from each foreach loop then echo them out once the overall loop is finished?
Something like this:
foreach ($html->find('div.product') as $results) {
foreach ($results->find('div.image') as $img) {
array($img);
}
foreach ($results->find('a.title') as $title) {
array($title->plaintext);
}
foreach ($results->find('div.price') as $price) {
array($price);
}
}
echo array($img);
echo array($title);
echo array($price);
Sorry if this question is confusing I don't have the best grasp on PHP, especially arrays!

$array_img = array();
$array_title = array();
$array_price = array();
foreach ($html->find('div.product') as $results) {
foreach ($results->find('div.image') as $img) {
$array_img[] = $img;
}
foreach ($results->find('a.title') as $title) {
$array_title[]= $title->plaintext;
}
foreach ($results->find('div.price') as $price) {
$array_price[]= $price;
}
}
echo '<pre>';
print_r($array_img);
print_r($array_title);
print_r($array_price);
echo '</pre>';

$images = array();
foreach ($html->find('div.product') as $results) {
foreach ($results->find('div.image') as $img) {
$images[] = $img; // append $img to the $images array
}
}
var_dump($images);
Do the same for the title and price data as well.

$img = array();
$title = array();
$price = array();
foreach ($html->find('div.product') as $results) {
$img[] = $results->find('div.image');
$title[] = $results->find('a.title');
$price[] = $results->find('div.price');
}
print_r($img);
print_r($title);
print_r($price);

Not sure if I completely understand you question, try the following.
$priceList = $titleList = $imgList = array();
foreach ($html->find('div.product') as $results) {<br/>
foreach ($results->find('div.image') as $img) {<br/>
$imgList[] = $img;<br/>
}<br/>
foreach ($results->find('a.title') as $title) {<br/>
titleList[] = $title;<br/>
}<br/>
foreach ($results->find('div.price') as $price) {<br/>
priceList[] = $price;<br/>
}<br/>
}<br/>
foreach ($imgList as $img) {<br/>
echo $img;<br/>
}<br/>
And so on...

Related

Error while uploading multiple images for a product in Laravel

Im receiving this error, "foreach() argument must be of type array|object, null given" but with a successful upload, how can resolve it.
foreach ($this->attribute_values as $key=>$attribute_value) {
$avalues = explode(",", $attribute_value);
foreach ($avalues as $avalue) {
$attr_value = new AttributeValue();
$attr_value->product_attribute_id = $key;
$attr_value->value = $avalue;
$attr_value->product_id = $product->id;
$attr_value->save();
}
}
Well I wrapped the code in an if statement like this.
if (is_array($this->attribute_values)){
foreach ($this->attribute_values as $key => $attribute_value) {
$avalues = explode(",", $attribute_value);
foreach ($avalues as $avalue) {
$attr_value = new AttributeValue();
$attr_value->product_attribute_id = $key;
$attr_value->value = $avalue;
$attr_value->product_id = $product->id;
$attr_value->save();
}
}
}
if (is_array($this->attribute_values)) {
$attr_values = [];
foreach ($this->attribute_values as $key => $attribute_value) {
$avalues = explode(",", $attribute_value);
if (is_array($avalues)) {
foreach ($avalues as $avalue) {
$attr_values['product_attribute_id'] = $key;
$attr_values['value'] = $avalue;
$attr_value['product_id'] = $product->id;
}
AttributeValue::insert($attr_values);
}
}
}
$main_data = new Collection($this->attribute_values);
$main_data = $main_data->toArray();
foreach ($main_data as $key=>$attribute_value) {
$avalues = explode(",", $attribute_value);
foreach ($avalues as $avalue) {
$attr_value = new AttributeValue();
$attr_value->product_attribute_id = $key;
$attr_value->value = $avalue;
//$attr_value->product_id = $product->id;
$attr_value->save();
}
}

Count item tags from merged rss feed

I'm trying to count the title tag from a list of RSS feeds.
$urls = "http://www.engadget.com/rss.xml";
$xml = simplexml_load_file($urls);
$tags = array();
foreach($xml->channel->item as $item) {
$children = $item->title;
foreach ($children as $node) {
$tags[] = $node->getName();
}
}
$count= array_count_values($tags);
echo '<pre>';
print_r($count);
doing it this way above... it works but what if I had multiple URLs. how can I go about it?
Place the urls in an array and then just foreach around the urls array doing basically what you were already doing
$urls = ["http://www.engadget.com/rss.xml","http://www.engadget.com/rss.xml"];
$tags = array();
foreach ( $urls as $url ) {
$xml = simplexml_load_file($url);
foreach($xml->channel->item as $item) {
$children = $item->title;
foreach ($children as $node) {
$tags[] = $node->getName();
}
}
}
$count= array_count_values($tags);
echo '<pre>';
print_r($count);

Storing multiple foreach loops into a array

foreach (array_chunk($html->find('div[class=yemek]'), 4, true) as $array) {
echo '///';
foreach($array as $ul) {
foreach($ul->find('img') as $li) {
echo $li->alt . ',';
}
}
}
I want to store the last result into an array. I have no idea since there is 2 foreach loop. Is it possible? Or I have something in my mind.
assign to the same variable each step, at last you will just the last value.
$last = '';
foreach (array_chunk($html->find('div[class=yemek]'), 4, true) as $array) {
echo '///';
foreach($array as $ul) {
foreach($ul->find('img') as $li) {
echo $li->alt . ',';
}
}
}
$lastArray = [$last];

how can echo item in php foreach only at First item

I have an array $result and I want to echo $image for the first item only. My code this is:
<?php
foreach ($result as $item)
{
$image = $item->image;
$id = $item->id;
$cat = $item->catid;
}
?>
i want echo $image in first item and don't echo it Second, or third, etc item
$item = reset($array);
// do stuff with $item here
^ gets the first item of the array without removing it.
Or just break before the closing } in your code.
<?php
$first = true;
foreach ($result as $item)
{
$image = ($first) ? $item->image : '';
$id = $item->id;
$cat = $item->catid;
$first = false;
}
?>
if you want display only first item, you can use array_shift
$item = array_shift($result);
$image = $item->image;
$id = $item->id;
$cat = $item->catid;
You can count your iteration and print only at first run
$i=o;
foreach ($result as $item)
{
$image = $item->image;
$id = $item->id;
$cat = $item->catid;
if($i==0)
{
echo $image;
}
$i++;
}
$arr = array('foo', 'baz', 'baz');
reset($arr);
$first = current($arr);
echo $first->image; // outputs 'foo'
Try this:
<?php
foreach ($result as $item)
{
$image = $result[0]->image;
$id = $item->id;
$cat = $item->catid;
}
?>

How do I display all the values of a multi dimentional array?

Hey I have an array that contains an array:
$outPutResults = array();
while($row = mysqli_fetch_array($results))
{
$outPutResults[] = $row['industry'];
$outPutResults[] = $row['location'];
$outPutResults[] = $row['title'];
$outPutResults[] = $row['description'];
}
$searchResults[] = $outPutResults;
I am little confused how I would echo the contents.
As far as I am aware $outputresults[] should now be contained within $searchresults[].
To output the content I'm using :
foreach ($searchResults[0] as $item) {echo $item;}
But this is only echoing the first set of results so rather than having to repeat the above and changing the number (0) each time how would i do it so it outputs all the sub arrays?
Just nest your foreach:
foreach ($searchResults as $row) {
foreach($row as $item) {
echo $item;
}
}
$outPutResults = array();
$i = 0;
while($row = mysqli_fetch_array($results))
{
$outPutResults[$i] = $row;
$i++;
}
foreach ($outPutResults as $items)
foreach($items as $item)
echo $item;
Why are you doing
$searchResults[] = $outPutResults;
instead of just
$searchResults = $outPutResults;
Then you don't have to worry about nesting your foreach statements.
You should nest two loops. For example like this:
foreach ($searchResults as $array) {
foreach ($array as $item){
echo $item;
}
}

Categories