0

I have an array named $result, the output for print_r($result) look like this.

Array
(

[0] => Profile Object
    (
        [id] => 3
        [name] => alvis
        [parentLink] => ProfileLink Object
            (
                [href] => web
                [type] => data
            )
    )

[1] => Profile Object
    (
        [id] => 3
        [name] => gorgia
        [parentLink] => ProfileLink Object
            (
                [href] => text
                [type] => values
            )
    )

[2] => Profile Object
    (
        [id] => 4 
        [name] => text
        [parentLink] => ProfileLink Object
            (
                [href] => text
                [type] => values
            )
    )

 )

I tried displaying just the name by using:

foreach ($results[name] as $key => $value) {
            echo $key . ": " . $value."<br>";
            }

but I got an error Cannot use object of type Profile as array.

and when I tried

foreach ($results->name as $key => $value) {
            echo $key . ": " . $value."<br>";
            }

I got another error : Invalid argument supplied for foreach()
is there's away to display the id and the name?

2 Answers 2

1

You need to loop through your $result array using foreach, and then access the "name" attribute using "->" notation.

    foreach ($result as $profile) {
            print $profile->name;
     }

If you want to get all properties of the profile object, you will have to use get_object_vars().

     foreach ($result as $profile) {
           $vars = get_object_vars($profile);
           foreach ($vars as $key => $var) {
                 echo $key . ": " . $var."<br>";
                 // add suitable looping for the ProfileLink object too
           }
     }
Sign up to request clarification or add additional context in comments.

1 Comment

I used your code to get the answer! Thanks a lot, The code was: $vars = get_object_vars($results); for ($i=0;$i<100;$i++){ print_r($vars[$i]->name); print_r($vars[$i]->id); }
1

You can use foreach to iterate through each profile object in your array, and then reference the properties of each profile using ->. The code below ignores the numeric index of the array and just uses the id and name from the profile object.

foreach ($result as $profile) {
    echo $profile->id .": ".$profile->name . " <br>";
}

If you want to include the numeric index of the array as well:

foreach ($result as $index=>$profile) {
    echo "(index ". $index .") ".$profile->id.": ".$profile->name." <br>";
}

3 Comments

Thanks a lot doublesharp for your quick response, I tried the first script and I got this output " : : : : : :" and this "(index id) : (index name) : " for the second script, and it didn't trough any error message. Do you have any idea what's happened. Thanks again!
Are you using $result or $results - I think I incorrectly copied you variable name. I also updated my answer to not use curly braces and concatenate the string in case that matters.
That just displays the Key, not the values, I used the code that @Janenz00 suggested and I modify it and it works perfect! thanks a lot @doublesharp! $vars = get_object_vars($results); for ($i=0;$i<100;$i++){ print_r($vars[$i]->name); print_r($vars[$i]->id); }

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.