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.

In PHP I have an array $test. Running var_dump($test) looks like this:

array(2) {
  [0]=>
  object(stdClass)#2 (6) {
    ["name"]=>
    string(45) "Lorem"
    ["title"]=>
    string(96) "Lorem ipsum"
  }
  [1]=>
  object(stdClass)#3 (6) {
    ["name"]=>
    string(41) "Ipsum"
    ["title"]=>
    string(86) "Dolor sit amet"
  }
}

Now I need to add another field (url) to the $test objects so it looks like:

array(2) {
  [0]=>
  object(stdClass)#2 (6) {
    ["name"]=>
    string(45) "Lorem"
    ["title"]=>
    string(96) "Lorem ipsum"
    ["url"]=>
    string(86) "http://www.google.com"
  }
  [1]=>
  object(stdClass)#3 (6) {
    ["name"]=>
    string(41) "Ipsum"
    ["title"]=>
    string(86) "Dolor sit amet"
    ["url"]=>
    string(86) "http://www.stackoverflow.com"
  }
}

I've tried foreach() and $test->append('xxxxxxxx');, but am getting errors. Shouldn't this be real easy to do? What am I doing wrong?

share|improve this question
    
Replace append with suggestion from this answer: stackoverflow.com/questions/164395/… –  diegoperini Sep 8 '13 at 13:01

2 Answers 2

up vote 4 down vote accepted

You were close:

foreach( $test as $t ) {
    $t->url = "http://www.example.com";
}

It looks like you're trying to use append() (a method of ArrayObject) when you're really dealing with a stdClass object.

share|improve this answer
    
Not much of an explanation offered... –  Whymarrh Sep 8 '13 at 13:01
2  
@Whymarrh Working on it ;) –  André Dion Sep 8 '13 at 13:01
    
as you see I dont know lots about array objects. your answer works! thanks for that! but I dont understand anything with this "It looks like you're trying to use append() (a method of ArrayObject) when you're really dealing with a stdClass object." yes I've tried to use append and yes it is a method of ArrayObject. how could I have done it to work it that way? –  caramba Sep 8 '13 at 13:12
1  
@caramba ArrayObject::append() only works on instances of ArrayObject and only takes a value, so for your case you wouldn't really want to use it since you are dealing with an Array containing stdClass objects and you also need to define a property (url). –  André Dion Sep 8 '13 at 13:16

Append is for appending an entire object to another object. Just use normal object referencing (obj->value) to assign a url


$objectOne = new \stdClass();
$objectOne->name = 'Lorem';
$objectOne->title = 'Lorem ipsum';

$objectTwo = new \stdClass();
$objectTwo->name = 'Ipsum';
$objectTwo->title = 'Dolor sit amet';

$test = array(
    0 => $objectOne,
    1 => $objectTwo
);

$urls = array(
    0 => 'http://www.google.com',
    1 => 'http://www.stackoverflow.com'
);

$i = 0;
foreach ($test as $site) {
  // Add url from urls array to object
  $site->url = $urls[$i];

  $i++;
}

var_dump($test);

Output:

array(2) {
  [0]=>
  object(stdClass)#1 (3) {
    ["name"]=>
    string(5) "Lorem"
    ["title"]=>
    string(11) "Lorem ipsum"
    ["url"]=>
    string(21) "http://www.google.com"
  }
  [1]=>
  object(stdClass)#2 (3) {
    ["name"]=>
    string(5) "Ipsum"
    ["title"]=>
    string(14) "Dolor sit amet"
    ["url"]=>
    string(28) "http://www.stackoverflow.com"
  }
}
share|improve this answer
    
The counter inside the foreach could be better written using a simple for loop instead, especially since $test and $urls are the same length. You also don't need to specify your indexes on your arrays, nor do you even need $result. –  André Dion Sep 8 '13 at 13:10
1  
@AndréDion: But we do not know how he adds the urls, this was just a quick mockup as an example ^^ Im guessing he has some kind of query that checks for urls for each title, or something. –  JimL Sep 8 '13 at 13:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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