使用数组获取价值


Getting value with array

我正在努力获取特定数组中元素的值。在这种情况下,我想获取"徽标"的值,以从下面的代码中返回"徽标Google 2013官方.svg"。任何帮助将不胜感激。

<html>
<head>
</head>
<body>
<html>
<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" value="google" />
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['q'])) {
$search = $_POST['q'];

$url_2 = 
"http://en.wikipedia.org/w/api.php?   
action=query&prop=revisions&rvprop=content&format=json&titles=$search&rvsection=0&continue=";
$res_2 = file_get_contents($url_2);
$data_2 = json_decode($res_2);

?>
<h2>Search results for '<?php echo $search; ?>'</h2>
<ol>
<?php foreach ($data_2->query->pages as $r): 
?>
<li>
<?php foreach($r->revisions[0] as $a); 
echo $a; ?>
</li>
<?php endforeach; ?>
</ol>
<?php 
}
?>
</body>
</html>

生成的$url_2为 http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=google&rvsection=0&continue=

使用正则表达式捕获所需的内容:

<ol>
<?php foreach ($data_2->query->pages as $r): ?>
    <?php foreach($r->revisions[0] as $a): ?>
    <li>
        <?php
            preg_match_all('/ logo += +([^|]+)/', $a, $result, PREG_PATTERN_ORDER);
            echo trim($result[0][0]); // prints 'Logo Google 2013 Official.svg'
        ?>
    </li>
    <?php endforeach; ?>
<?php endforeach; ?>
</ol>