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

  • Home
  • SEARCH
  • 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 6708463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:45:45+00:00 2026-05-26T07:45:45+00:00

Test in some json code, like [{a:1},{a:2},{a:3},{b:2}] , I want use json decode, get

  • 0

Test in some json code, like [{"a":"1"},{"a":"2"},{"a":"3"},{"b":"2"}], I want use json decode, get the data and make a judge, if first a nod is == b nod or fisrt a nod is != b nod. some code here.

<?php
header("Content-type: text/html; charset=utf-8");
$json = json_decode('[{"a":"1"},{"a":"2"},{"a":"3"},{"b":"2"}]',true);
$number=1;
foreach($json as $num){
if($num['a']!=$num['b']){
    if($num['a']){
        echo 'a'.$number.''.$num['a'].'<br />'; 
    }
}else{
    if($num['a']){
        echo 'b'.$number.''.$num['a'].'<br />'; 
    }
}
$number++;
}
?>

Now my result is:

a11 
a22 
a33

And I need get the result:

a11
b22
a33
  • 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-05-26T07:45:46+00:00Added an answer on May 26, 2026 at 7:45 am

    How can you compare non-existing array fields?

    there is no $num[‘b’] field, there are these fields, its array[4] (index 0..3)

    • a:1
    • a:2
    • a:3
    • b:2

    $num in first iteration will hold

    array("a"=>1);
    

    so if you wanna compare to "b":2 you have to use this pattern:

    $cmpr = array_shift($json);
    ...
    if($num != $cmpr)
    

    Array_shift : http://php.net/manual/en/function.array-shift.php

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

Sidebar

Related Questions

I'm using the following code to get some json formatted data: $.ajax({ type: GET,
I want to extensively test some pieces of C code for memory leaks. On
I'm trying to use jQuery.post() function to retrieve some data. But i get no
i have this code for sending data var test={imagename:apple.jpg,x:13,y:33}; $.ajax({ type: POST, url: some.php,
I want to test some PHP on my local machine running Windows XP Professional.
I am trying to test some of these code here http://ha.ckers.org/xss.html on my code.
I would like to test some exception handling logic in the empty catch block
I'd like to stress test some of my SQL queries and find out about
I've found myself unsatisfied with Django's ability to render JSON data. If I use
I would like to test out some interviewees by having them write some javascript

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.