6

I have taken values from a MySQL query into an array so that I can sort them. It all works well except I am getting lots of errors

 Warning: Creating default object from empty value 

This is so that I can sort the array based on post_id and quality

$count = 0;
$searcharticles = new stdClass();
$postid = 0;
while ($row = mysqli_fetch_object($result1) ) {

    $postid     = $row->ID;
    $title      = $row->post_title;
    $titlescore = $row->titlescore;
    $quality    = $row->quality;

    $searcharticles->$postid->post_id =$postid;
    $searcharticles->$postid->title =$title;
    $searcharticles->$postid->titlescore = $titlescore;
    $searcharticles->$postid->quality =$quality;

$count ++;
} 

Some posts on Stackexchange indicated that I should add in $searcharticles = new stdClass();

But this has not solved the problem

I am looking to remove the errors and not just suppress the warnings.

Any ideas?

10
  • 2
    Possible duplicate of Creating default object from empty value in PHP? Commented Jan 8, 2016 at 17:44
  • No I tried that first. That is whi I tried adding the $searcharticles = new stdClass(); and $postid = new stdClass(); lines to the code. Commented Jan 8, 2016 at 17:47
  • Yes $postid is an integer.I have changed the code to $postid=0; but the error is the same Commented Jan 8, 2016 at 17:54
  • What type of structure are you trying to create? Creating object properties named by the integer $postid is confusing. If $searcharticles is to be a collection, it should be an array of objects, such that you are creating $searcharticles[$postid]->title = $title for example. Commented Jan 8, 2016 at 17:56
  • ... Which then requires that you initialize each array item to a new stdClass first: $searcharticles[$postid] = new stdClass(); and instead of creating $searcharticles as a stdClass, create it as an array(). Commented Jan 8, 2016 at 17:57

2 Answers 2

10

You get the warning

Warning: Creating default object from empty value

because you do

$searcharticles->$postid->post_id = $postid;

Your $searcharticles is a StdClass. You assign the property $postId and then immediately chain off another property post_id, which forces PHP to create a new default object.

To avoid it, put this before the chained method calls:

$searcharticles->$postid = new StdClass;
$searcharticles->$postid->post_id =$postid;
$searcharticles->$postid->title =$title;
$searcharticles->$postid->titlescore = $titlescore;
$searcharticles->$postid->quality =$quality;
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe your problem is not $searcharticles, could be $row.

Try

$searcharticles = new stdClass; AND $row = new stdClass;

Good luck!

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.