3

The heading might be a bit confusing because I'm not quite sure how to describe my 'array'. Here's how it looks when I use print_r...

stdClass Object
(
[0] => stdClass Object
    (
        [Name] => Claude Bemrose
        [Skill] => 7
        [Age] => 14
        [AgeWeeks] => 11
        [ChanceOfLeaving] => 12
    )

[1] => stdClass Object
    (
        [Name] => Willy Gearon
        [Skill] => 7
        [Age] => 12
        [AgeWeeks] => 27
        [ChanceOfLeaving] => 8
    )

[2] => stdClass Object
    (
        [Name] => Kevin Broderick
        [Skill] => 9
        [Age] => 13
        [AgeWeeks] => 21
        [ChanceOfLeaving] => 12
    )

It sort of looks like an object with an array of objects.

I want to remove an element (e.g the whole of [1]), but if I try to use unset, e.g unset($this->U16sArray[$arrayindex]) I get ...

Fatal error: Cannot use object of type stdClass as array.

I'm still very confused as to how or why my 'array' comes out this way in the first place. But I'm happy using it this way, so long as I can remove on element.

A bit more information as requested. It's fetched using PDO from the database with ..

    try {
        $query = $db->prepare("SELECT * FROM Teams WHERE ID = :TeamID");
        $query->bindValue(':TeamID', $ID, PDO::PARAM_INT);
        $query->setFetchMode(PDO::FETCH_INTO, $this);
        $query->execute();
        $query->fetch();    
    }

It's part of a much larger object.

And then decoded from JSON with

$this->U16sArray = json_decode($this->U16sJSON);

EDIT - Update.

I'm slowly tracking down the problem. Basically, it's all working fine, until I use the unset function, at which point, something is altered, saved and then when I reload it, it starts throwing errors. Presumably it's changed from one type of array to another or something.

For example, before using the unset function on my data, the data in my database looks like this...

[{"Name":"James Suiter","Skill":2,"Age":15,"AgeWeeks":19,"ChanceOfLeaving":8},{"Name":"Neil Rowlett","Skill":8,"Age":15,"AgeWeeks":11,"ChanceOfLeaving":3}

It shows as an U16sArray -> Array when I do a print_r.

After the unset has been used somewhere in the data and saved again, the data now looks like this.

{"0":{"Name":"James Suiter","Skill":2,"Age":15,"AgeWeeks":20,"ChanceOfLeaving":9},"1":{"Name":"Neil Rowlett","Skill":8,"Age":15,"AgeWeeks":12,"ChanceOfLeaving":4}

So the '0' and '1' have been added. Now my code is wrong in various places and a print_r now shows it as U16sArray -> stdClass object.

SOLUTION (I think) - About 1/4 way down the PHP:json_encode page, I think is the answer, answered by 'simoncpu was here'. Apparently, 'Unsetting an element will also remove the keys. json_encode() will now assume that this is an object, and will encode it as such.'

http://php.net/manual/en/function.json-encode.php

So it would appear that it's working fine, unset changes it to an object, then when I next load it in, it doesn't function as an array any more.

The solution is to use array_values to re-index the array before encoding / saving.

Farflame
  • 421
  • 1
  • 5
  • 17
  • did you used `unset` ? – KodeFor.Me Nov 03 '15 at 18:25
  • 1
    Try `unset($this->{'1'});` There are no arrays just numeric properties to the object. – AbraCadaver Nov 03 '15 at 18:25
  • Why are you using `stdClass` for an array instead of just using a normal array? – Barmar Nov 03 '15 at 18:26
  • @Barmar I'm not sure why this guy is doing it in particular, but this often happens when pulling results from the database through an ORM or a framework and not specifying array as the result type. Many assume objects. – Mikel Bitson Nov 03 '15 at 18:27
  • Can you show us how you are creating this object? – Andy Noelker Nov 03 '15 at 18:28
  • `$this->U16sArray = json_decode($this->U16sJSON, true);` – AbraCadaver Nov 03 '15 at 18:58
  • I think this is the best solution yes. It makes it into an array. – Farflame Nov 03 '15 at 19:48
  • I won't even pretend to understand what's happening, but it starts off correctly as an array, even after loading / encoding / saving / decoding. I get [U16sArray] => Array. But then, after I try to use the 'unset' command, I get a fatal error, which as far as I can tell, means the data isn't saved, but then when I load it in again, it shows as [U16sArray] => stdClass Object. I'm completely confused by this. – Farflame Nov 04 '15 at 03:26
  • So long as I don't use the unset command, it remains [U16sArray] => Array, but as soon as I do, it becomes [U16sArray] => stdClass Object. I can do anything else without it changing, but the unset command, followed by the fatal error, seems to change it somehow. – Farflame Nov 04 '15 at 03:27

3 Answers3

2

This is an odd object, not going to lie.

Do either of these work? (Assuming that your print_r is a print_r($this);)

unset($this->{1});
unset($this->{'1'});

More info: Is it possible to delete an object's property in PHP?

EDIT: Though, I'd recommend changing the way you're getting this information through PDO so that it's in array format. More information on how to do this here: PDO returning execute results to an array

Once you have it in an array format, you can simply use:

unset($data[1]);
Community
  • 1
  • 1
Mikel Bitson
  • 3,583
  • 1
  • 18
  • 23
  • Yes, the object is strange in the first place. I should perhaps try to fix it so that it's a bit more... sensible. I'm not even sure how it's got that way.... The solutions don't work but they don't cause an error. They seem to do nothing at all to the object/array. – Farflame Nov 03 '15 at 18:34
  • How did you get this data? Is it pulled from the database using PDO or some other framework? I can assist in getting this into an array that contains objects. Either way, did this unset work? – Mikel Bitson Nov 03 '15 at 18:36
  • $this wil word only inside a class – Mihai Nov 03 '15 at 18:37
  • I think the formation of the object is the problem. It's created as objects, added to an array with $object->array[] = $newobject. The problem may be that it's then going through a database using PDO and JSON. So it's encoded to JSON, saved to the database, loaded and decoded. Then it's in this form. But no, the unset options didn't work, though they gave no error. – Farflame Nov 03 '15 at 18:40
  • Can you update your question with the code used to pull the data from the database with PDO? PDO has options to pull the data automatically as objects or as arrays. Switching to array will remedy this. – Mikel Bitson Nov 03 '15 at 18:42
  • Updated the question but it's getting a bit lost inside the larger object. Basically, it should be an array of smaller objects, inside a larger object. It's pulled from the DB using the code above. – Farflame Nov 03 '15 at 18:48
1

When you are using json_decode you can pass in a second parameter of true

$this->U16sArray = json_decode($this->U16sJSON, true);

the result will them be an array you can work directly with.

Jeremy Quinton
  • 688
  • 4
  • 13
0

I'm afraid this is just one of those things you can't do much about. It has to do with the way PHP was(not) designed to handle such cases. Basically you can not access an object property with a numeric variable name

This answer to a similar question will better explain: https://stackoverflow.com/a/10333200/1012699

However, you can cast the object into an array and access/delete its content. Then cast back into an object if you so desire. See the manual on type casting in php : http://php.net/manual/en/language.types.type-juggling.php

Community
  • 1
  • 1
Solomon A.
  • 491
  • 4
  • 7
  • Thanks, looks like I need to cast / delete / recast, or fix the way I've formed the object/array in the first place then. At least this points me in a direction, so thanks :) – Farflame Nov 03 '15 at 18:51
  • I believe it will be much simpler of you json decode directly as an associative array and cast to an object after data manipulation `$this->U16sArray = json_decode($this->U16sJSON, true);` – Solomon A. Nov 03 '15 at 18:57
  • Yes, that's fixed it so that it's now an array. So that does sort of solve the problem, though I have some other issues come up now... but they're all solvable :) – Farflame Nov 03 '15 at 19:48