removing objects from an object array in php? - php

for example say i have an object array called $list looks like this:
<(stdClass)#16 (3) {
["1"]=>
<(8) "50504496"
["2"]=>
<(8) "12435374"
["3"]=>
<(8) "12436374"
Im doing a foreach on the object array and removing them if they exist in the database i.e
foreach($list as $l){
//do the query
if( it exists){
//remove from objects: this is where i need help!!
}
}
i have the db logic, im just stuck to know how i can remove objects, i was thinking maybe i should create a new object and add them thier. thanks
}

Use: unset
foreach($list as $key => $obj){
if( exists .. ) {
unset($list[$key]);
}
}

run your query and if it returns >0 number of rows then you have a result, use unset($object->$var) to unset a variable

Try like:
foreach($list as $li)
{
if($li)
{
//copy into another array.
}
}
i thnk its may simple

Try like:
$i=0;
foreach($list as $li)
{
if($li)
{
$b[i] = $li;
$i++;
}
}
array '$b' will hold the values in $list which are not null

Related

get object data stdCLass in PHP

i want get data from stdclass object. i use foreach method to get key and value.
with var_dump i can get all infromation about the post, buy i want extract all 'display'.
foreach($data as $key=>$value){
var_dump($value);
}
var_dump result :
i just want extract all display_ur property. can anyone exlain me ?
do like below:-
foreach($data as $key=>$value){
foreach($value as $val){
echo $val->node->display_url;
echo PHP_EOL;
}
}
Your original array is made out of stdClassObjects. Each of these class objects has a public property called node that is also a stdClassObject.
That means that if you want to retrieve the display_url for each of these objects, you need:
foreach ($array as $object) {
$node = $object->node;
var_dump($node->display_url); // this should return what you are looking for
}
$value->display_url. I think this will work for you.
With type cast
$array=(object) $stdClassObject;
foreach($array as $key=>$value){
var_dump($value);
}

How to search in an array of objects for an object with array_search?

I have an array ($sensors) of objects. In this array I want to search for a sensor with an ID ($deviceId).
With a foreach loop it works. I get exactly one object. No array.
Now I'm looking for a solution with array_search. But it doesn't work.
Does anyone have a solution?
$sensor = null;
foreach ($sensors as $item)
{
if ($item->DeviceId == $deviceId)
{
$sensor = $item;
break;
}
}
Object convert to array and search in array ,I cant see your all code but almost should code as below
$array=json_decode(json_encode($object),true);
$result=array_search($sensor,$array);
print_r($result);
if ( ($key = array_search($item->DeviceId, $sensors)) !== false) {
echo $sensors[$key]);
}

Provoque element change in a foreach in Laravel

I have 2 loops,
I would like that for each iteration of fighters2, $fighter1 also advance 1 element. Is is posible to do it with foreach????
foreach ($fighters1 as $fighter) {
foreach ($fighters2 as $fighter2) {
}
}
not sure if i understand the question correctly, but here is the answer to how i understood the question.
if your arrays are the same size you could do sth like this:
foreach($fighters1 as $index => $fighter) {
$fighter2 = $fighters2[$index];
//do what you need to do with $fighter and $fighter2
}
that is assuming both arrays are same size and have numeric indexes
Maybe using next() might help you.
foreach ($fighters1 as $fighter) {
$nextFighter1 = next($fighters1);
foreach ($fighters2 as $fighter2) {
//do whatever you need
}
}
next() reference
Bye!

Remove Object from array by object value in php

$followers = [['id'=>0], ['id'=>1]];
So, assuming I have this array, what would be the best way to remove the object by it's id?
That's what i came up with
foreach($followers as $key=>$follower){
if($follower->id == 0){unset $followers[$key]}
}
is there a better more efficient way?
It is an array not an object, so why are you accessing as an object?
Try this,
foreach ($followers as $key => $follower) {
if($followers[$key] == 0) {
unset($followers[$key]);
}
}
You were accessing values of an array like an object.
You were using unset incorrectly.
You were missing the ; at the end of your unset part.
What I really need is removing an item from array of arrays in my Cache I tried this:
if(Cache::has('user_follower'.$user_id)){
$user_follower = Cache::get('user_follower'.$user_id);
foreach($user_follower as $key=>$follower){
if($follower->user_id == Auth::id()){
unset($user_follower[$key]);
Cache::put('user_follower'.$user_id, $user_follower, 30);
return;
}
}
}
I failed sadly, so I'm for now I'm using Cache::forget.

PHP foreach of multidimensional array

I have an array and it has two arrays inside of it...I am able to access what I want for the first row by doing this...
print_r( $_SESSION['shopcart']['cart']['qty']);
How would I write that in a foreach?
Thanks,
J
foreach($_SESSION['shopcart']['cart']['qty'] as $value) {
echo $value;
}
you would do something like this:
to dump the array: $_SESSION['shopcart']['cart']
foreach($_SESSION['shopcart']['cart'] as $key=>$value){
echo $key." => ".$value."<br/>";
}
If you want to iterate through multiple dimensions, you can nest foreach as follows:
foreach($_SESSION['shopcart'] as $cart) {
foreach ($cart as $qty) {
// do something
}
}
Though I'd need a little more information about the array structure and what you really want to do in order to provide usable code, this is probably in the right ballpark.
I would recommend you you do do like this:
foreach($_SESSION['shopcart'] as $key=>$value){
if(is_array( $value ) ){
foreach($value => k1 => $v1){
//do something here if array
echo $k1." => ".$v1."<br/>";
}
}else{
//do something here if not array
}
}

Categories