Jump to content

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/131313-variable-as-key-to-array/
Share on other sites

<?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

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

 

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

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

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

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.

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.

 

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.