Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array like this:

Array ( [0] => stdClass Object ( [id] => 1 [title] => ABC [sentece] => blablabla... [topic] => Science ) [1] => stdClass Object ( [id] => 2 [title] => DEF [sentece] => nomnomnom... [topic] => Technology )

How to add an array in stdClass Object in php?

For example: I want to add this array:

$number = array (82, 61);

So, I will have this array:

Array ( [0] => stdClass Object ( [id] => 1 [title] => ABC [sentece] => blablabla... [topic] => Science [number] => 82) [1] => stdClass Object ( [id] => 2 [title] => DEF [sentece] => nomnomnom... [topic] => Technology [number] => 61

)

Thanks for your help.

share|improve this question

closed as too localized by deceze, Jocelyn, brasofilo, Mario, Elias Van Ootegem May 10 '13 at 0:38

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Where are you stuck on this task in particular? Are you looking for the property accessor ->? –  deceze May 9 '13 at 10:03
    
I think he wants to distibute the array elements over the existing array so each array object contains a number from $number array –  Muhannad A.Alhariri May 9 '13 at 10:05
    
yes, that's my point Mr. Muhannad. –  bob May 9 '13 at 10:08

1 Answer 1

up vote 0 down vote accepted

With the information you provided, you'll need to do that manually.

Manual Entry

$Array = [
    //your array
];
$Array[0]->number = 82;
$Array[1]->number = 61;

Automated?

$Array = [
    //your array
];
$Numbers = [82, 61];
foreach($Numbers as $key => $number) {
    if(isset($Array[$key]) {
        $Array[$key]->number = $number;
    }
}
share|improve this answer
    
Thanks. it works! –  bob May 9 '13 at 12:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.