Php 仅输出基于初始字符的数组的一部分
我有一个数组Php 仅输出基于初始字符的数组的一部分,php,multidimensional-array,Php,Multidimensional Array,我有一个数组$myArr['words'],其中包含以下格式的数据: Array ( [above-the-fold] => Array ( [term] => Above the fold [desc] => The region of a Web ... ) [active-voice] => Array ( [term] => Active voice [desc] => Makes subjects do
$myArr['words']
,其中包含以下格式的数据:
Array (
[above-the-fold] => Array
(
[term] => Above the fold
[desc] => The region of a Web ...
)
[active-voice] => Array
(
[term] => Active voice
[desc] => Makes subjects do ...
)
[backlinks] => Array
(
[term] => Backlinks
[desc] => Used on content ....
)
)
我输出的内容如下:
foreach($myArr['words'] as $k => $v) {
echo '
<a href="#'.$k.'">
'.$v['term'].'
</a>';
}
您可以检查钥匙的位置0:
if(strpos($k, 'a') === 0) {
//echo
}
//or
if($k[0] === 'a') {
//echo
}
或者对数组进行过滤,并在$result
上进行foreach
:
$result = array_filter($myArr['words'],
function($v) { return $v[0] === 'a'; },
ARRAY_FILTER_USE_KEY);
您可以检查位置0的键:
if(strpos($k, 'a') === 0) {
//echo
}
//or
if($k[0] === 'a') {
//echo
}
或者对数组进行过滤,并在$result
上进行foreach
:
$result = array_filter($myArr['words'],
function($v) { return $v[0] === 'a'; },
ARRAY_FILTER_USE_KEY);
您只需访问字符串的第一个字母(如访问
数组
索引),然后查看它是否是所需的字符
下面的示例显示了以a
或a
开头的所有术语:
$arr = [
'above-the-fold' => [
'term' => 'Above the fold',
'desc' => 'The region of a Web ...'
],
'active-voice' => [
'term' => 'Above the fold',
'desc' => 'The region of a Web ...'
],
'backlinks' => [
'term' => 'Above the fold',
'desc' => 'The region of a Web ...'
]
];
foreach($arr as $k => $v) echo ($v['term'][0] != 'a' && $v['term'][0] != 'A') ?: $v['term'];
这是一个演示。您只需访问字符串的第一个字母(就像访问
数组
索引)并查看它是否是您想要的字符
下面的示例显示了以a
或a
开头的所有术语:
$arr = [
'above-the-fold' => [
'term' => 'Above the fold',
'desc' => 'The region of a Web ...'
],
'active-voice' => [
'term' => 'Above the fold',
'desc' => 'The region of a Web ...'
],
'backlinks' => [
'term' => 'Above the fold',
'desc' => 'The region of a Web ...'
]
];
foreach($arr as $k => $v) echo ($v['term'][0] != 'a' && $v['term'][0] != 'A') ?: $v['term'];
这是一个演示。您还可以使用更灵活的方式:通过正则表达式匹配:
<?php
$myArr = [
'zzzz' => [
'term' => 'zuzuzu',
'desc' => 'zazaza ...'
],
'above-the-fold' => [
'term' => 'Above the fold',
'desc' => 'The region of a Web ...'
],
'active-voice' => [
'term' => 'Active voice',
'desc' => 'Makes subjects do ...'
],
'backlinks' => [
'term' => 'Backlinks',
'desc' => 'Used on content ....'
]
];
$reg = "/^a/"; // Key starts with "a"
foreach (preg_grep($reg, array_keys($myArr)) as $k => $v)
echo "<a href=\"#$v\" title=\"{$myArr[$v]['desc']}\">{$myArr[$v]['term']}</a>\n";
您还可以使用更灵活的方式:通过正则表达式匹配:
<?php
$myArr = [
'zzzz' => [
'term' => 'zuzuzu',
'desc' => 'zazaza ...'
],
'above-the-fold' => [
'term' => 'Above the fold',
'desc' => 'The region of a Web ...'
],
'active-voice' => [
'term' => 'Active voice',
'desc' => 'Makes subjects do ...'
],
'backlinks' => [
'term' => 'Backlinks',
'desc' => 'Used on content ....'
]
];
$reg = "/^a/"; // Key starts with "a"
foreach (preg_grep($reg, array_keys($myArr)) as $k => $v)
echo "<a href=\"#$v\" title=\"{$myArr[$v]['desc']}\">{$myArr[$v]['term']}</a>\n";
请举例说明您希望输出的样子?请举例说明您希望输出的样子?注意,如果上面的键以大写字母开头,它将被忽略。注意,如果上面的键以大写开头,它将被忽略。这里有一个例子。