batman99 Posted December 27, 2006 Share Posted December 27, 2006 HelloIm 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 domainecho "Domain Status: " . $result . '<br>'; // prints "Active"$result = $response->getName(); // returns the domain nameecho "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 More sharing options...
onlyican Posted December 27, 2006 Share Posted December 27, 2006 Showing the Data from Arrays is not as simple as echonote the examples Below[code]<?php$MyArray = array("Name" => "Jamie", "Age" => "20", "Gender" => "Male"); echo $MyArray;//Outputs //Arrayprint_r($MyArray);//Outputs //Array ( [Name] => Jamie [Age] => 20 [Gender] => Male )foreach($MyArray as $key => $value){ echo $key." is ".$value."<br />\n"; }/*Name is JamieAge is 20Gender 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 More sharing options...
batman99 Posted December 27, 2006 Author Share Posted December 27, 2006 HelloThanks 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 #221 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 More sharing options...
Barand Posted December 27, 2006 Share Posted December 27, 2006 Suppose you have something like this, storing the objects in an array[code]<?phpclass 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]<?phpforeach ($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 More sharing options...
TKirahvi Posted March 30, 2007 Share Posted March 30, 2007 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.6Thanks in advance Link to comment https://forums.phpfreaks.com/topic/31949-php-arrays-objects/#findComment-218227 Share on other sites More sharing options...
Guest upirate Posted March 30, 2007 Share Posted March 30, 2007 ahhhh script kiddies. Link to comment https://forums.phpfreaks.com/topic/31949-php-arrays-objects/#findComment-218228 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.