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 8421581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:11:16+00:00 2026-06-10T03:11:16+00:00

Some easy code, If I have a json data. I want do somethings, first

  • 0

Some easy code, If I have a json data. I want do somethings, first check the match string in the json data, if have, output the value after the match line, else output all the json data.

Exapmle 1, match string is 9, match in the json data, output the value after the match line 7, 3.

$txt = '[{"a":"5"},{"a":"9"},{"a":"7"},{"a":"3"}]';
$array = json_decode($txt);
$match_string = '9';
foreach ($array as $data){      
    echo $data->a;//7, 3
}

Exapmle 2, match string is 2, not match in the json data, output all the value, 5, 9, 7, 3.

$txt = '[{"a":"5"},{"a":"9"},{"a":"7"},{"a":"3"}]';
$array = json_decode($txt);
$match_string = '2';
foreach ($array as $data){      
    echo $data->a;//5, 9, 7, 3
}

How to do this judgment? I do something like in the foreach, just ignore the match string:

if($match_string == $data->a){
  continue;//fut this in the foreach ,get 5, 7, 3, but I need 7, 3, next value from 9.
}

Thanks.

  • 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-10T03:11:17+00:00Added an answer on June 10, 2026 at 3:11 am

    You need to set a flag telling you whether or not you’ve found a match:

    $txt = '[{"a":"5"},{"a":"9"},{"a":"7"},{"a":"3"}]';
    $array = json_decode($txt);
    $match_string = "2";
    $found = false;
    foreach ($array as $data) {
        if ($found) {
            echo $data->a;
        } else if ($data->a === $match_string) {
            // If we set $found *after* we have the opportunity to display it,
            // we'll have to wait until the next pass.
            $found = true;
        }
    }
    if (!$found) {
        // Display everything
        foreach ($array as $data) {
            echo $data->a;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this might be very easy to some,, I have a simple string
I'm pretty sure there's some quick and easy error in this code but somehow
Some easy points for somebody here, take the following table of data: EventCode ProcessId
This supposedly easy task gave me some headache. I simply want to let the
First I want to thank you guys here, because your posts have gotten me
Both ideone.com and codepad.org have Little-Endian architechtures. I want to test my code on
I have a rest service returning some data. I use Restlet client api as
Here's some easy reputation for someone. The scenario I have a table that is
I have seen there are many JSON parsers in Java. Some target speed, some
In a ajax-driven site I have added some default data using the ajaxSetup, ala

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.