0

I try to create an generic object which needs to be structuered like this:

[Content] => stdClass Object
    (
        [item] => Array
            (
                [0] => stdClass Object
                    (
                        [Value] => STRING
                    )

            )
        [item] => Array
            (
                [0] => stdClass Object
                    (
                        [Value] => ANOTHER STRING
                    )
            )
    )

This is my code:

$content = new stdClass();
$data = file('filname.csv');

foreach($data as $key => $val) {
    $content->item->Value = $val;
}

This overwrites itself each time the loop iterates. By defining item as an array like this:

$content->item = array();
...
$content->item[]->Value = $val;

...the result is also not the estimated.

1
  • 1
    You can't have 2 properties with the same name! Commented Jul 30, 2015 at 13:03

1 Answer 1

3

You are overwritting data each time even using array. You should create temporary object for storing value and then put them to item array.

$content = new \stdClass();
$content->item = array();

foreach($data as $key => $val) {
    $itemVal = new \stdClass();
    $itemVal->Value = $val;
    $content->item[] = $itemVal;
}
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.