Arrays are a powerful tool.

Once you get the hang of it, you can do almost anything.

Notice that some of them have named keys.

Anyway, I remember when I first started using arrays, especially with named keys, and the world of possibilities
that they opened up. So I thought I would share this...

Hope it helps.

(then find a good tutorial/book and use your imagination...)

glenn

 $myarray[1][0] = 'somekey';
 $myarray[1][]   = '1234';
 $myarray[1][]   = '5678';
 $myarray[1][]   = '9101112';

 print "example a\n";
 print_r($myarray);

print "example b\n";
print_r($myarray[1][1]);
print "\n";

// or
$myarray = array(
     'somekey' => array()
 );

 $myarray['somekey'][] = '1234';
 $myarray['somekey'][] = '5678';
 $myarray['somekey'][] = '910112';

print "example c\n";
print_r($myarray['somekey']);
print "example d\n";
print_r($myarray['somekey'][1]);
print "\n";

 //or

$myarray = array(
     'somekey' => array(
             'someotherkey' => array(),
             'anotherkey' => array()
     )
 );

 $myarray['somekey']['someotherkey'][] = '1234';
 $myarray['somekey']['someotherkey'][] = '5678';
 $myarray['somekey']['someotherkey'][] = '910112';

 $myarray['somekey']['anotherkey'][] = 'aaaa';
 $myarray['somekey']['anotherkey'][] = 'bbbb';
 $myarray['somekey']['anotherkey'][] = 'cccc';

 print "example e\n";
 print_r($myarray['somekey']);
 print "example f\n";
 print_r($myarray['somekey']['someotherkey']);
 print "example g\n";
 print_r($myarray['somekey']['anotherkey']);

Output from the above code is;

example a
Array
(
    [1] => Array
        (
            [0] => somekey
            [1] => 1234
            [2] => 5678
            [3] => 9101112
        )

)
example b
1234
example c
Array
(
    [0] => 1234
    [1] => 5678
    [2] => 910112
)
example d
5678
example e
Array
(
    [someotherkey] => Array
        (
            [0] => 1234
            [1] => 5678
            [2] => 910112
        )

    [anotherkey] => Array
        (
            [0] => aaaa
            [1] => bbbb
            [2] => cccc
        )

)
example f
Array
(
    [0] => 1234
    [1] => 5678
    [2] => 910112
)
example g
Array
(
    [0] => aaaa
    [1] => bbbb
    [2] => cccc
)


On Aug 25, 2009, at 3:27 PM, Hall, Leam wrote:

Okay, here's one of the places I don't really get and can use some help on. On my text processing script I'm going to slurp up host names. They come in as the first, counting from 0, element. I can explode and create the array, which gives me a string:

$hostname[1] = somehost.example.com

I can safely get rid of the ".example.com" bit, leaving $hostname[1] as "somehost". What I need to understand is how to take $hostname[1] and make it an array in it's own right so I can assign it a list of numbers.

For example, my text file looks something like this:

Bug ID:  1234
Affected Machines:
  somehost1
  somehost2.example.com
  somehostwehavenotseeninyears.example.com

Bug ID:  3456
Affected Machines:
  somehost1
  somehostwehanvenotseeninyears.example.com

At the end I need to print out something like this:

somehost1:  1234, 3456
somehost2:  1234
somehostwehavenotseeninyears:  1234 3456

Recommendations for a neophyte PHP'r?

Thanks!

Leam
_______________________________________________
New York PHP User Group Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

http://www.nyphp.org/show_participation.php

_______________________________________________
New York PHP User Group Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

http://www.nyphp.org/show_participation.php

Reply via email to