php unset object from array - php

I am trying to remove an Article object from an array inside a foreach loop but I am getting the error
Fatal error: Cannot use object of type Article as array
foreach ($articles as $i => $article) {
foreach ($categorys as $category) {
if (checkCategory($category,$article)) {
unset($article[$i]);
}
}
if ($userName != Null) {
if ($article->getUserName() != $userName) {
unset($article[$i]);
}
}
if ($keyWords != Null) {
if (!containsKeyWords($keyWords, $article)) {
unset($article[$i]);
}
}
}

You have to unset from $articles array(main or parent array).so do like below:-
unset($articles[$i])

The error gives me some clue that your $article may not be an array but an stdClass Object, you can usevar_dump to check its type.
you can unset an object using unset($article->{$i}) or unset($article->somekey).
Hope it helps.

$article is of an Object type and you're trying to access it via array type.
Try var_dump() to check it's type & then accordingly use unset.
For array type : unset($article[$i])
For object type : unset($article->{$i}) where $i is key

Related

Foreach array within an object

I've got a PHP object. One of the values within the object is in array. I'm trying to get a foreach to just read the array within the object but it doesn't seem to be working.
PHP:
foreach ($object->ArrayName as $value) {
echo $value;
}
is there any way of doing this?
well, if you have an object but you don't know which property is an array, you need to catch them all and verify if they are an array:
// get each value of the object and call it as property
foreach(get_object_vars($object) as $property) {
// if this property is an array...
if (is_array($property) {
// ... access to every value of that array
foreach($property as $value) {
// $property is your array and $value is every value
// here you can execute what you need
}
}
}
Use
is_array($obj)
to validate whether it's an array or not.
you said your php object contains arrays, hence you need to use 2 foreach loops like this.
<?php
foreach($object->ArrayName as $value) {
foreach($value as $item) {
echo $item;
}
}
?>
if the loop is withing the object wich mean inside the class use $this instead :
foreach ($this->ArrayName as $value) {
echo $value;
}

function returns array return null

i got a php function in Wordpress that get serialized user meta : like this
function get_allowwed_tournees_ids(){
$tournees = get_user_meta($this->ID, 'tournees',true);
$tournees = unserialize($tournees);
$tournees_ids = array();
foreach ($tournees as $key => $value) {
array_push($tournees_ids, $key);
}
var_dump($tournees_ids);
return $tournee_ids;
}
get_allowwed_tournees_ids() is in a class that extends WP_User
and when i want to call it :
$id_tournees = $current_user->get_allowwed_tournees_ids();
var_dump($id_tournees);
the var_dump inside the function returns me the unserialised array, and the second var_dump outside the function returns null.
Any idea ?? Thanks !
Because you are returning $tournee_ids which is never defined. I think you should
return $tournees_ids;

How to parse stdClass object?

I have a SOAP response whose var_dump looks like this:
object(stdClass)[14]
public 'GetClientsResult' =>
object(stdClass)[15]
I can't figure out how to parse this for the life of me, I've never used stdClass before.
How can I parse this response in PHP?
For starters, you can cast it into an array (assuming the object is stored in $response):
$response = (array) $response;
Or you can access things by:
$response->GetClientResult->otherStuff;
An StdClass is an empty class where you can set and get property values. An example:
<?php
// $response is a normal array
$response['GetClientResult'] = 'foo'; // set
$response['GetClientResult']; // get
// $response is a StdClass
$response->GetClientResult = 'foo'; // set
$response->GetClientResult; // get
?>
And if you want to cast the class back to an array you can use:
$response = (array) $response
And if you want to do that recursive, because you have multiple StdClasses:
function StdClass2array(StdClass $class)
{
$array = array();
foreach ($class as $key => $item)
{
if ($item instanceof StdClass) {
$array[$key] = StdClass2array($item);
} else {
$array[$key] = $item;
}
}
return $array;
}

php access object within object

I have an object in PHP and I'm using a
foreach ($items as $item)
to iterate through the items. However, $item contains another object called $item, which contains the $type variable whose value I need to access. How can I access the value of $type? What I want to do is:
foreach($items as item){
if($item->item->type == 'this'){
//do this
} elseif ($item->item->type == 'that'){
//do that
}
}
But $item->item->type isn't picking up that value. How can I access it and use it for my function?
have you tired:
foreach($items as item){
if($item->type == 'this'){
//do this
} elseif ($item->type == 'that'){
//do that
}
}
or you can debug your code to find out what is going on:
foreach($items as item){
print_r($item);
}
and then you can see what kind of childs this $item has.

Update object value within foreach loop

I would like to loop through the contents of a query object update certain values and return the object.
function clearAllIds($queryObject)
{
foreach($queryObject->result() as $row)
{
$row->id = 0;
}
return $queryObject
}
In this example I would like to zero out all of the ID values. How can I accomplish this within the foreach loop?
Please excuse the formatting.
This entirely depends on what the class of your query object is, and whether or not you'll be able to Pass by reference.
Assuming your $queryObject->result() can be delivered in a write-context, you could preface the $row with an ampersand to pass it by reference, like so:
foreach($queryObject->result() as &$row)
{
$row->id = 0;
}
function clearAllIds($queryObject)
{
foreach($queryObject->result() as &$row)
{
$row->id = 0;
}
return $queryObject
}
Use the & operator to get $row as a reference.
Edit: This will work if $queryObject is an array. You should probably do
$data = $queryObject->result();
foreach($data as &$row) { ... }
return $data;
function trim_spaces($object)
{
foreach (get_object_vars($object) as $property=> $value)
{
$object->$property=trim($value);
}
}
//no need to return object as they are passed by reference by default

Categories