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?

Jonty5817
  • 335
  • 2
  • 3
  • 13
  • 2
    Possible duplicate of [Creating default object from empty value in PHP?](http://stackoverflow.com/questions/8900701/creating-default-object-from-empty-value-in-php) – summea Jan 08 '16 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. – Jonty5817 Jan 08 '16 at 17:47
  • Yes $postid is an integer.I have changed the code to $postid=0; but the error is the same – Jonty5817 Jan 08 '16 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. – Michael Berkowski Jan 08 '16 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()`. – Michael Berkowski Jan 08 '16 at 17:57
  • I am trying to combine 4 different MySQL queries in a separate array with postid as the common identifier between the queries. I then calculate a weighted average score based on the 4 queries and then sort on the combined score. It works well but is throwing these errors. – Jonty5817 Jan 08 '16 at 18:06
  • The problem is the multiple chaining. Do `$searcharticles->$postid = new StdClass` before you do any `$searcharticles->$postid->foo = "bar"` – Gordon Jan 08 '16 at 18:06
  • @Jonty5817 Is the query you have above expected to produce multiple results? If so, make `$searcharticles` an array _indexed_ by `$postid` as in my comment. – Michael Berkowski Jan 08 '16 at 18:10
  • Gordon that solved the problem. Many thanks. – Jonty5817 Jan 08 '16 at 18:10
  • @Michael Berkowski. I think that that is a good suggestion for improving my object structure. – Jonty5817 Jan 08 '16 at 18:13

2 Answers2

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;
Gordon
  • 312,688
  • 75
  • 539
  • 559
0

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

Try

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

Good luck!

Kike Gamboa
  • 994
  • 7
  • 8