简体   繁体   English

遍历多维数组中的数组

[英]Loop through array within a multidimensional array

Here is my function that retrieves an array of paragraphs: 这是我的获取段落数组的函数:

    function first_paragraph() {
      global $post, $posts;
      $first_para = '';
      ob_start();
      ob_end_clean();
      $post_content = $post->post_content;
      $post_content = apply_filters('the_content', $post_content);
      $output = preg_match_all('%(<p[^>]*>.*?</p>)%i', $post_content, $matches);
      $first_para = $matches[0][0];
      print_r($matches);
}

Which results in the following array: 结果为以下数组:

(
    [0] => Array
        (
            [0] => <p>I am not in any category.</p>
            [1] => <p>Second paragraph.</p>
            [2] => <p>Third paragraph</p>
            [3] => <p>Fourth paragraph</p>
        )

    [1] => Array
        (
            [0] => <p>I am not in any category.</p>
            [1] => <p>Second paragraph.</p>
            [2] => <p>Third paragraph</p>
            [3] => <p>Fourth paragraph</p>
        )

)

Is it possible to loop through only one of these arrays, rather than both? 是否可以仅遍历这些数组之一而不是全部遍历? I am new to PHP, so any guidance or resources would be appreciated. 我是PHP新手,所以任何指导或资源将不胜感激。

PS: I'm not sure why preg_match_all returns two arrays, maybe someone can shed some light on that? PS:我不确定为什么preg_match_all返回两个数组,也许有人可以对此有所了解?

Yes you can doing this : 是的,您可以这样做:

foreach ($matches as $key => $paragraph){
    if( $key == 0 ) {
        // Here you can use the value of $paragraph
        // $paragraph[0] contains "<p>I am not in any category.</p>"
        // $paragraph[1] contains "<p>Second paragraph.</p>"
        etc...
    }
}

I assume, that your post data looks like that: 我假设您的帖子数据如下所示:

<p>I am not in any category.</p>
<p>Second paragraph.</p>
<p>Third paragraph</p>
<p>Fourth paragraph</p>

So, according to PHP.net, by default $flags parameter is equal to PREG_PATTERN_ORDER , so "Orders results so that $matches[0] is an array of full pattern matches , $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on." 因此,根据PHP.net,默认情况下,$ flags参数等于PREG_PATTERN_ORDER ,因此“对结果进行排序 ,以使$ matches [0]是一个完整模式匹配的数组 ,$ matches [1]是一个由字符串匹配的字符串数组第一个带括号的子模式,依此类推。”

So, for example, if you will change your patter to %<p[^>]*>(.*?)</p>%i , you woll get somthing like that: 因此,例如,如果您将模式更改为%<p[^>]*>(.*?)</p>%i ,那么您会大声疾呼:

(
    [0] => Array
        (
            [0] => <p>I am not in any category.</p>
            [1] => <p>Second paragraph.</p>
            [2] => <p>Third paragraph</p>
            [3] => <p>Fourth paragraph</p>
        )

    [1] => Array
        (
            [0] => I am not in any category.
            [1] => Second paragraph.
            [2] => Third paragraph
            [3] => Fourth paragraph
        )

)

So, if you need only subpatterns, you have to loop only throw subpattern results array. 因此,如果只需要子模式,则必须循环仅抛出子模式结果数组。 I assume that in your case it will be the second one ($matches[1]). 我假设您的情况将是第二个($ matches [1])。

Read more about preg_match_all at PHP.net: http://php.net/manual/en/function.preg-match-all.php 在PHP.net上了解有关preg_match_all的更多信息: http : //php.net/manual/en/function.preg-match-all.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM