Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8832791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:37:03+00:00 2026-06-14T08:37:03+00:00

I have preg_match_all function: preg_match_all(‘#<h2>(.*?)</h2>#is’, $source, $output, PREG_SET_ORDER); It’s working as intended, BUT the

  • 0

I have preg_match_all function:

preg_match_all('#<h2>(.*?)</h2>#is', $source, $output, PREG_SET_ORDER);

It’s working as intended, BUT the problem is, it preg_matches all items twice and into a huge multi dimensional array like this for example where it, as intended, preg_matched all 11 items needed, but twice and into a multidimensional array:

Array
(
    [0] => Array
        (
            [0] => <h2>10. <em>Cruel</em> by St. Vincent</h2>
            [1] => 10. <em>Cruel</em> by St. Vincent
        )

    [1] => Array
        (
            [0] => <h2>9. <em>Robot Rock</em> by Daft Punk</h2>
            [1] => 9. <em>Robot Rock</em> by Daft Punk
        )

    [2] => Array
        (
            [0] => <h2>8. <em>Seven Nation Army</em> by the White Stripes</h2>
            [1] => 8. <em>Seven Nation Army</em> by the White Stripes
        )

    [3] => Array
        (
            [0] => <h2>7. <em>Do You Want To</em> by Franz Ferdinand</h2>
            [1] => 7. <em>Do You Want To</em> by Franz Ferdinand
        )

    [4] => Array
        (
            [0] => <h2>6. <em>Teenage Dream</em> by Katie Perry</h2>
            [1] => 6. <em>Teenage Dream</em> by Katie Perry
        )

    [5] => Array
        (
            [0] => <h2>5. <em>Crazy</em> by Gnarls Barkley</h2>
            [1] => 5. <em>Crazy</em> by Gnarls Barkley
        )

    [6] => Array
        (
            [0] => <h2>4. <em>Kids</em> by MGMT</h2>
            [1] => 4. <em>Kids</em> by MGMT
        )

    [7] => Array
        (
            [0] => <h2>3. <em>Bad Romance</em> by Lady Gaga</h2>
            [1] => 3. <em>Bad Romance</em> by Lady Gaga
        )

    [8] => Array
        (
            [0] => <h2>2. <em>Pumped Up Kicks</em> by Foster the People</h2>
            [1] => 2. <em>Pumped Up Kicks</em> by Foster the People
        )

    [9] => Array
        (
            [0] => <h2>1. <em>Paradise</em> by Coldplay</h2>
            [1] => 1. <em>Paradise</em> by Coldplay
        )

    [10] => Array
        (
            [0] => <h2>Song That Get Stuck In Your Head YouTube Playlist</h2>
            [1] => Song That Get Stuck In Your Head YouTube Playlist
        )

)

How to convert this array into simple one and without those duplicated items? Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:37:04+00:00Added an answer on June 14, 2026 at 8:37 am

    You will always get a multidimensional array back, however, you can get close to what you want like this:

    if (preg_match_all('#<h2>(.*?)</h2>#is', $source, $output, PREG_PATTERN_ORDER))
        $matches = $output[0]; // reduce the multi-dimensional array to the array of full matches only
    

    And if you don’t want the submatch at all, then use a non-capturing grouping:

    if (preg_match_all('#<h2>(?:.*?)</h2>#is', $source, $output, PREG_PATTERN_ORDER))
        $matches = $output[0]; // reduce the multi-dimensional array to the array of full matches only
    

    Note that this call to preg_match_all is using PREG_PATTERN_ORDER instead of PREG_SET_ORDER:

    PREG_PATTERN_ORDER 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.

    PREG_SET_ORDER Orders results so that $matches[0] is an array of first
    set of matches, $matches[1] is an array of second set of matches, and
    so on.

    See: http://php.net/manual/en/function.preg-match-all.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update Yahoo error Ok, so I got it all working, but the preg_match_all wont
I have a problem with preg_match_all that maybe you can help. I have a
So... I have a problem with counting all ccurrences of preg_match and summing/addition first
I have the following code: preg_match_all('/(.*) \((\d+)\) - ([\d\.\d]+)[,?]/U', E-Book What I Didn't Learn
I need help with preg_match function in php. I have read a lot of
I have some HTML that looks like this: $(document).ready(function(){ $('#cumulative-returns').graph({ width: 400, height: 180,
i'm not sure how i could have phrased the title better, but my issue
Here's the problem I have, I'm trying to separate News by categories, I have
Consider that i have the string, $string = 'tag2 display=users limit=5'; Using the preg_match_all
function all_images(&$post){ $content = $post->post_content; if(preg_match_all('/<img[^>]+src=(.*?)[^>]*>/', $content, $results)){ $i = 0; $count = count($results);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.