这段代码输出的内容类似于下面的描述,我需要将每个输出精确地转换为一个PHPvar

eg:

[product-link]

[0] »» $var_prod_link_0 = 'the link 0 here'; 
[1] »» $var_prod_link_1 = 'the link 1 here'; 
etc..

[product-img-src]

[0] »» $var_prod_img_0 = 'the image 0 link here'; 
[1] »» $var_prod_img_1 = 'the image 1 link here';
etc..

[h3-title-text]

[0] »» $var_title_text_0 = 'the title 0 here'; 
[1] »» $var_title_text_1 = 'the title 1 here';
etc.. 

[price]

[0] »» $var_price_0 = 'the price 0 here';
[1] »» $var_price_1 = 'the price 1 here'; 
ect.. 

This is the code:

# set the libxml parameters and create new DOMDocument/XPath objects.
libxml_use_internal_errors( true );
$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->strictErrorChecking=false;
$dom->recover=true;
$dom->loadHTML( $html );
libxml_clear_errors();

$xp=new DOMXPath( $dom );

# some basic XPath expressions
$exprs=(object)array(
    'product-link'      =>  '//a[@class="product-image-link"]',
    'product-img-src'   =>  '//a[@class="product-image-link"]/img',
    'h3-title-text'     =>  '//h3[@class="wd-entities-title"]',
    'price'             =>  '//span[@class="price"]/span/bdi'
);
# find the keys (for convenience) to be used below
$keys=array_keys( get_object_vars( $exprs ) );

# store results here
$res=array();

# loop through all patterns and issue XPath query.
foreach( $exprs as $key => $expr ){
    # add key to output and set as an array.
    $res[ $key ]=[];
    $col=$xp->query( $expr );
    
    # find the data if the query succeeds
    if( $col && $col->length > 0 ){
        foreach( $col as $node ){
            switch( $key ){
                case $keys[0]:$res[$key][]=$node->getAttribute('href');break;
                case $keys[1]:$res[$key][]=$node->getAttribute('src');break;
                case $keys[2]:$res[$key][]=trim($node->textContent);break;
                case $keys[3]:$res[$key][]=trim($node->textContent);break;
            }
        }
    }
}
printf('<pre>%s</pre>',print_r($res,true));

The output from the above code:

Array
(
    [product-link] => Array
        (
            [0] => https://...linkhere
            [1] => https://wwwle.com/banana
        )

    [product-img-src] => Array
        (
            [0] => https://image-goes-here.jpg
            [1] => https://www.example.com/kittykat.jpg
        )

    [h3-title-text] => Array
        (
            [0] => The title goes here
            [1] => Oh look, another title!
        )

    [price] => Array
        (
            [0] => â¬20,00
            [1] => â¬540,00
        )

)

推荐答案

在Foreach循环中使用这样的数组,并使用$idx来寻址所有其他数组中的正确项

//The array you say you have produced!
$res= [
        'product-link'      => ['https://...linkhere', 'https://wwwle.com/banana'],
        'product-img-src'   => ['https://image-goes-here.jpg', 'https://www.example.com/kittykat.jpg'],
        'h3-title-text'     => ['The title goes here', 'Oh look, another title!'],
        'price'             => ['20,00','540,00']
];

所以为了处理这些数组,我做了

foreach ( $res['product-link'] as $idx => $prod ) :
?>
        <div id="master"> 
            <h3><?php echo $res['h3-title-text'][$idx]; ?></h3> 
            <a href="<?php echo $prod; ?>" target="_blank"> 
                <img src="<?php echo $res['product-img-src'][$idx] ?>"> 
                <span><?php echo $res['price'][$idx]; ?></span> 
            </a> 
        </div>
<?php
endforeach;

结果

        <div id="master"> 
            <h3>The title goes here</h3> 
            <a href="https://...linkhere" target="_blank"> 
                <img src="https://image-goes-here.jpg"> 
                <span>20,00</span> 
            </a> 
        </div>
        <div id="master"> 
            <h3>Oh look, another title!</h3> 
            <a href="https://wwwle.com/banana" target="_blank"> 
                <img src="https://www.example.com/kittykat.jpg"> 
                <span>540,00</span> 
            </a> 
        </div>

Php相关问答推荐

Laravel通过(扩展FormRequest类)方法验证表单请求

SQLSTATE [42S02]:找不到基表或视图:1146 Table eshop. sessions不存在'''

仅在WooCommerce管理订单视图上显示订单项自定义元数据

由于某种原因,FILE_GET_CONTENTS从一天到另一天停止工作,并在PHP 7.2.34中返回一条&http/1.1 406不能接受&的错误

添加登录:需要app.yaml仍然允许所有拥有Google帐户的人访问

目标类[Set_Locale]不存在.拉威尔

Laravel:如何展平多维Eager 加载的集合

我必须对所有的SQL查询使用预准备语句吗?

根据发货类别在WooCommerce产品页面中显示快捷代码

在Laravel中创建辅助函数时的约定

在带有livewire 3的laravel中使用规则方法时,无法进行实时验证

如何用Ubuntu 22.04在VSCode中启用XDebug

是否有条件判断是否发布了另一个Wordpress页面?

除WooCommerce中的特定国家/地区外,有条件地要求填写邮政编码 checkout 字段

防止 WooCommerce 在前端加载全尺寸图像

加载 Xdebug PHP 7.4.0 失败

为WooCommerce中的Guest用户保存唯一的客户代码到订单中

为WooCommerce中的运输方式ID设置必填的 checkout 订单备注字段

如何通过工厂为点列制作一个假坐标?

使用 Process facade 在外部 Laravel 项目上运行 Artisan 命令会返回主项目的数据库错误