1

Following is my $newArr0 which is an array of objects.

Array
(
    [0] => stdClass Object
        (
            [created_at] => Mon Dec 08 03:04:47 +0000 2014
            [text] => How a individual man can adopt a village in andhra real guds needed hats off to you MASTER BLASTER #Sachin
            [source] => Twitter Web Client
        )
    [1] => stdClass Object
        (
            [created_at] => Sun Dec 07 17:23:25 +0000 2014
            [text] => #two #cool #peoples ..#Coolfieee ..#me nd #Sachin http://t.co/JU971nWAPo
            [source] => Instagram

    [2] => stdClass Object
        (
            [created_at] => Sun Dec 07 15:18:22 +0000 2014
            [text] => Snga hit 90 odi international fifty...can he hit 6 more to break sachin s fifty record ..#sachin 96 odi fifty ...
kya lgta h tod dega record
            [source] => Twitter Web Client
        )    
    [3] => stdClass Object        
            [created_at] => Sun Dec 07 14:50:53 +0000 2014
            [text] => @jeeturaaj Jeeturaaj want @sachin chi book pahje, please give #Sachin #playingitmyway ......:)
            [source] => Twitter Web Client
        )

    [4] => stdClass Object
            [created_at] => Sun Dec 07 14:33:49 +0000 2014
            [text] => RT @UthMag: Old... http://t.co/b1HMzE3TZI #BCCI #cricket #featured #global #ICC #india #littlemaster #News #ODI #retire #sachin #sports #te…
            [source] => Twitter for Android
        )
)

I am trying to drop a key with source name from the array, so I tried this code -

foreach ($newArr0 as $nkey1 => $nval1) {
    if($nkey1 == "source") {
        unset($newArr0["source"]);
    }
}

But it is not removing the key, from $newArr0 Let me know what I am doing wrong here.

1
  • try this code foreach ($newArr0 as $nkey1 => $nval1) { unset($newArr0[$nkey1]->source); } print_r($newArr0); Commented Dec 8, 2014 at 6:14

3 Answers 3

1

First off, its a collection of objects inside the array, so you'd use the -> arrow operator on each object inside your loop. Then, alternatively, you could reference each copy of the object inside the foreach and make your unset.

foreach($newArr0 as &$nval1) {
    // for each object inside `$newArr0` is in `$nval1`
    unset($nval1->source);
    // unset $nval1's source 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow..this one actually works :) thx I will accept it soon(time limit)
1
<?php
$newArr = array();

foreach ($newArr0 as $key => $value) {
    unset($value->source);
    $newArr[$key] = $value;
}
$newArr0 = $newArr;

Demo

Comments

-1

Here:

foreach ($array as $key => & $object) {
    if ($object->source === 'source') {
        unset($array[$key]);
    }
}

1 Comment

This is not working either, since you are effectively deleting an entry in the array you are looping over... Try and give some explanation to your code in the future, so it's not just the solution, but also the explanation to the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.