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.
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