1

I have a variable in PHP, named as $partialSegments. I want to iterate through it and perform actions when I strike operator or rOperandValue, etc. I receive it in a function call, so while making the

function named(say):
 public function convertJsCode($partialSegments){
//logic
}

I am struggling with the logic

"segments":{"type":"custom","partialSegments":[{"type":"7","operator":11,"rOperandValue":["windows xp"],"prevLogicalOperator":null},{"type":"8","operator":11,"rOperandValue":["other"],"prevLogicalOperator":"AND"}]}}
8
  • Where You get this. Add Some Description Commented Jan 11, 2018 at 5:32
  • json_decode should help you to transform it to arrays/objects. Commented Jan 11, 2018 at 5:33
  • @TarangP sorry, I have edited the question. Please have a look. Commented Jan 11, 2018 at 5:35
  • @jh1711 Okay I will have a look However, is there a chance that this data can be stored in a PHP variable? If yes, how would I iterate through it? Commented Jan 11, 2018 at 5:36
  • title should be: How to read json encoded value using php? and you should add json tag. Your title does not make sense. Commented Jan 11, 2018 at 6:29

2 Answers 2

2

Use json_decode() function in php

  <?php

$json = '{"segments":{"type":"custom","partialSegments":[{"type":"7","operator":11,"rOperandValue":["windows xp"],"prevLogicalOperator":null},{"type":"8","operator":11,"rOperandValue":["other"],"prevLogicalOperator":"AND"}]}}';

$parsed = json_decode($json, true);

foreach($parsed["segments"]["partialSegments"] as $val) {


    $operator = $val["operator"]; // operator value 
    $rOperandValue = $val["rOperandValue"][0]; // rOperandValue value 

}

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

2 Comments

And how can I iterate through it? I have used the function json_decode($partialSegments, true) Now when I print the decoded value, this is the answer: I am getting the same json printed while print_r(), my doubt is how can i iterate through it?
[stackoverflow.com/users/6023296/aman-kumar](Aman Kumar) I got what you are saying. Sorry for not getting it at first and thanks for your time.
0

Step1: convert json encoded data into php array using json_decode(); function. This function receive two arguments: first one is: json_encoded data which should be decoded and 2nd one is: boolean(TRUE or FALSE). pass second argument as TRUE to convert the json concoded values as php array, if you pass false it will return php object.

Step2: iterate php array and set the logic like a charm.

<?php

$json = '{"segments":{"type":"custom","partialSegments":[{"type":"7","operator":11,"rOperandValue":["windows xp"],"prevLogicalOperator":null},{"type":"8","operator":11,"rOperandValue":["other"],"prevLogicalOperator":"AND"}]}}';

$parsed = json_decode($json, true);

foreach($parsed as $single){
  $segments = $single['partialSegments'];
  //print_r($single);

  foreach($segments as $segment){
      echo 'operator:'. $segment['operator'].'<br>';
            print_r($segment['rOperandValue']);
      // put your logic based on 'operator' or 'rOperandValue'
    }

}

?>

Suggestion: Don't use hard code index to work with an array like: $segment['rOperandValue'][0]. array could be empty. i.e: if you want to do anything when a value is found in $segment['rOperandValue'] use in_array().

1 Comment

Yeah, I avoided using the hard coded value. Thank you!

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.