1

I don't see the wood for the trees, please point me into the right direction.

I've got a mysql query and retrieve the rows as objects. This works fine. Now, I want to populate an object's array with the values from the database. This object ($myobject) has several arrays (i.e. $myobject->field_1). These arrays contain (multiple) values for a certain field in various languages, hence it could be sth. like $myobject->field_1['en'][0] = 'value'. Here's what I tried to do:

$query = 'SELECT t.id, t.lang, t.val FROM table t';
$result = db_query($query); // custom db layer function
foreach ($result AS $row) {
    $fieldname = 'field_'.$row->id;
    $myobject->$fieldname[$row->lang][] = $row->val;
}

The last line is not working. Since I do not know, how many values are already stored for a language, I just want to add ([]) a value to the end of the array.

Which neat trick am I missing?

3
  • Show sample of $result Commented Mar 20, 2012 at 11:26
  • On line 4, you have a square bracket which doesn't match up. Is that a typo in your example here, or is that in your code? Commented Mar 20, 2012 at 11:30
  • @Zack: Yes, that has been a typo. Just fixed that. Commented Mar 20, 2012 at 15:28

1 Answer 1

1

You can try this:

$query = 'SELECT t.id, t.lang, t.val FROM table t';

$result = db_query($query); // custom db layer function

foreach ($result AS $row) {
    $fieldname = 'field_'.$row->id;
    $myobject->{$fieldname}[$row->lang][] = $row->val;
}

Notice the braces around the $fieldname at the last line

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

1 Comment

That's it. Thanks, I knew it was something simple like that, I really need some days off. ;)

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.