0

With this code:

if($foo['bar'] == $baz) {
    $results[] = $foo;
}

I never reach the assignment $results[] = $foo; (with a certain value)

Now if I do (with this same, certain value):

strcmp(trim($foo['bar']), trim($baz));

I get a result of 0, indicating the strings are in fact equal. So... why in the world can't I reach $results[] = $foo;?

Shouldn't $foo['bar'] == $baz be true if a strcmp of the two equals 0? What am I missing?


By the way, it's only one particular comparison that fails. Other comparisons using the same if statement will work fine.

example:

when $foo['bar'] == 'meat' and $baz == 'meat' I get $results['meat']

but when $foo['bar'] == 'meat sauce' and $baz == 'meat sauce' I get $results[]

2
  • Can you post the code surrounding this and possibly the real values being sent so it can be tested? thnx. Commented Mar 21, 2011 at 1:24
  • @DeaconDesperado - Got it figured out. I simply included trim() in my test but not in the actual code that was having the trouble, sigh. Commented Mar 21, 2011 at 1:38

2 Answers 2

2

The observed behavior does not make sense, so there has to be a hidden gotcha somewhere. Let's start with the fact that trim seems to make it work.

Did you use var_dump to verify that $foo['bar'] and $baz really have the same value?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jon. I had included trim() in my test but not in the code that was having the problem. When I saw the var_dump I saw that one result was one character longer, went "What the heck? I trimmed that re... uhm, oops". So thanks for the help.
0

If you have tow arrays, say A and B and you want assing array B to A you do this:

$A = $B;

if you want assing just one element:

$A[] = $B[5]; // ether by index or key

in your case you was trying to assing whole array to an element from array, try this:

$results[] = $foo['bar'];

In case you want to assign whole array do like so:

$results['key1'] = $foo[];

Every key in results array will hold array.

1 Comment

Actually, I am trying to assign the whole array like that, not just one value. I am iterating through a multi-dimensional array and building a new array based on whether or not a specific value is present in each sub-array. It's part of a function to filter some returned results.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.