Changing values of an array

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hope someone can help

I have an array called $test
It can have one element or many elements

If I do a print_r($test) for 3 elements I would get:

Array
(
[mx1.megamailservers.com] => 0
[mx2.megamailservers.com] => 1
[mx3.megamailservers.com] => 2
)

What I want to do is change the values, 0,1,2 with a new value $new which is found from the key values

I have tried many methods but none seem to work

Can someone make a suggestion on how to do this.

I figure we would be using a foreach loop, since the array can vary.

Thanks
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9090
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

I am not sure exactly what you are trying to do, but you could change values of that array like so:

PHP Code: [ Select ]
$test['mx1.megamailservers.com'] = $new;
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I can't use what you say because the key always changes

The key is basically the host smtp mail server
the $new is found by gethostbyname($host) to get the IP address which is basically the key value here.

I wanted to use a foreach loop to go through the array and find $new since host is always changing.

Unless I don't understand you
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Code: [ Select ]
foreach($test as $key => $val)
{
  $test[$key] = gethostbyname($key);
}
  1. foreach($test as $key => $val)
  2. {
  3.   $test[$key] = gethostbyname($key);
  4. }
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi jobert

It worked

thanks

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 67 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.