Jump to content

PHP Arrays / Objects


batman99

Recommended Posts

Hello

Im looking for some help with PHP please.

I have some code I am trying to use that returns a number of values when called.  This works fine for standard variables, but one part of it returns an array, and I am having difficulties extracting the information.

The code is called by using the following code:
[code]$command = new DomainInfoCommand();
$command->setName("adomain.ext");
$response = CommandProcessor::process($command);
$result = $response->getResult();
[/code]

...and the following code works perfectly:
[code]$result = $response->getStatus();  // returns the status of a domain
echo "Domain Status: " . $result . '<br>';    // prints "Active"
$result = $response->getName();  //  returns the domain name
echo "Domain Name: " . $result . '<br>';    // prints "adomainname.com"
[/code]

The problem is with this piece of code:
[code]$result = $response->getNss();  // returns the name servers for the domain
[/code]
which returns an array or object (or something like that).

Viewing the array with print_r($result) gives:
[code]Array ( [0] => DomainNs Object ( [name:private] => dns1.dnsserver.com [ip:private] => ) [1] => DomainNs Object ( [name:private] => dns3.dnsserver.com [ip:private] => ) )
[/code]

My question is, how can I extract the name and IP address from $result ?

Any help is really appreciated.

Thanks
Link to comment
https://forums.phpfreaks.com/topic/31949-php-arrays-objects/
Share on other sites

Showing the Data from Arrays is not as simple as echo

note the examples Below

[code]
<?php
$MyArray = array(
"Name"    => "Jamie",
"Age"      => "20",
"Gender"  => "Male"
);

echo $MyArray;
//Outputs
//Array

print_r($MyArray);
//Outputs
//Array ( [Name] => Jamie [Age] => 20 [Gender] => Male )

foreach($MyArray as $key => $value){
echo $key." is ".$value."<br />\n";

}
/*
Name is Jamie
Age is 20
Gender is Male
*/


echo $MyArray["Name"];
//Outputs
//Jamie
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/31949-php-arrays-objects/#findComment-148264
Share on other sites

Hello

Thanks onlyican for helping me with this.

It appears the array is another array inside the main one, so when I run this code:
[code]
$result = $response->getNss();
print_r($result);
foreach($result as $key => $value){ 
echo $key." is ".$value."<br />\n";  // returns "X is Object id #YY" - indicating another array, so problem is to get this data out of the 2nd array
}
echo "<br><br>Name Servers :: Size of array = ".sizeof($result)."<br><br>";
[/code]


I get the following:
[code]Array ( [0] => DomainNs Object ( [name:private] => dns1.dnsserver.com [ip:private] => ) [1] => DomainNs Object ( [name:private] => dns3.dnsserver.com [ip:private] => ) )
0 is Object id #22
1 is Object id #24
[/code]

I tried nesting the foreach's, but it just returns an error
[code]$result = $response->getNss();
print_r($result);
foreach($result as $key => $value){
echo $key." is ".$value."<br />\n";

            foreach($key as $key2 => $value2){    // added this code to get array content, but gives an error
echo $key2." is ".$value2."<br />\n";
}
}
[/code]
Any ideas?

Really appreciate the help.
Thanks
Link to comment
https://forums.phpfreaks.com/topic/31949-php-arrays-objects/#findComment-148305
Share on other sites

Suppose you have something like this, storing the objects in an array
[code]
<?php
class  DomainNs  {
        private $name;
        private $ip;
       
        function __construct ($name, $ip) {
            $this->name = $name;
            $this->ip = $ip;
        }
}

$ar = array();
$ar[] = new DomainNs('dns1.dnsserver.com', '123.123.123.123');
$ar[] = new DomainNs('dns2.dnsserver.com', '231.231.231.231');
$ar[] = new DomainNs('dns3.dnsserver.com', '132.132.132.132');
?>
[/code]

then this would output the array contents for PHP4 and PHP5 public variables
[code]
<?php
foreach ($ar as $obj)  {
    echo $obj->name, '<br/>';
    echo $obj->ip, '<br/><br/>';
}
?>
[/code]

It will only work for private variables in PHP5 if the class has a __get() method declared.

[code]
<?php       
        function __get($v) {
            return $this->$v;
        }
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/31949-php-arrays-objects/#findComment-148440
Share on other sites

  • 3 months later...
Why can't I create Objects in an Array within for/while loop?

If I have:

[code]
while( $db->next_record() )
{
$foo = $db->f('id');
$galleries[] = new Gallery($foo);
}
[/code]

Database query returns two id's and galleries array should then contain two Gallery Objects but it contains  only the first one.

If I do it like this, it works fine:

[code]
$galleries[] = new Gallery(1);
$galleries[] = new Gallery(2);
[/code]

I have PHP 5.1.6

Thanks in advance
Link to comment
https://forums.phpfreaks.com/topic/31949-php-arrays-objects/#findComment-218227
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.