#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    20
    Rep Power
    0

    Foreach through an array with objects and return the values and keys


    PHP Code:
    array(14) {


    [
    0]=>
      
    object(stdClass)#2 (2) {
        
    ["set_id"]=>
        
    int(44)
        [
    "name"]=>
        
    string(7"Cameras"
      
    }
      [
    1]=>
      
    object(stdClass)#3 (2) {
        
    ["set_id"]=>
        
    int(38)
        [
    "name"]=>
        
    string(11"Cell Phones"
      
    }
      [
    2]=>
      
    object(stdClass)#4 (2) {
        
    ["set_id"]=>
        
    int(39)
        [
    "name"]=>
        
    string(8"Computer"
      

    The Above is my Data.

    I want to return the object names ["Set_ID"] etc and the value.

    I have googled and googled and tried examples from here with various failures and now I'm giving up.

    PHP Code:
    I have tried to simply manually return data ie

    foreach($result as $results2)
    {
        foreach(
    $results2->name as $item)
        {


                    echo 
    "<pre>";
                    
    print_r($item);
                    echo 
    "</pre>";
        }

    I have tried all sorts of flavors of it I had kind of hoped the above would at least return data and it didn't - just errored.

    In the end, I'd like to be able to both pull names and data. I don't want to have to manually find element names and code it as $result->SET_ID or whatever - I want to be able to just feed SET_ID in as a variable. I think my problem lies with it being an array of objects and I cant access that object name and all..

    This:


    PHP Code:

    foreach($results2 as $item
    Returns some clean data, which is cool, but I need to get the object name (i.e "name" and "SET_ID") I am eventually going to shove it into a database using the key, or element or whatever those items are called and the associated data so I need that.


    Im a noob, so any kind of like detailed explanation wouldn't hurt my feelings so I can learn something.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,056
    Rep Power
    9398
    foreach on the outer thing (because it's an array), get_object_vars on the items to get their properties.

    But if all you're trying to do is get "set_id" (lowercase!) in a variable and access that in one of those objects, you can
    PHP Code:
    $attr "set_id";
    echo 
    $object->$attr
    (if $object is one of the objects in that array). Note the second $ in there: it's part of a feature called "variable variables" where you can have string variables with the names of object members, as well as variable and class and other names, and use that to access them.
  4. #3
  5. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Sounds like you just want to know how to access the object elements within the loop?

    PHP Code:
    foreach( $arrayOfObjects as $object ){
       print 
    'set_id: '.$object->set_id.'\n';
       print 
    'name: '.$object->name.'\n';

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    20
    Rep Power
    0
    Im probaby too unclear.


    I want to be able to get the object name. So when I parse the data, I get its associative name.

    so (this code isn't "right" its jsut reflective of the concept)

    I mean, if I just wanted DATA of the $result[1]->SET_ID, I'd just hard code that, but I don't want to have to specify the object name (hardcode the actual array key name), so eventually Id like a variable in the "->SET_ID" place.
    so I need to get the item's Key name and data contents.

    I want one var to contain SET_ID (or whatever the key name is) and another to contain the data so at some point i can insert it into a database just using key and data value.

    At some point id be


    "INSERT INTO table

    ($result[1]->$KeyName from Parse ) VALUES ($Data From Parse)"
  8. #5
  9. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    PHP Code:
    foreach( $arrayOfObjects as $object ){
        foreach( 
    $object as $name => $value ){
            print 
    "$name = $value\n";
        }

    You can treat objects like arrays in foreach to get the element name/value pairs.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    20
    Rep Power
    0
    Thank you so much! That Got it. I guess I missed that somehow. Seems simple when shown. I tried a bunch of stuff but apparently not that combination. Thanks again.
  12. #7
  13. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,056
    Rep Power
    9398

IMN logo majestic logo threadwatch logo seochat tools logo