1

i have a var that holds an array of objects. $object:

Array
(
[0] => First_Model Object
    (
        [_id:protected] => 988198
        [_status:protected] => some string here
        [_user_id:protected] => 18289
    )

[1] => Second_Model Object
    (
        [_id:protected] => 566198
        [_status:protected] => some string here
        [_user_id:protected] => 18290
    )
....

if i do:

<?php foreach($object as $row): ?>
<?php echo $row->status(); ?>
<?php endforeach: ?>

i get: some string here

what i want to do is to edit $row->status(), maybe use ucwords or something on this string and them place it back in the $object so that when i do the loop i get the changed result.

i could alter the result inside the foreach loop but i want my code to be clean.

so i need to grab $object->status do something with it then place it back on the $object then ill have the modified result..

any ideas on how to place the modified string inside the array of objects?

thanks

3
  • Why is using a foreach loop not clean? Commented Jan 30, 2012 at 22:47
  • @webbiedave Theory is: when you use specialized function (such as array_walk), in future it may work in parallel threads. And I guess php provides some kind of optimalization for calling function via this function (at least callback are called from C source (array_walk is implemented in C), not via interpreted (slower) code). Commented Jan 30, 2012 at 23:02
  • 1
    @Vyktor: Using array_walk is almost always slower than just foreach-ing. Future parallel thread? By the time PHP can do that, we'll all have flying cars :) Commented Jan 30, 2012 at 23:12

1 Answer 1

1

You need array_walk, all necessary examples are in documentation :)

I would also create interface, that would declare methods: setStatus and getStatus and use

if( !($item1 instanceof YourInterface)){
    throw new Exception(...);
}
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.