Jump to content

Archived

This topic is now archived and is closed to further replies.

daniel1988

PHP Newbie.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
You're almost there, I'd do it like this...

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

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

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

Share this post


Link to post
Share on other sites
:( :(

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. :(

Share this post


Link to post
Share on other sites
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:

[code]
<?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";
        }
    }
?>
[/code]

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

Regards
Huggie

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.