SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to access falues in my array

    hi

    have an array getting from a function and when i'm using print_r, print_r($answer).

    the string i get:
    Code:
    Array ( [DomainCheckResult] => Array ( [@attributes] => Array ( [Domain] => mystuff.com [Available] => false ) ) )
    if i want to print\use the domain(mystuff.com) and Available(false) attributes how can i do it ?

    thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let's format that better to understand it:

    Code:
    $answer = array(
      'DomainCheckResult' => array(
        '@attributes' => array(
          'Domain' => 'mystuff.com',
          'Available' => false
        )
      )
    );
    This is a multi-dimensional array, meaning arrays inside arrays, so we need to use multiple square brackets:

    This will output the string mystuff.com
    Code:
    echo $answer['DomainCheckResult']['@attributes']['Domain'];
    And you can use the true/false value like this:

    Code:
    if ($answer['DomainCheckResult']['@attributes']['Available']) {
      echo $answer['DomainCheckResult']['@attributes']['Domain'] . ' is available.';
    } else {
      echo $answer['DomainCheckResult']['@attributes']['Domain'] . ' in not available.'];
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •