2

I want to parse json string inside a loop . I tried using the following code but the parsed data never prints out inside the loop! could any one tell me what i am doing wrong here ? Thanks in advance.

$code2 sample data:

{ p '5123': {
            p 'tmp': p '1', p 'name': p 'mango', p 'abc': p 'abcd4 http://mysite/items/1234', p 'number': p '1123', p 'itemCategory_title': p 'fruits', p 'logo': p '2123.png', p 'itemCategory_id': p '90'
        }, p '700': {
            p 'tmp': p '0', p 'name': p 'cherry', p 'abc': p 'abcd4 http://mysite/items/1235', p 'number': p '1124', p 'itemCategory_title': p 'fruits', p 'logo': p '2124.png', p 'itemCategory_id': p '91'
        } }

$clean_str data:

{ '5123': {
            'tmp': '1', 'name': 'mango', 'abc': 'abcd4 http://mysite/items/1234', 'number': '1123', 'itemCategory_title': 'fruits', 'logo': '2123.png', 'itemCategory_id': '90'
        }, '700': {
            'tmp': '0', 'name': 'cherry', 'abc': 'abcd4 http://mysite/items/1235', 'number': '1124', 'itemCategory_title': 'fruits', 'logo': '2124.png', 'itemCategory_id': '91'
        } }

php code:

<?
$code2 = stripslashes($_POST['outputtext']);
$clean_str = str_replace("p '","'",$code2);

$data = json_decode($clean_str);

foreach( $data as $item ) {
  echo $item['tmp'];
  echo $item['name'];
  echo $item['abc'];
  echo $item['number'];
  echo $item['itemCategory_title'];
  echo $item['log'];
  echo $item['itemCategory_id'];    

?>
<a href="./process.php?tmp=<?php  echo $item['tmp'] ; ?>&name=<?php  echo $item['name']; ?>&abc=<?php  echo $item['abc'] ; ?>&itemCategory_title=<?php  echo $item['itemCategory_title'] ; ?>&log=<?php  echo $item['log'] ; ?>&itemCategory_id=<?php  echo $item['itemCategory_id'] ; ?>"><?php  echo $item['itemCategory_title'] ; ?>test </a> <br />
<?

}

?>
3
  • Possible duplicate of How to parse data from a big string using php? Commented Nov 19, 2015 at 13:46
  • 1
    To get array with json_decode add second param TRUE ($string, TRUE).. or you are going to catch object not array.. Commented Nov 19, 2015 at 13:47
  • Thanks for reply. I changed it to $data = json_decode($clean_str, true); but still did not parse the json string inside loop! Commented Nov 19, 2015 at 13:52

1 Answer 1

1

To get a result as an array you should:

$data = json_decode($clean_str, true);

JSON uses double quotes " not single '

<?php

$code2 = stripslashes($_POST['outputtext']);

$clean_str = str_replace("p '","'",$code2);
$clean_str = str_replace('\'', '"', $clean_str);

$data = json_decode($clean_str, true);

foreach( $data as $item ) {
  echo $item['tmp'];
  echo $item['name'];
  echo $item['abc'];
  echo $item['number'];
  echo $item['itemCategory_title'];
  echo $item['log'];
  echo $item['itemCategory_id'];    

?>
<a href="./process.php?tmp=<?php  echo $item['tmp'] ; ?>&name=<?php  echo $item['name']; ?>&abc=<?php  echo $item['abc'] ; ?>&itemCategory_title=<?php  echo $item['itemCategory_title'] ; ?>&log=<?php  echo $item['log'] ; ?>&itemCategory_id=<?php  echo $item['itemCategory_id'] ; ?>"><?php  echo $item['itemCategory_title'] ; ?>test </a> <br />
<?

}

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

5 Comments

Thanks for reply . I tried that but still didn't parse the json string!could you tell me if the json string has the correct format ?
Json uses double quotes " not single ' :)
@faressoftThe data is from an api response so i have no control over how to construct it! So how i can change all single quotes back to double quotes?i tried this but didn't wrok!$clean_str = str_replace("'",""",$code2);
$clean_str = str_replace('\'', '"', $clean_str);
thanks your solution worked and also this one fixed the problem: $clean_str = str_replace("'","\"",$code2); $clean_str = str_replace("p ","",$clean_str);

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.