0

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?

1

2 Answers 2

7

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.

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

3 Comments

@Whymarrh Working on it ;)
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 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).
1

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"
  }
}

2 Comments

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: 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.

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.