Jump to content

PHP Newbie.


daniel1988

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.

Link to comment
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
Link to comment
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. :(
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.