我有以下代码:
<?php
$html = '<div>
<div class="block">
<div class="id">10</div>
<div class="name">first element</div>
</div>
<div class="block">
<div class="name">second element</div>
</div>
<div class="block">
<div class="id">30</div>
<div class="name">third element</div>
</div>
</div>';
preg_match_all('/<div class="block">[\s]+<div class="id">(.*?)<\/div>[\s]+<div class="name">(.*?)<\/div>[\s]+<\/div>/ms', $html, $matches);
print_r($matches);
我想得到带有id和name的数组,但是第二个位置没有id,所以我的preg match跳过了这个。如何在不跳过的情况下生成数组并像这样打印某事 [ ... [id => 0 // or null, name => 'second element'] ...]?