私は非常に長い間PHPを使用してきましたが、ごく最近までコールバックをあまり使用していませんでした。次のコードでは、コールバック(例は、疑問に思っている場合はQueryPathですが、コールバックを受け入れるものであれば何でもかまいません)は、配列にリンクを追加します。
// parse any product links out of the html
$aProducts = array();
qp( $html, 'a' )->each(function($index, $element){
global $aProducts;
$link = qp($element)->attr('href');
$pregMatch = preg_match('@(.*)-p-(.*)\.html@i', $link, $matches);
if( $pregMatch ) {
$product_id = (int)$matches[2];
if( !in_array($product_id, $aProducts) ) {
$aProducts[] = $product_id;
}
}
});
// print out our product array
print_r( $aProducts );
使用する代わりに何global $aProducts
がありますか(ある場合)?