Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅输出基于初始字符的数组的一部分_Php_Multidimensional Array - Fatal编程技术网

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";

请举例说明您希望输出的样子?请举例说明您希望输出的样子?注意,如果上面的键以大写字母开头,它将被忽略。注意,如果上面的键以大写开头,它将被忽略。这里有一个例子。