CodeIgniter Forums

Full Version: unset value from object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Jeroen Brussich[/eluser]
How do I remove a certain value from an object?

I'm writing a library program for my highschool. I'm currently working on the part where you can assign a student to a book. You search for a student (candidates) and the view returns only those students (candidates) that match your search. I'm trying to filter out those matches that already have the book in their possession (loaners).

I thought that matching user_ids would be a good way to do this. Pass the loaners (those who already have the book in their possession) and the candidates (those who match the search) to a method. If the user_ids match, you got a double and you remove that double from the object candidates.

Then you return the candidates and you display them.

Everything seems to work, right up to the point where I try to delete the candidate from the object candidates. It just doesn't do anything...

So: How do I remove a certain value from an object?
Code:
public function remove_doubles($loaners, $candidates)
{
// build an array with the user_ids
        $haystack = array();
        foreach($loaners->result() as $loaner)
        {
            $haystack[]    = $loaner->id;
        }

//look if the user_id from the candidate is in the already_loaned_haystack
        foreach($candidates->result() as $candidate)
        {
            $needle    = $candidate->id;
            if(in_array($needle, $haystack))
            {
//If so, remove the candidate from the object.
                unset($candidate) // FAILS!
            }
        }
}

El Forum

[eluser]Isern Palaus[/eluser]
Hello,

Too unset a value from object:
Code:
unset($item->value);

The problem is, if I remember, that you can't unset a object if you haven't unset all the values. If someone can correct me.

Regards,
Isern

El Forum

[eluser]Jeroen Brussich[/eluser]
When I try this code, I get a PHP ERROR.
Code:
//look if the user_id from the candidate is in the already_loaned_haystack
        foreach($candidates->result() as $candidate)
        {
            $needle    = $candidate->id;
            if(in_array($needle, $haystack))
            {
//If so, remove the candidate from the object.
                unset($candidates->$candidate) // PHP ERROR
            }
        }
Quote:A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: models/leerlingen.php
Line Number: 66

El Forum

[eluser]jedd[/eluser]
I think you are confused about objects. Certainly, reading your code makes me even more confused about the suckers.

Quote:
Code:
foreach($candidates->result() as $candidate)
{
    $needle    = $candidate->id;
    if(in_array($needle, $haystack))
    {
        unset($candidates->$candidate) // PHP ERROR
    }
}

$candidate is a copy of one instance of $candidates->result() - I don't think it makes sense to try to refer to $candidates->$candidate

It might be instructive to put print_r()'s or var_dump()'s throughout your application, so you can see the nature of the variables that you're using.

El Forum

[eluser]Jeroen Brussich[/eluser]
I removed all my print_r() and var_dump() in that little method, but let's say I wrote them about 10 times in there Wink

I am indeed rather green on the concept of objects but now I understand I do delete the copy of the instance and not the instance itself.
Code:
foreach($candidates->result() as $candidate)
        {
            $needle    = $candidate->id;
            if(in_array($needle, $haystack))
            {
                unset($candidate);
            }
            print_r($candidate); // gives Undefined variable: candidate, like it should be!
        }

        echo '<pre>';
        var_dump($candidates); // Still prints out all the candidates with doubles!
        echo '</pre>';

But this does not solve my initial problem. How do I remove that particular instance from my object instead of the copy? Big Grin

El Forum

[eluser]jedd[/eluser]
Jeroen, understand that I'm an object-avoider .. so take this advice with a large pinch of salt (it's how I do this kind of stuff with arrays).

Go play with it, and if it doesn't work don't panic, as someone much smarter than me will be along in a few minutes anyway.

I suspect(!) that much of your frustration will be solved if you change this line:
Code:
foreach($candidates->result() as $candidate)

To this:
Code:
foreach($candidates->result() as $candidate_id => $candidate_info)

Note that I'm kind of making a few assumptions about the nature of your $candidates object, or rather the layout of your result array or object within your $candidates object.

You may want to give a summarised output of a print_r of $candidates so the next person along can help you faster.

El Forum

[eluser]Jeroen Brussich[/eluser]
I found a imho dirty solution, but I still would like somebody to have a look, point his finger and laugh at me and then provide me with the right code. :p
Code:
public function remove_doubles($candidates, $loaners)
    {
// build an array with the user_ids
        $haystack = array();
        foreach($loaners->result() as $loaner)
        {
            $haystack[]    = $loaner->id;
        }

//look if the user_id from the candidate is in the already_loaned_haystack
//iterate i
        $i = 0;
        foreach($candidates->result() as $candidate)
        {
            $needle    = $candidate->id;
            if(in_array($needle, $haystack))
            {
// DIRTY!!!
// hack into the object and force unset.
// manually reset num_rows
// do not user ->num_rows() after this, because it would return the number of rows of the initial object, not the hacked object.
                unset($candidates->result_object[0]);
                $candidates->num_rows = $candidates->num_rows -1;
            }
            $i++;
        }


// return hacked object or FALSE
        if($candidates->num_rows != 0) // do not use ->num_rows() but ->num_rows
        {
            sort($candidates->result_object);
            return $candidates;
        }
        return FALSE;
    }