1

So, I have following db result:

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [user] => 1           
        [img] => 2016/02/img_8488.jpg
        [url] => /p=?44           
        [sent_date] => 2016-02-13 00:00:00
    )

[1] => stdClass Object
    (
        [id] => 2
        [user] => 185                     
        [img] => 
        [url] => /?p=54         
        [sent_date] => 2016-02-06 00:00:00
    )

)

How would I remove [id] and [sent_date] from the query result?

I am not sure if I am using unset right.

unset($results[0]['id']); 
$reindex = array_values($results); 
$objectarray = $reindex; 
1
  • [id] and [sent_date] are not keys, it's properties of an object. Commented Feb 13, 2016 at 20:34

2 Answers 2

1

Instead of removal or unset you can create a new array;

$i = 0;
$newResult = array();
foreach($result as $value){
$newResult[$i]["user"] = $value->user;
$newResult[$i]["img"] = $value->img;
$newResult[$i]["url"] = $value->url;
$i++;
}

print_r($newResult);

$newResult will return the new array and your original array remains same you can use it if you need.

Or removal of indexes is must required than use unset inside the foreach loop as:

unset($value->id); 
unset($value->sent_date);

Side note:

Also keep in mind you can not use it as $value["id"] becuase its a property not an array index.

Sign up to request clarification or add additional context in comments.

Comments

1

Use unset($results[0]->id); and unset($results[0]->sent_date) instead and it should work. If you want to do this in all of the array objects:

for($i = 0; $i<sizeof($results); $i++)
{
    unset($results[$i]->id);
    unset($results[$i]->sent_date);

}

1 Comment

Ah, i see. Thank you for the clarification! =)

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.