Jump to content


Photo

PHP Newbie.


  • Please log in to reply
7 replies to this topic

#1 daniel1988

daniel1988
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 21 September 2006 - 12:15 PM

Im trying to modify values in an array.

I have several strings and passing them into an array, I then wish to modify the strings value.

He is what I have, this is not correct but I hope it demostrates my problem.

$StringA = "John";
$StringB = "Jack";
$StrnigC = "Joe";

$array = array ( $StringA, $StringB, $StringC );

foreach( $array as $temp )
{
    if ( $temp == "Joe" )
    {
          $temp = "NEW NAME";
    }
}

So, I just really want to change the value. :)

Thanks in advance.



#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 September 2006 - 12:19 PM

You're almost there, I'd do it like this...

<?php
$array = array ( $StringA, $StringB, $StringC );

 foreach($array as $key => $temp)
 {
     if ( $temp == "Joe" )
     {
          $array[$key] = "NEW NAME";
     }
}
?>

Like I say, you were almost there.  All I've done differently is use both the key and value in the foreach, as opposed to just the value, as you need the key to assign the new name to.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 daniel1988

daniel1988
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 21 September 2006 - 12:23 PM

THANKYOU :D

#4 daniel1988

daniel1988
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 21 September 2006 - 12:33 PM

:( :(

I still cant get it.

I have elided some stuff to keep it simple.

$projComment = $_POST["projComment"];
   
    $banLive = $_POST["banLive"];
    $banCapp = $_POST["banCapp"];
    $banCat = $_POST["banCat"];
    $postIntranet = $_POST["postIntranet"];
    $eSent = $_POST["eSent"];
    $archFiles = $_POST["archFiles"];
    $progStatus = $_POST["progStatus"];

    $testArray = array (
    $banLive, $banCapp, $banCat,
    $postIntranet, $eSent, $archFiles,
    $progStatus );
   

    foreach( $testArray as $key => $temp )
    {
        if ( $temp == "" )
        {
          $testArray[$key] = "TEMP";
        }
        else
        {
            echo $testArray[$key] = "THIS";
        }
    }

But then when I try and echo the variable out later in the page

eg.

  echo "        <td class='button'>$banLive &nbsp;";
        echo "        </td>  ";
        echo "      <tr> ";

The changes are not saved, and the variable prints out as blank still. :(

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 September 2006 - 12:46 PM

That's because you're not calling the array, you're calling the original values, they're unchanged.

If you wanted to echo the banlive value, you'd need to do echo "$testArray[0]";

Now we know this isn't ideal, as you don't necessarily know what the numeric key is, this is why you should assign easy keys to the array.

Try this:

<?php
$testArray = array (
   'banLive' =>  $banLive,
   'banCapp' => $banCapp,
   'banCat' => $banCat,
   'postIntranet' => $postIntranet,
   'eSent' => $eSent,
   'archFiles' => $archFiles,
   'progStatus' => $progStatus);
   

     foreach( $testArray as $key => $temp )
     {
        if ( $temp == "" )
        {
           $testArray[$key] = "TEMP";
        }
        else
        {
            echo $testArray[$key] = "THIS";
        }
     }
?>

Then when you want to echo the values, use this code: echo $testArray['banLive'];

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 daniel1988

daniel1988
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 21 September 2006 - 12:49 PM

I can't change those original values? :)

#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 September 2006 - 12:53 PM

I've amended my previous post.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#8 daniel1988

daniel1988
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 21 September 2006 - 12:53 PM

Thanks so much mate.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users