2017-05-10 5 views
0

私はeコマースのウェブサイトをこすると、製品名、価格など、製品のいくつかのデータを取得する必要があるのですからデータを多次元配列を移入:PHPはそのため <p></p>は、私が持っている、...多くのforeachの

... 
// 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... 
} 

このデータをデータベースに保存します。だから、私は挿入または更新するだけで各製品を検証した後に、すべてのデータを配列に保存したい。私は、このデータを多次元配列を移入することを意図しています:

つの製品についての情報を含む別の配列との配列の各位置... ...それが簡単に後にデータベースに保存できるようにすること

助けが必要ですか?

+0

あなたのステートメント 'foreach($ element-> find( 'productTitle')$ product)... 'で、$ productはデータの_copy_を持つ変数です。それを '$ product-> plaintext'に置き換えることは意味がありますが、そのままでは結果を捨てているように見えます。どこにも割り当てられていません。それはあなたの意図ですか? – Manngo

答えて

1

これは異常なデータ構造のようです。そうでなければ、別の方法でループする必要があります。しかし、それは異常な構造であり、製品と価格が一緒にグループ化されていない、彼らはちょうど同じ順序でリストされている場合、これは動作するはずです:

$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; 
} 

注私は$をインクリメントするキーとして++ $は私はそれぞれのループ。

製品と価格が要素でグループ化されている場合は、その要素をループする必要があり、製品と価格についてforeachは必要ありません。

1

以下のコードを確認してください、私はあなたの考えをお聞かせ...最初のforeachで

<?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; 
+0

うまく働いた!しかし、それはちょうど最初の製品を取得しています。配列をprint_r()すると、配列内には1つの製品内の1つの位置しかありません。 – Developer1903

1

使用カウント項目:

... 
// 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... 
} 

となります:

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

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

これは、 'foreach($ element-> find( '。productTitle'))'を実行することで問題になります。複数のコードが存在する可能性があり、それぞれのコードはそれぞれのコードを上書きします。 – Devon

+0

商品にはユニファイドコードがありますか? –

関連する問題

 関連する問題