簡體   English   中英

如何返回數組的文本值而不是函數結果?

[英]How can I return text value of an array instead the function result?

例如,如果我有一個包含一個元素的數組,如下所示:

$dom = [file_get_html('https://www.homedepot.com/p/Connecticut-Electric-30-Amp-8-Space-10-Circuits-G2-Manual-Transfer-Switch-Kit-EGS107501G2KIT/100669964', false)];

我如何echo $dom[0]; 並讓它只返回 URL(或至少是文本值)而不是返回函數的結果?

謝謝你。

代碼:

<?php 
include "db.php";
include "Includes/header.php";
require 'simple_html_dom.php';

$dom = [file_get_html('https://www.homedepot.com/p/Connecticut-Electric-30-Amp-8-Space-10-Circuits-G2-Manual-Transfer-Switch-Kit-EGS107501G2KIT/100669964', false),
        file_get_html('https://www.homedepot.com/p/31180154554', false),
        file_get_html('https://www.homedepot.com/p/Connecticut-Electric-30-Amp-Adapts-to-20-Amp-CESMAD3020/100128920', false)
       ];

$answer = array();
for($i = 0; $i < sizeof($dom); ++$i)
{
if(!$dom[$i]->find('span#ajaxPrice',0))
{
    $formattedUrl = '$dom[$i] - SHOW URL HERE THAT SEARCH WAS ATTEMPTED ON';    
    echo "<tr><td>" . "Not Listed" . "</td><td>" . $formattedUrl . "</td><td>" . "Not Listed" . "</td></tr>";
    continue;
}

$element = $dom[$i]->find('span#ajaxPrice',0);
$title = $dom[$i]->find('h1.product-title__title',0);
$sku = $dom[$i]->find('h2.product_details.modelNo',0);
$formattedSku = str_replace("Model # ", "", $sku);
$titlePlain = $title->plaintext;

if(isset($element->content)) 
{
    $price = $element->content; 
    $priceFloat = floatval($price); 
}

$query = "INSERT INTO homedepot(Date,Title,Price) VALUES (CURDATE(), ?, ?)";
$stmt = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt, "sd", $titlePlain, $priceFloat);
mysqli_stmt_execute($stmt);

echo "<tr><td>" . $formattedSku . "</td><td>" . $titlePlain . "</td><td>" . $price . "</td></tr>";

sleep(1);
}

?>

只需將 URL 存儲在數組中,然后在需要時執行file_get_html

$urls = ['https://www.homedepot.com/p/Connecticut-Electric-30-Amp-8-Space-10-Circuits-G2-Manual-Transfer-Switch-Kit-EGS107501G2KIT/100669964',
         'https://www.homedepot.com/p/31180154554',
         'https://www.homedepot.com/p/Connecticut-Electric-30-Amp-Adapts-to-20-Amp-CESMAD3020/100128920'
        ];

foreach($urls as $url)
{
    $dom = file_get_html($url, false);

    if(!$dom->find('span#ajaxPrice', 0))
    {
        echo "<tr><td>" . "Not Listed" . "</td><td>" . $url . "</td><td>" . "Not Listed" . "</td></tr>";
        continue;
    }
    // more code . . .
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM