try
$array = json_decode( json_encode($array), true);
EDIT: I've tested this case, and it works:
$stdClass= new stdClass();
$stdClass->test = "foo";
$array = Array(
"a" => Array("b","c"),
"d" => $stdClass
);
$array = json_decode( json_encode($array), true);
var_dump($array);
OUTPUT
array
'a' =>
array
0 => string 'b' (length=1)
1 => string 'c' (length=1)
'd' =>
array
'test' => string 'foo' (length=3)
Answer from Luca Rainone on Stack Overflowtry
$array = json_decode( json_encode($array), true);
EDIT: I've tested this case, and it works:
$stdClass= new stdClass();
$stdClass->test = "foo";
$array = Array(
"a" => Array("b","c"),
"d" => $stdClass
);
$array = json_decode( json_encode($array), true);
var_dump($array);
OUTPUT
array
'a' =>
array
0 => string 'b' (length=1)
1 => string 'c' (length=1)
'd' =>
array
'test' => string 'foo' (length=3)
You can try
$array = array_map(function ($v) {
return (array) $v ; // convert to array
}, $array);
Or if this data is from json use
$array = json_decode($data,true);
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]);
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.
Remove empty stdclass object from array in PHP - Stack Overflow
php - how can i remove array from STDObject? - Stack Overflow
arrays - Remove string from an stdClass object in php - Stack Overflow
php - Remove stdclass object from array second time always fails - Stack Overflow
You can filter out the empty objects.
$data = array_values(array_filter($data, function($item) {
return (bool) (array) $item;
}));
array_values is necessary to prevent the {"0":{"id":2088996538},"2":{"id":2077495673}} form you were getting, because it reindexes the array.
The intermediate cast to array in the callback is necessary because any object evaluates to true whether it's empty or not, but an empty array will evaluate to false. If you decode to arrays instead of objects by setting the second parameter of json_decode to true, that cast isn't necessary.
Not directly related to the original question, but here's my suggestion for another way to do this starting with the data from the API:
$activities = json_decode($json, true);
$set_keys = array_flip(["start_latlng", "id", "start_date","name"]);
foreach ($activities as $activity) {
if (!is_null($activity['start_latlng'])) {
$result[] = array_intersect_key($activity, $set_keys);
}
}
echo json_encode($result);
If you decode to arrays using the second argument, then filtering is easier:
$activities = json_decode('[{"id": 2088996538},{},{"id": 2077495673}]', true);
$activities = array_values(array_filter($activities));
But you still need to re-index if you want sequential indexes.
Try this
$arr=array(17,10);
foreach ($array as
obj) {
if (!in_array($obj->category_id, $arr)) {
unset($array[$key]);
}
}
// edited, missing closing bracket
You can use array_filter to filter what you want or what you don't want
$filterCategory = array(17,10);
$array = array_filter($yourArray,function(
filterCategory) { return in_array($var->category_id, $filterCategory); } );
you can try
json_decode($yourJson,true);
for force return associative array
Cannot use object ( $object->property ) as $object['property']
But this is object and syntax is $obj->property
And array is $array['offset']
Or use $query->result_array() instead $query->result()
And return in a model method query object, this more convenient
unset($a->new_property);
This works for array elements, variables, and object attributes.
Example:
$a = new stdClass();
$a->new_property = 'foo';
var_export($a); // -> stdClass::__set_state(array('new_property' => 'foo'))
unset($a->new_property);
var_export($a); // -> stdClass::__set_state(array())
This also works specially if you are looping over an object.
unset($object[$key])
Update
Newer versions of PHP throw fatal error Fatal error: Cannot use object of type Object as array as mentioned by @CXJ . In that case you can use brackets instead
unset($object->{$key})
You can do
function unsetValue(array $array, $value, $strict = TRUE)
{
if(($key = array_search($value, $array, $strict)) !== FALSE) {
unset($array[$key]);
}
return $array;
}
You can also use spl_object_hash to create a hash for the objects and use that as array key.
However, PHP also has a native Data Structure for Object collections with SplObjectStorage:
$a = new StdClass; $a->id = 1;
$b = new StdClass; $b->id = 2;
$c = new StdClass; $c->id = 3;
$storage = new SplObjectStorage;
$storage->attach($a);
$storage->attach($b);
$storage->attach($c);
echo $storage->count(); // 3
// trying to attach same object again
$storage->attach($c);
echo $storage->count(); // still 3
var_dump( $storage->contains($b) ); // TRUE
$storage->detach($b);
var_dump( $storage->contains($b) ); // FALSE
SplObjectStorage is Traversable, so you can foreach over it as well.
On a sidenote, PHP also has native interfaces for Subject and Observer.
I agree with the answers above, but for the sake of completeness (where you may not have unique IDs to use as a key) my preferred methods of removing values from an array are as follows:
/**
* Remove each instance of a value within an array
* @param array $array
* @param mixed $value
* @return array
*/
function array_remove(&$array, $value)
{
return array_filter($array, function($a) use($value) {
return $a !== $value;
});
}
/**
* Remove each instance of an object within an array (matched on a given property, $prop)
* @param array $array
* @param mixed $value
* @param string $prop
* @return array
*/
function array_remove_object(&$array, $value, $prop)
{
return array_filter($array, function($a) use($value, $prop) {
return $a->$prop !== $value;
});
}
Which are used in the following way:
$values = array(
1, 2, 5, 3, 5, 6, 7, 1, 2, 4, 5, 6, 6, 8, 8,
);
print_r(array_remove($values, 6));
class Obj {
public $id;
public function __construct($id) {
$this->id = $id;
}
}
$objects = array(
new Obj(1), new Obj(2), new Obj(4), new Obj(3), new Obj(6), new Obj(4), new Obj(3), new Obj(1), new Obj(5),
);
print_r(array_remove_object($objects, 1, 'id'));
Is unset($row); what you want? But there is no need to do that if you are just doing it to save memory...
You need to know the key. However, if you iterate through the array you can find it.
/* Your original code: */
$obj = array();
// add the new items
$row = new stdClass();
$row->first = $first;
$row->last = $last;
$row->phone = $phone;
$obj[] = $row;
/* My addition: */
$theOneToDelete = $last;
foreach ($obj as $key => $row) {
if ($row->last == $theOneToDelete) {
unset($obj[$key]);
break;
}
}
There may be a terser approach with some of the array_* functions, but this lays it out.
You could use array_filter to do this:
$objects = array(
(object)array('id' => 1, 'name' => 'test1'),
(object)array('id' => 2, 'name' => 'test2'),
(object)array('id' => 3, 'name' => 'test1'),
);
$known = array();
$filtered = array_filter($objects, function ($val) use (&$known) {
$unique = !in_array($val->name, $known);
$known[] = $val->name;
return $unique;
});
check out this fiddle
You can do something like this:
$newArray = array();
foreach ($array as $value) { $newArray[$value->name] = $value; }
By the end of that cycle you will have an array, where every name meets only once (and you will keep the latest occurrence of that name).