array search part 2 [message #183752] |
Mon, 18 November 2013 14:10  |
Mr Oldies
Messages: 241 Registered: October 2013
Karma: 0
|
Senior Member |
|
|
I am attempting to search the array for matching entries.
All matches, not just the first.
While the code works fine, the result is not what I want.
$btrack should return the value of six0[$track][0].
Instead, the result shows the array key number.
Even if I change the [0] to [1].
Why?
<?php
$track=$_GET['item'];
echo $track;
$flip=$six0[$track][1];
echo $flip;
echo "<br>";
$max=count($six0);
$arow=0;
while ($max>$arow){
$btrack=$six0[$arow][0];
if ($btrack=$track){echo $btrack;}
$arow++;
}
?>
Result example.
468The Happy Heart Of Paris
468468468468468468468468468468468468468............
|
|
|
|
|
|
|
|
|
|
|
Re: array search part 2 [message #183772 is a reply to message #183757] |
Mon, 18 November 2013 18:08   |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 11/18/2013 10:05 AM, Tim Streater wrote:
> In article <93xuovoqvo59(dot)1wcgm8i5gn2hb$(dot)dlg(at)40tude(dot)net>, richard
> <noreply(at)example(dot)com> wrote:
>
>> I am attempting to search the array for matching entries.
>> All matches, not just the first.
>> While the code works fine, the result is not what I want.
>> $btrack should return the value of six0[$track][0].
>> Instead, the result shows the array key number.
>> Even if I change the [0] to [1].
>> Why?
>
> For the 17 billionth time, you thick dope, you have used = instead of ==
>
A bit of an exaggeration, Tim. It's only been 16,364,196,273 times. :)
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
|
|
|
|
|
Re: array search part 2 [message #183784 is a reply to message #183760] |
Tue, 19 November 2013 08:27   |
Arno Welzel
Messages: 317 Registered: October 2011
Karma: 0
|
Senior Member |
|
|
richard, 2013-11-18 17:06:
> On Mon, 18 Nov 2013 15:05:52 +0000, Tim Streater wrote:
>
>> In article <93xuovoqvo59(dot)1wcgm8i5gn2hb$(dot)dlg(at)40tude(dot)net>, richard
>> <noreply(at)example(dot)com> wrote:
>>
>>> I am attempting to search the array for matching entries.
>>> All matches, not just the first.
>>> While the code works fine, the result is not what I want.
>>> $btrack should return the value of six0[$track][0].
>>> Instead, the result shows the array key number.
>>> Even if I change the [0] to [1].
>>> Why?
>>
>> For the 17 billionth time, you thick dope, you have used = instead of ==
>>
[...]
>>> $btrack=$six0[$arow][0];
>>> if ($btrack=$track){echo $btrack;}
>>> $arow++;
>>> }
>>>
>>>
>>> ?>
>
> You thicker DOPE! IT MAKES NO FRICKIN DIFFERENCE!
> And the examples I have seen at php.net all use a single =.
The examples DO NOT MATTER!
Read THIS:
<http://us3.php.net/manual/en/language.operators.php>
--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
|
|
|
|
|
|
|
|
|
|
|
|
Re: solved [message #183794 is a reply to message #183792] |
Tue, 19 November 2013 18:15   |
Ben Bacarisse
Messages: 82 Registered: November 2013
Karma: 0
|
Member |
|
|
richard <noreply(at)example(dot)com> writes:
> On Tue, 19 Nov 2013 13:50:02 +0000, Ben Bacarisse wrote:
>
>> richard <noreply(at)example(dot)com> writes:
>>
>>> <?php
>>>
>>> $track=$_GET['item'];
>>>
>>>
>>> $asong=$six0[$track][0];
>>> $flip=$six0[$track][1];
>>>
>>> $max=count($six0);
>>>
>>> $match=0;
>>>
>>> for ($item=0;$item<=$max;$item++){
>>> if ($flip==$six0[$item]['0'])
>>> {if ($six0[$item][2]=$six0[$track][2])
>>
>> There's no need to assign something to itself just to test it. The
>> test:
>
> If you'd bother to comprehend what is happening, it is not assigning
> itself.
Clam down. I miss-read the line, it's not a question of not bothering.
> $item is the value derived from the loop.
> $track is the fixed valued assigned at the time of $_GET.
> so if $item and $track are equal then the statement is true and the result
> assigns the value to $match and that is what is used to display the proper
> item.
No, that's not what happens. The condition is true if and only if
$six0[$track][2] is considered to be true (i.e. not null, false, 0, 0.0,
"", "0", array() or an empty object).
As a side effect, one or more elements of $six0 have their [2] component
set to $six0[$track][2] depending on exactly what data there is in the
array.
Two other points: (1) you access outside the array but running the loop up
to and including $max, and (2) using '0' when you mean 0 is confusing.
It works, but it's not as clear as it should be.
> If $track=1 and $item=101 and the pairs match in value, then $match gets
> the number to use.
--
Ben.
|
|
|
|
|
|
Re: solved [message #183806 is a reply to message #183796] |
Wed, 20 November 2013 15:02  |
Arno Welzel
Messages: 317 Registered: October 2011
Karma: 0
|
Senior Member |
|
|
Am 19.11.2013 20:41, schrieb richard:
> On Tue, 19 Nov 2013 17:43:25 +0100, Arno Welzel wrote:
>
>> Am 19.11.2013 03:45, schrieb richard:
>>> <?php
>>>
>>> $track=$_GET['item'];
>>>
>>>
>>> $asong=$six0[$track][0];
>>> $flip=$six0[$track][1];
>>>
>>> $max=count($six0);
>>>
>>> $match=0;
>>>
>>> for ($item=0;$item<=$max;$item++){
>>> if ($flip==$six0[$item]['0'])
>>> {if ($six0[$item][2]=$six0[$track][2])
>>> {$match=$item;}}
>>> }
>>
>> PLEASE format your code properly!
>>
>> for($item=0; $item <= $max; $item++)
>> {
>> if($flip == $six0[$item]['0'])
>> {
>> if($six0[$item][2] = $six0[$track][2])
>> {
>> $match = $item;
>> }
>> }
>> }
>>
>> And why this?
>>
>> if($six0[$item][2] = $six0[$track][2])
>>
>> Please note: "=" is NOT the same as "=="!
>>
>> If you want to COMPARE $six0[$item][2] and $six0[$track][2] you MUST write:
>>
>> if($six0[$item][2] == $six0[$track][2])
>
> The script works if it were on one line or 100 lines.
READ what I wrote. I said nothing about the question, if your code works
or not - but I doubt, that you really *know* what the difference is between:
if($six0[$item][2] = $six0[$track][2])
and
if($six0[$item][2] == $six0[$track][2])
Do you? Can you explain, why your code uses the assignement and not the
comparison?
Some how it feels if I already explained this a dozen times and I just
forgot that...
> You've never seen a page where the source code was 10,000 characters long
> all on one single line?
I did. And you don't see any difference in readability above in your
version and my formatted version?
Also see here:
<http://arnowelzel.de/wiki/lib/exe/js.php>
But this does not mean, that such code is *readable* nor that I
recommend to write code this way.
In this special case (<http://arnowelzel.de/wiki/lib/exe/js.php>) it is
a "compressed" JavaScript to save bandwidth and lower the number of HTTP
requests needed. But this is constructed dynamically by js.php which
takes the local JavaScript files and creates a big "compressed" version
of them in one single block.
But for scripts which remains on the server and will never be
transmitted to the client it should be *readable* for *humans*.
--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
|
|
|