0

Here is the code:

<? $fields = json_decode($this->item->extra_fields); 
$allowed_fields= array('3','5');  
if ( $fields != NULL ) { 
foreach ( $fields as $field ) { 
if ( in_array($field->id, $allowed_fields ) && $field->value != NULL) {  
?>
<span class = "list_for<? echo $field->id; ?>">
<? echo $field->value;?></span><? }   } }?>

When I change json_decode to json_encode I get the following error:

Warning: Invalid argument supplied for foreach()...
----------

Everything worked before the upgrade of PHP to 5.4 from 5.3.

I am not a developer so please try to not be too technical. Thanks in advance!

2
  • it already says in the error message, feed it with a json string, but instead you fed it with an array Commented Sep 9, 2014 at 12:08
  • $fields is not an array. Commented Sep 9, 2014 at 12:11

5 Answers 5

2

as stated in the manual, do it like this

$fields = json_decode($this->item->extra_fields, true); 

When TRUE, returned objects will be converted into associative arrays.

$fields will be an array and the foreach() will work

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

4 Comments

Was just about to answer this myself, good answer and should keep the rest of the code working
inside the foreach he uses field as an object, he will have to rewrite it if changing to an assoc array.
I added the true bit, but I still get the original error message.
exactly what is in '$this->item->extra_fields', what do you get when you execute var_dump($fields); ?
1

Test this. before sending to PHP change your jsonObj to string.

jsonObj{..}

jsonObj2=JSON.stringify(jsonObj);
$.ajax({
   type: "POST",
   url:"Methods.php",
   data : { 'Json_data':jsonObj2},
   success: function(response) {
    console.log("Result:" + response);
   }
});

Comments

0

Note that json_decode accepts string and according to the above error you are trying to pass an array to it so if you need to iterate on the fields array you may want to ignore json_decode as it seems that you already have an array.

json_encode accepts array but it gives a string so it give you a warning when you try to iterate on sting

so try the below code.

<? $fields = $this->item->extra_fields; 
$allowed_fields= array('3','5');  
if ( $fields ) { 
foreach ( $fields as $field ) { 
if ( in_array($field->id, $allowed_fields ) && $field->value != NULL) {  
?>
<span class = "list_for<? echo $field->id; ?>">
<? echo $field->value;?></span><? }   } }?>

Comments

0

You can't check if its NULL.

<? $fields = json_decode($this->item->extra_fields); 
$allowed_fields= array('3','5');  
if($fields) {
foreach ( $fields as $field ) { 
if ( in_array($field->id, $allowed_fields ) && $field->value != NULL) {  
?>
<span class = "list_for<? echo $field->id; ?>">
<? echo $field->value;?></span><? }   } }?>

Try if($fields) {

You get true if he can decode the json string.

Comments

0
<?php 
$fields = json_decode($this->item->extra_fields); 
$allowed_fields= array('3','5');  
if ( !empty($fields) ) { 
  foreach ( $fields as $key => $field ) { 
    if ( in_array($field->id, $allowed_fields ) && !empty($field->value)) {  
   ?>
      <span class = "list_for<?php echo $field->id; ?>">
      <?php echo $field->value;?></span>
  <?php 
  }   
 } 
} 
?>

Please copy paste this code and check that error remove or not. You can't check the $fields with NULL.So you have to check it only by using !empty($fields)

Comments

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.