0

Objective to replace part of an object name. Get an error with the code below. Guessing the str_replace needs to be in another format?

foreach($data as $i => $item) {
   if(strpos($i, 'SIEcat134') !== false){
       $data-> str_replace("SIEcat134","newname",$i) = $data->$i; 
       unset($data->$i);
    } 

}

Object example (contains many more rows)

stdClass Object
(
    [SIEcat134analys] => test
    [SIEcat134avst1] => test2
    [SIEcat134avstbelcy1] => 469 915
    [SIEcat134avstref1] => 19.1
    [SIEcat135analys] => test
    [SIEcat135avst1] => test2
    [SIEcat135avstbelcy1] => 469 915
    [SIEcat135avstref1] => 19.1
 etc
etc
}

1 Answer 1

1

If you want to use an expression as a dynamic property name, you have to wrap it in {}.

$data->{str_replace("SIEcat134","newname",$i)} = $data->$i; 

You can only leave out the braces when the expression is just a simple variable, as in $data->$i.

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

Comments

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.