Jump to content

[Solved] Array of objects - accessing members using for?


TomOlsson
 Share

Recommended Posts

Hello, I'm having a slight issue, which I'm not sure why I'm having. I'm basically retrieveing an array of objects from a database, and first modifying a parameter of each object using a for-each loop, then sorting them using a for-loop based on the modified parameter before publishing them. Or well, that is the plan except I can't access the objects properly during the for-loop. The code which is not working is this:

for($i=0; $i<($len); $i++){  echo 'Innan if </br>';  echo 'Result['.$i.']='.$result[i]->hits.'</br>Result['.($i+1).']='.$result[i+1]->hits.'</br>';  //First ->hits value is always 1, seconds is null.  if (($result[i]->hits) < ($result[i+1]->hits)){	echo 'efter IF';  //Reached only once. Original array has lowest-highest (roughly) so this should fire dozens of times	$dummy = $result[i]->hits;	$result[i]->hits = $result[i+1]->hits;	$result[i+1]->hits=$dummy;	$bool = true;	echo $result[i]->hits.'</br>';  }}

Well, it sort of works. It loops and goes around, the if is true once. But it's not drawing it's values from the objects inside the array. As I've commented inside the code, it's not throwing errors, but it's not doing what it should do either. I'm assuming that this has to do with my array containing objects, so I need add another step in here to get the object on each location in the array... Maybe. Any ideas? Using a foreach solves the value issue, but it's obviously sketchy to use it during a bubblesort. Can post full code if relevant, though this is the only part which is not working the way I expect it to. The output is this:

Result[0]=Result[1]=1efter IF1Innan ifResult[1]=1Result[2]=Innan ifResult[2]=1Result[3]=Innan ifResult[3]=1Result[4]=Innan ifResult[4]=1Result[5]=Innan ifResult[5]=1Result[6]=Innan ifResult[6]=1Result[7]=Innan ifResult[7]=1Result[8]=Innan ifResult[8]=1Result[9]=Innan ifResult[9]=1Result[10]=Innan ifResult[10]=1Result[11]=Innan forInnan ifResult[0]=1Result[1]=Innan ifResult[1]=1Result[2]=Innan ifResult[2]=1Result[3]=Innan ifResult[3]=1Result[4]=Innan ifResult[4]=1Result[5]=Innan ifResult[5]=1Result[6]=Innan ifResult[6]=1Result[7]=Innan ifResult[7]=1Result[8]=Innan ifResult[8]=1Result[9]=Innan ifResult[9]=1Result[10]=Innan ifResult[10]=1Result[11]=

Edited by TomOlsson
Link to comment
Share on other sites

You can use a built-in sort function to sort that array, like usort, but if you want to figure that one out the first step would be to verify your data. You can use var_dump to print out the entire array and the objects to make sure everything is set to what you think it is before looping: var_dump($result);

Link to comment
Share on other sites

Guest So Called

Bugs are always that way. They hide in with the good code so you don't see them, like a tree hiding inside a forest.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...