Questions tagged [arrayobject]

ArrayObject class allows objects to work as arrays in PHP. Properties of the object have their normal functionality when accessed as list. Use this tag for questions related to arrayobject.

ArrayObject class allows objects to work as arrays in PHP. Properties of the object have their normal functionality when accessed as list. Use this tag for questions related to arrayobject.

ArrayObject is an object that is designed to behave exactly like an array. If that seems confusing, don’t worry, it’s not. ArrayObject is an object. It follows all the rules of how objects work. But it’s designed to implicitly behave like an array for all intents and purposes, including being used in a foreach loop, and accessing its properties just like you would access the values in an array. Consider the following code sample:

<?php

 $array = array('one', 'two', 'three');

 $arrayObj = new ArrayObject($array);

 var_dump(($array[0] == $arrayObj[0])); // Outputs true.

?>

For information on object and array please check and

Reference

337 questions
36
votes
6 answers

PHP Array and ArrayObject

which one should be used to manipulating data, Array or Array Object? Like search,sort and other array manipulations.
Imrul
  • 3,456
  • 5
  • 32
  • 27
35
votes
2 answers

Spl, ArrayObject, ArrayObject::STD_PROP_LIST

I'm trying to understand STD_PROP_LIST constant in the documentation but so far i didn´t understand it, and didn´t found any explanation :( The documentation has the following example: $a = new ArrayObject(array(),…
Fried Smurf
  • 489
  • 1
  • 5
  • 10
35
votes
7 answers

Difference between ArrayIterator, ArrayObject and Array in PHP

Can somebody explain clearly the fundamental differences between ArrayIterator, ArrayObject and Array in PHP in terms of functionality and operation? Thanks!
jkhamler
  • 543
  • 2
  • 6
  • 13
10
votes
1 answer

Unexpected behaviour in foreach while looping and unsetting in ArrayObject. An item is ignored

(examples at the bottom!!!) we have just upgrade our backend to PHP7 and after that, we have found a bug in our code related to an ArrayObject. The code just loops over a copy of an Object (type native ArrayObject). The foreach iterates by…
Daniel Nieto
  • 131
  • 6
9
votes
2 answers

Sort Proxy Array of Objects, localeCompare is not a function

Trying to use ES6 arrayObj.sort(a,b) => a.property.localeCompare(b.property) syntax but getting error: TypeError: a.property.localeCompare is not a function. I'm thinking localeCompare is not in scope but do not understand how to bind it to the…
Harvey Mushman
  • 615
  • 1
  • 11
  • 23
8
votes
4 answers

How to unset nested array with ArrayObject?

ideone Sample Code: array('d')); print_r($a); unset($a['b']['c']); print_r($a); Output ArrayObject Object ( [b] => Array ( [c] => Array ( …
mpen
  • 272,448
  • 266
  • 850
  • 1,236
8
votes
2 answers

array_slice (or other array_* functions) on ArrayObject

I have a question regarding ArrayObject. I wanted to use array_slice in an ArrayObject class and I couldn't. Is there a way to do it, without needing to write an "slice" method to the class that implements ArrayObject?
pocesar
  • 6,860
  • 6
  • 56
  • 88
7
votes
3 answers

Extending ArrayObject in PHP properly?

Problem: I am trying to extend PHP's ArrayObject as shown below. Unfortunately I can't get it to work properly when setting multi-dimensional objects and instead an error thrown as I have the strict settings enabled in PHP. (Error: Strict standards:…
Industrial
  • 41,400
  • 69
  • 194
  • 289
7
votes
5 answers

PHP how to array_unshift on an arrayObject

As stated in the title, How do you perform an array_unshift() on a arrayObject, array_push() is obtained by doing an arrayObject->append() but what about unshift ? Edit: What I've forgot to mention is that i also need in this particular case to…
malko
  • 2,292
  • 18
  • 26
7
votes
3 answers

How can I sum objects property of an array using PHP

I have an array of objects, and i want to sum value of one of the property.Here is a picture which will show the structre of array. Here is my code,that doesn't work. print_r($res);//this appear the structure of array,which i will show. $sum = 0;…
Mr Alb
  • 291
  • 2
  • 4
  • 16
7
votes
2 answers

PHP ArrayObject / ArrayIterator : concept with example

I'm trying to understand the concept of the Object Array in PHP. Up to date I was simply using regular arrays to loop through the list of records and display them in say table. I know that I could do this using Object, but I'm not quite sure how to…
Spencer Mark
  • 5,263
  • 9
  • 29
  • 58
7
votes
1 answer

Does PHP's ArrayObject have an in_array equivalent?

PHP has an in_array function for checking if a particular value exists in an native array/collection. I'm looking for an equivalent function/method for ArrayObject, but none of the methods appear to duplicate this functionality. I know I could cast…
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
6
votes
2 answers

Overriding functions incompatibility

TL;DR I want to override offsetSet($index,$value) from ArrayObject like this: offsetSet($index, MyClass $value) but it generates a fatal error ("declaration must be compatible"). What & Why I'm trying to create an ArrayObject child-class that forces…
Nanne
  • 64,065
  • 16
  • 119
  • 163
5
votes
3 answers

How to get the unique id's of objects in an array swift

I have a custom class like this - class Event: NSObject { var eventID: String? var name:String? } Now i have an array of Event object's like var events = [Event]() var event1 = Event() event1.eventID = "1" event1.name = "Anu" var…
Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63
5
votes
1 answer

ArrayObject, getIterator();

I am trying to understand what getIterator() is, I will explain: As I know getIterator is a method we call to include an external Iterator. The problem is getIterator include it's own methods the closes think looks the same is Iterator interface…
Aviel Fedida
  • 4,004
  • 9
  • 54
  • 88
1
2 3
22 23