Forum Moderators: coopster

Message Too Old, No Replies

echo Data from array

         

TheKiller

4:58 pm on Jan 7, 2011 (gmt 0)

10+ Year Member



Hello
I Have a few problems understanding what to do with this Arrays

So if i have a string that echo's :


Array
(
[TEST1] => Tested 1
[TEST2] => Tested 2
[TEST3] => Tested 3
)


How may i echo the middle line from the array ?

penders

5:11 pm on Jan 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If your array is in the variable $myArray, then:
echo $myArray['TEST2'];


... will echo 'Tested 2' - the middle line.

TheKiller

2:19 pm on Jan 9, 2011 (gmt 0)

10+ Year Member



Ty i understand that now
but what about if i have two arrays like this


[0] => Array
( )

[1] => Array
( )

<?php
$result = dns_get_record("php.net");
echo"<pre>";
print_r($result);
echo"</pre>";

echo"===========";

echo $result["target"];
?>


by the way is there any BBCode for PHP Script ? on phpbb and other forums there is

penders

3:52 pm on Jan 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Arrays within arrays, aka multi-dimensional arrays, then in the case of the dns_get_record() [uk2.php.net] function which returns an array of associative arrays...

echo $result[0]['target']; 
echo $result[1]['target'];
:


Note: The first index is numeric, so no quotes around the value.

Be aware, however, that in this case values in the associative array might not be set for a particular key (eg. 'target') so you might need to check for the existence first in order to avoid a PHP warning:
if (isset($result[1]['target'])) { 
echo $result[1]['target'];
}


No special BBCode for PHP script that I'm aware of. However, here's some general code posting guidelines:
[webmasterworld.com...]

TheKiller

4:16 pm on Jan 9, 2011 (gmt 0)

10+ Year Member



oh thanks ! now i know how to do this
but are there other ways to get data out from arrays besides this?

i was thinking to write some kind of a Whois page for domain names to get their name servers and so on .. but in this dns_get_record has a new array number etc .. i cant know what to write for each domain the nr to get the NS

penders

5:18 pm on Jan 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you don't know the specific array keys, you can step through arrays using foreach() [uk2.php.net] OR while(), list() and each().

Check out PHP's array fucntions:
[uk2.php.net...]

TheKiller

11:31 pm on Jan 10, 2011 (gmt 0)

10+ Year Member



could you show me a example using list()?

penders

11:52 pm on Jan 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



From the PHP Manual page for each() [uk2.php.net] - Example #2 Traversing an array with each()

<?php 
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
reset($fruit);
while (list($key, $val) = each($fruit)) {
echo "$key => $val\n";
}
?>


Outputs:
a => apple
b => banana
c => cranberry


As you can see while(), list() [uk2.php.net] and each() are used together in this instance.

TheKiller

9:08 pm on Jan 11, 2011 (gmt 0)

10+ Year Member



hmmmm........
the output looks like an array
i want to output some line from an array with the name target for example

but i want to do it without to specify the number

i want to look in all arrays with the target name and show it

penders

11:15 pm on Jan 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i want to look in all arrays with the target name and show it


Something like this (untested):

$result = dns_get_record("php.net"); 
foreach($result as $record) {
// $record is each sub array (0, 1, 2, ...) in turn
if (isset($record['target'])) {
echo 'TARGET = '.$record['target'].'<br>';
}
}

TheKiller

11:26 pm on Jan 11, 2011 (gmt 0)

10+ Year Member



YES !
Thats what i was looking for
Thankk You

i will bump this when/if i'll need more support :)