Web Analytics Made Easy -
StatCounter Echo an Array's Value - CodingForum

Announcement

Collapse
No announcement yet.

Echo an Array's Value

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Echo an Array's Value

    Hey,

    I currently am messing around with twitter's API and am using a class I found on the internet (http://ryanfaerman.com/twittersearch if it matters)

    It prints out all of the info I need into an array, but I'm not very good with arrays and was wondering if anyone could help.

    The array is formatted like this:

    Code:
    Array
    (
        [0] => stdClass Object
            (
                [text] => this is my tribe http://www.sfondideldesktop.com/Images-Animals/Deers/Whitetail-Deer-B-Female-Drinking-Water.Jpg
                [to_user_id] => 
                [from_user] => laurenestelle
                [id] => 1464496735
                [from_user_id] => 5519148
                [iso_language_code] => en
                [source] => <a href="http://twitter.com/">web</a>
                [profile_image_url] => http://s3.amazonaws.com/twitter_production/profile_images/115481833/Photo_100_normal.jpg
                [created_at] => Mon, 06 Apr 2009 18:37:14 +0000
            )
     
        [1] => stdClass Object
            (
                [text] => man, sara is stubborn, she is still fighting her nap after a diaper change and being put back in bed with all her stuffed animals and bl ...
                [to_user_id] => 
                [from_user] => jenseibel
                [id] => 1464495282
                [from_user_id] => 2231331
                [iso_language_code] => en
                [source] => <a href="http://www.friendbar.com">Friendbar</a>
                [profile_image_url] => http://s3.amazonaws.com/twitter_production/profile_images/70035203/jen_normal.JPG
                [created_at] => Mon, 06 Apr 2009 18:36:57 +0000
            )
     
        [2] => stdClass Object
            (
                [text] => @kraupu ftw, kraupuk, ftw! http://septyni.tumblr.com/post/90047077/dancing-animals-in-love-vancouver-film-school
                [to_user_id] => 55168
                [to_user] => kraupu
                [from_user] => septyni
                [id] => 1464489944
                [from_user_id] => 2950730
                [iso_language_code] => is
                [source] => <a href="http://twitter.com/">web</a>
                [profile_image_url] => http://s3.amazonaws.com/twitter_production/profile_images/96311877/7enas_normal.jpg
                [created_at] => Mon, 06 Apr 2009 18:36:02 +0000
            )
     
    )
    I don't know how to get all this info set into individual variables though ([text] = $text).

    I think I'm suppose to use foreach, but this is really my first time using arrays like this (normally its just mysql ones and I can use a simple while and get variables through that)

    This also involves objects (Which I have 0 experience with to make everything harder)

    So can anyone help me with this?

    Thanks in advance for any help!
    Last edited by animedude123; Apr 6, 2009, 03:33 PM.

  • #2
    Inserting/editing a specific spot in the array:
    PHP Code:
    $array_variable[0]->text 'Hi, my name is Shane';
    $array_variable[0]->profile_image_url 'http://images.com/myimage.jpg'
    Inserting at the end of the array:
    PHP Code:
    $array_variable[]->text 'Hi, my name is Shane';
    $array_variable[]->profile_image_url 'http://images.com/myimage.jpg'
    Searching through the array:
    PHP Code:
    foreach($array_variable as $twit)
    {
        echo 
    '<img src="' $twit->profile_image_url '" />';
        echo 
    $twit->text;

    -Shane
    Last edited by TheShaner; Apr 6, 2009, 03:40 PM. Reason: Noticed it's objects inside an array rather than a multidimensional array

    Comment


    • #3
      Ok, I think I got this actually. Just took some messing around (and a lot of guess and check)

      Just did this (extremely simple actually):

      PHP Code:
      foreach($results as $tweet) {
          
      $text $tweet->text;
          echo 
      $text "<br><br>\n\n";

      My main problem was I was using array methods (like I would with mysql arrays), instead of object methods.
      Last edited by animedude123; Apr 6, 2009, 03:39 PM.

      Comment

      Working...
      X