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

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    4

    Printing Items from Array


    Hi there,

    I am working with an array of objects but want to just echo the itemID as a stand alone list.

    The structure of the array of Objects looks like this:

    PHP Code:
    $myarray=array();

        
    $myarray[] = new myProduct('Product_A''26');
        
    $myarray[] = new myProduct('Product_B''52');
        
    $myarray[] = new myProduct('Product_C''13'); 
    When I do print_r with the above I get the following:

    Array
    (
    [0] => myProduct Object
    (
    [name] => Product_A
    [itemID] => 26

    )

    [1] => myProduct Object
    (
    [name] => Product_B
    [itemID] => 52

    )

    [2] => myProduct Object
    (
    [name] => Product_C
    [itemID] => 13

    )

    However, my aim is for php to display just the itemID's to the browser:

    26
    52
    13

    Any help much appreciated.

    Cheers
    Volterony
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,868
    Rep Power
    368
    well print_r just prints everything. you need to use a loop ( foreach, for etc ) to go through the array and then get the appropriate attribute of each object in the array.

    This should be easy, if you have problems post your code, i want you to think hence not posting the solution
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    4
    Originally Posted by paulh1983
    well print_r just prints everything. you need to use a loop ( foreach, for etc ) to go through the array and then get the appropriate attribute of each object in the array.

    This should be easy, if you have problems post your code, i want you to think hence not posting the solution
    Nearly there. This is printing out both the name and id, so just need to figure out how to echo just the itemID?

    PHP Code:
    foreach ($myarray as $key => $value)
    {
        foreach (
    $value as $k => $val)
        {
            echo 
    $val .'</br>';
        }

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

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0
    PHP Code:
    foreach ($myarray as $item)
    {
            print(
    $item->name);

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    4

    Smile


    Originally Posted by dedicatedzone
    PHP Code:
    foreach ($myarray as $item)
    {
            print(
    $item->name);

    BINGO !!!!

    Thanks for your help

    Volterony
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by volterony
    BINGO !!!!

    Thanks for your help

    Volterony
    Anytime!

IMN logo majestic logo threadwatch logo seochat tools logo