daniel1988 Posted September 21, 2006 Share Posted September 21, 2006 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 https://forums.phpfreaks.com/topic/21521-php-newbie/ Share on other sites More sharing options...
HuggieBear Posted September 21, 2006 Share Posted September 21, 2006 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.RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/21521-php-newbie/#findComment-96006 Share on other sites More sharing options...
daniel1988 Posted September 21, 2006 Author Share Posted September 21, 2006 THANKYOU :D Link to comment https://forums.phpfreaks.com/topic/21521-php-newbie/#findComment-96010 Share on other sites More sharing options...
daniel1988 Posted September 21, 2006 Author Share Posted September 21, 2006 :( :(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 pageeg. echo " <td class='button'>$banLive "; echo " </td> "; echo " <tr> ";The changes are not saved, and the variable prints out as blank still. :( Link to comment https://forums.phpfreaks.com/topic/21521-php-newbie/#findComment-96014 Share on other sites More sharing options...
HuggieBear Posted September 21, 2006 Share Posted September 21, 2006 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'];RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/21521-php-newbie/#findComment-96021 Share on other sites More sharing options...
daniel1988 Posted September 21, 2006 Author Share Posted September 21, 2006 I can't change those original values? :) Link to comment https://forums.phpfreaks.com/topic/21521-php-newbie/#findComment-96023 Share on other sites More sharing options...
HuggieBear Posted September 21, 2006 Share Posted September 21, 2006 I've amended my previous post.RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/21521-php-newbie/#findComment-96025 Share on other sites More sharing options...
daniel1988 Posted September 21, 2006 Author Share Posted September 21, 2006 Thanks so much mate. Link to comment https://forums.phpfreaks.com/topic/21521-php-newbie/#findComment-96027 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.