Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use foreach() to loop through object and change values

Tags:

php

I've got a problem looping trough an object ( stdObject ) and altering a value.

What happens is:

  • a query result comes in the function.
  • It fetches the object to an 'array'
  • After that I need to decode the content inside the object with html_entity_decode() to convert & etc. to readable characters...

Thats what is going wrong. I don't know how to put the converted string back into the object.

Here is the code of this function.

function jsonRequestHandlerUTF8($query) {
    $id = "0";
    $message = errorHandler($id);
    $a_result = array();
    if (mysql_num_rows($query) == 0) {
        //Empty sql query
        $id = '1';
        $a_result = jSONErrorObject($id);
    } else {
        //No error occurred
        $a_result['ExceptionId'] = $id;
        $a_result['ExceptionMessage'] = $message;
        $a_result['Items'] = null;
        while ($my_result = mysql_fetch_object($query)) {
            $a_result['Items'][] = $my_result;
        }

        $test = $a_result['Items'];
        foreach ($test as $v1) {
            foreach ($v1 as $v2) {
                $v2 = html_entity_decode($v2, ENT_QUOTES, "utf-8") . "\n";
// Here should be code to get the $v2 inside the object again.....                
            }
        }
}
    $a_result = json_encode($a_result);
    return $a_result;
}

$a_result['Items'] looks like this:

Array
(
    [0] => stdClass Object
        (
            [idziekmeldingen] => 1
            [meldingID] => 13190
            [title] => Ziekmelding: Alex
            [published] => 2011-05-09
            [updated] => 2011-05-09
            [content] => Per 9-05-2011 heeft Alex zich ziek gemeld.
            [location] => AP
            [institute] => CMI
            [lastCron] => 2011-05-11 11:32:54
        )

    [1] => stdClass Object
        (
            [idziekmeldingen] => 2
            [meldingID] => 12933
            [title] => Ziekmelding: Rimmert
            [published] => 2011-04-26
            [updated] => 2011-04-26
            [content] => Per 26-04-2011 heeft Rimmer zich ziek gemeld.Met vriendelijke groet,Luciënne Plomp
            [location] => AP
            [institute] => CMI
            [lastCron] => 2011-05-11 11:32:54
        )
)
like image 441
Highmastdon Avatar asked May 12 '11 12:05

Highmastdon


People also ask

Can you use a forEach loop on an object?

JavaScript's Array#forEach() function lets you iterate over an array, but not over an object. But you can iterate over a JavaScript object using forEach() if you transform the object into an array first, using Object. keys() , Object. values() , or Object.

How do you iterate through an array of objects?

To iterate through an array of objects in JavaScript, you can use the forEach() method aong with the for...in loop. The outer forEach() loop is used to iterate through the objects array.


1 Answers

Use the & symbol to pass the variables into the loop by-reference. This will mean you're modifying the actual variable rather than a copy of it.

foreach ($test as &$v1) {
    foreach ($v1 as &$v2) {
        $v2 = html_entity_decode($v2, ENT_QUOTES, "utf-8") . "\n";               
    }
}

(note, this only works in PHP 5.0 and up.... but if you're still using PHP4, you really need to upgrade!)

like image 96
Spudley Avatar answered Sep 21 '22 15:09

Spudley