Sphynx01 Posted November 4, 2008 Share Posted November 4, 2008 Is there some setting that prevents you from using a variable as a key to an array? I've got the following code where $sav = "B123456" $machine = "P12.34AB" $machineSAVs = array(); if( ! array_key_exists( $sav, $machineSAVs ) ) $machineSAVs["$sav"] = $machine; Here's what I see when I print_r( $machineSAVs ); Array ( [0] => "P12.34AB" ) I've tried $machineSAVs[$sav], $machineSAVs['$sav'], I've debugged $sav to make sure it was a string, and made double/triple sure that there were no typos. Made sure there were no hidden characters such as carriage returns or anything else in the $sav variable. I'll probably just convert it all to a Class variable anyhows, but sadly, as a programmer, I can't do that until I find out why the heck this doesn't work... Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/131313-variable-as-key-to-array/ Share on other sites More sharing options...
Adam Posted November 4, 2008 Share Posted November 4, 2008 I'm not sure if its just on this code but: $sav = "B123456" $machine = "P12.34AB" $machineSAVs = array(); Are missing ;'s on the end... Do you have error reporting turned off? Quote Link to comment https://forums.phpfreaks.com/topic/131313-variable-as-key-to-array/#findComment-681896 Share on other sites More sharing options...
Adam Posted November 4, 2008 Share Posted November 4, 2008 <?php $sav = "B123456"; $machine = "P12.34AB"; $machineSAVs = array(); if( !array_key_exists($sav, $machineSAVs)) $machineSAVs[$sav] = $machine; print_r($machineSAVs); ?> That works perfect on my work host - which normally has a bit of a strange configuration, so I can't imagine why it's not working on yours? ??? Try looking through phpinfo() for something to do with array keys? Adam Quote Link to comment https://forums.phpfreaks.com/topic/131313-variable-as-key-to-array/#findComment-681897 Share on other sites More sharing options...
Sphynx01 Posted November 4, 2008 Author Share Posted November 4, 2008 Thanks.... I think the problem may be something other than what I originally thought. The problem seems to be the string, it's like there's a hidden character or something in it. I get this to work just fine by taking the supposed string and exploding it, but when I extract that string from the database (working with a Progress DB) it doesn't work. I -see- the string just fine, the is_string() says it's a string, and when I copy paste the string it works fine.... Ie: $innerData = explode( "\t", $result ); $sav = $innerData[1]; $machine = explode("\n", $innerData[2]); $machine = $machine[0]; $machineSAVs[$sav] = $sav; print_r( $results ); print_r( $innerData ); print_r( $machineSAVs ); actually returns: X B123456 P12.34AB Array ( [0] => 9 [1] => B123456 [2] => P12.34AB ) Array ( [0] => B123456 ) BUT If I change the first line to: $innerData = explode( "\t", "X B123456 P12.34AB" ); It works..... o.O Quote Link to comment https://forums.phpfreaks.com/topic/131313-variable-as-key-to-array/#findComment-681954 Share on other sites More sharing options...
Adam Posted November 4, 2008 Share Posted November 4, 2008 You could try using single quotes around the string, that would stop the special characters actually meaning anything to PHP, or if the string is coming from another source where this might not be possible, try adding: $sav = str_replace("\t", "", $sav); Adam Quote Link to comment https://forums.phpfreaks.com/topic/131313-variable-as-key-to-array/#findComment-681963 Share on other sites More sharing options...
Sphynx01 Posted November 4, 2008 Author Share Posted November 4, 2008 No luck. The replace_str won't work because I've already exploded it by tabs. Single quotes don't work because '$result' becomes $result, not the value of the result variable (unless I'm mis-understanding where you are suggesting the single-quotes go). I've tried trim() stripslashes() strip_tags() and a var_dump shows no 'extra' characters in my $result or $sav. this is just bizzare..... Quote Link to comment https://forums.phpfreaks.com/topic/131313-variable-as-key-to-array/#findComment-681968 Share on other sites More sharing options...
Adam Posted November 4, 2008 Share Posted November 4, 2008 Really struggling to udnerstand what is you're trying to do, apart from use $sav as the array key.. Going with "X B123456 P12.34AB" as the value of $result.. Why are you using: $innerData = explode( "\t", $result ); .. when there are 3 spaces as tabs in $result and not actual tabs? Why are you using: $machine = explode("\n", $innerData[2]); .. when the value of $innerData[2] = P12.34AB ? $machine = explode("\n", $innerData[2]); $machine = $machine[0]; .. Is basically a long way of saying $machine = $innerData[2] ? Have you tried putting <pre> tags around $result to see excactly what's in there?? Sorry, just a little confused .. Adam Quote Link to comment https://forums.phpfreaks.com/topic/131313-variable-as-key-to-array/#findComment-682036 Share on other sites More sharing options...
thebadbad Posted November 4, 2008 Share Posted November 4, 2008 Why are you using: $innerData = explode( "\t", $result ); .. when there are 3 spaces as tabs in $result and not actual tabs? The PHP Freaks forum software converts tabs to three spaces. But apart from that, I'm also pretty lost on this topic. To the OP: Output the variables to be sure what they contain. And be sure to "view source" on the output, to see what's actually output - not how the browser interprets it. Quote Link to comment https://forums.phpfreaks.com/topic/131313-variable-as-key-to-array/#findComment-682060 Share on other sites More sharing options...
Sphynx01 Posted November 4, 2008 Author Share Posted November 4, 2008 I've decided to give up on it, spent too much time trying to resolve it already (and it took less than an hour to do it with Classes without having any bugs). But, to answer your questions... Those aren't spaces, those are tabs (I'm getting a tab delimited text from the Progress DB). If you're not seeing Tabs, it's because this forum is converting the strings. The double-talk on $machine is because there's a \n at the end of the line. If you look in the code I posted, you'll see there's an extra line drop after the P12.34AB and before the closing of the array, the closing parenthesis. But nothing of all that changes the fact that $machineSAVs[$sav] = $sav; -should- return Array ( [b123456] => B123456 ) but is instead returning Array ( [0] => B123456 ) I mean, the $sav is $sav for both key and value, it works great in the value slot, but comes up a 0 in the key slot... o.O Even if $sav had all sorts of garble in it, it should work as a key if it's working as a value (after all, you PHP even has a flip function that changes all keys to values and visa versa.... It just makes no sense at all. Quote Link to comment https://forums.phpfreaks.com/topic/131313-variable-as-key-to-array/#findComment-682075 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.