简体   繁体   中英

PHP populate multi-dimensional array with data from many foreach

I am scraping an ecommerce website and need to get some data from products, like product name, price, ...

For that, I have:

...
// library includes... 
$html = file_get_html($link);
foreach($html->find('.productBoxClass') as $element){

  foreach($element->find('.productTitle') as $product) {
    $product = $product->plaintext;
  }

  foreach($element->find('.price') as $price) {
    $price = $price->outertext;
  }  

   // and so on...
}

I wanna save this data in a database. So, I want to save all the data in an array for after verify each product if I have to insert or just update. I am intending to populate an multi-dimensional array with this data:

Each position of the array with another array containing the information about one product... To make it easier to save in the database after...

Any help?

This seems like an abnormal data structure or you should be looping through it differently. But if it is an abnormal structure and the product and price aren't grouped together, they are just listed in the same order, then this should work:

$products = [];

$i = 0;
foreach($element->find('.productTitle') as $product) {
   $products[$i++]['product'] = $product->plaintext;
}

$i = 0;
foreach($element->find('.price') as $price) {
   $products[$i++]['price'] = $price->outertext;
}  

Note the $i++ as the key which will increment $i each loop.

If the product and pricing are grouped together in an element, then you should be looping on that element and there should be no need for a foreach for product and price.

Please check the below code, let me know your thoughts...

<?php
// library includes... 
$html = file_get_html($link);
$productArr = array();
foreach($html->find('.productBoxClass') as $element){
 $tempArr = array('title' => '','price' => 0,'other' => ''); // declare temp array for stroing each product nodes   
  foreach($element->find('.productTitle') as $product) {
    $tempArr['title'] = $product->plaintext; // To do check for empty text here
  }

  foreach($element->find('.price') as $price) {
    $tempArr['price'] = $price->outertext; // To do validate the price
  }  

  foreach($element->find('.other-features') as $price) {
    $tempArr['other'] = $price->outertext; // To do validate the price
  }  
  // and so on... with $tempArr['key']
  // then assign
  $productArr[] = $tempArr; // save temp array in global product array
}

// Product array
echo '<pre>';print_r($productArr);die;

Use in first foreach the count item:

...
// library includes... 
$html = file_get_html($link);

// Array declaration
$products = array();

foreach($html->find('.productBoxClass') as $i => $element){

  foreach($element->find('.productTitle') as $product) {
   $products[$i]['name'] = $product->plaintext;
  }

  foreach($element->find('.price') as $price) {
    $products[$i]['price'] = $price->outertext;
  }  

   // and so on...
}

And will result:

 Array
    (
        [0] => Array
            (
                [name] => Product 1
                [price] => 1.00
            )

        [1] => Array
            (
                [name] => Product 1
                [price] => 1.00
            ),
    ...
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM