这段代码输出的内容类似于下面的描述,我需要将每个输出精确地转换为一个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有一个具有两个匹配字段

使用PHP阅读XML提要

WooCommerce基于元数据的自定义范围目录排序

LaravelEloquent 地根据表中的值提取记录

获取并判断WooCommerce用户订阅结束日期

使注册字段UserMeta值对于WooCommerce中的每个用户是唯一的

是否重新排序多维数组元素以将所有子数组中的子数组移动到元素列表的底部?

如何从该字符串中删除这些图标转换的ASCII问号字符?

我有一个显示记录的表格,每行在最右边有两个按钮.如何才能以正确的顺序获得表的值?

从两个日期创建日期范围,然后为范围中的每个日期复制子数组

升级到PHP 8.2后无法删除cURL Cookie文件

根据WooCommerce中的自定义用户元数据值更改用户角色

在WooCommercel邮箱通知上添加来自Apaczka插件的选定交付点

使用 PHP,我可以将 foreach 语句放入瞬态中吗?

如果 Select 的县不是store 所在的县,如何隐藏本地自提?

PHP 中使用 JSON 的自定义会话处理程序会因错误而 destruct 会话

无法在 Laravel 中实例化抽象类 ProductAbstract

安装 Herd 并返回 Valet 后为 502

如何使用在产品快速编辑菜单前端的自定义框中 Select 的值更新 woocommerce 产品属性值?

为什么 PDO 允许使用带命名占位符的索引数组,但仅在禁用仿真时才允许?