-2

This is json data i received:

{"id":"8","cardnum":"5678887","point_collected":"26","date":"2015-05-06"}

{"id":"15","cardnum":"3435435","point_collected":"20","date":"2015-05-04"}

{"id":"11","cardnum":"5678887","point_collected":"50","date":"2015-05-03"}

{"id":"12","cardnum":"5678887","point_collected":"80","date":"2015-05-02"}

{"id":"14","cardnum":"5678887","point_collected":"10","date":"2015-05-02"}

I want to get the "cardnum" for comparison with my $usercard, and display all the result with the same "cardnum" and encode into json again.

This is my current code:

 $decodeTrans=json_decode($transJson, true);

        $response = array();

        $usercard = "5678887";

       foreach ($decodeTrans as $dt)
                {      
                    $membercard = $dt['cardnum'];

                    if ($membercard == $usercard)
                    {

                       $response["success"] = 1;
                       $response ['id'] = $dt['id'];
                       $response ['card_number'] = $membercard;
                       $response ['point_collected'] = $dt['point']; 
                       $response ['date'] = $dt['date'];

                       echo json_encode($response);

                    }
                }  

    }

}

I get this error: Invalid argument supplied for foreach() Anyone can help? Thanks in advance.

3
  • 1
    if you're getting that literal JSON string then its an invalid one, thats why after decode and feeding it into the foreach, it spewed the error Commented May 11, 2015 at 5:03
  • What you have posted is not a URL, nor is it valid JSON. Commented May 11, 2015 at 5:04
  • stackoverflow.com/questions/16634016/get-value-from-json-string Commented May 11, 2015 at 5:10

1 Answer 1

0

Your input is not a valid JSON string. However, each line is still a valid JSON string. Update your code to split the $transJson and convert each line to PHP object using json_decode:

$response = array();
$usercard = "5678887";
foreach(explode("\n", $transJson) as $line)
{
    $dt = json_decode(trim($line), true);
    $membercard = $dt['cardnum'];

    if ($membercard == $usercard)
    {

       $response["success"] = 1;
       $response ['id'] = $dt['id'];
       $response ['card_number'] = $membercard;
       $response ['point_collected'] = $dt['point_collected']; 
       $response ['date'] = $dt['date'];

       echo json_encode($response);

    }
}

The demo: http://sandbox.onlinephpfunctions.com/code/3335a3fa9edb5a3b8d7e73fd85d4bad806e5ac92

Sign up to request clarification or add additional context in comments.

2 Comments

this is not working. I get a blank page after using this code. what is the problem?
There is an error in your original code, the $dt['point'] is not set, I have update the code and use $dt['point_collected'] instead. You can view the result in the link at the end of my answer now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.