Jump to content

in_array frustration


cyberbob1uk

Recommended Posts

Hi there,

 

I have queried a database with:

$class_search="33";
$sql="SELECT distinct hauls from temp_hauls_$account where type='loco' and hauls like '$class_search%'";
    $res=mysql_query($sql);
    while($r=mysql_fetch_array($res)){
       $l=$r['hauls'];
       echo "$l<br>";} 

When I echo $l it brings back the 4 results as expected(which are locomotive numbers).

33108
33109
33116
33117

 

A separate query brings all db information relating to each locomotive of the same type and includes a column 'all_nums' which gives every number each locomotive has carried.

 

I have succesfully put 'all_nums' into an array.

  $sql_loco="SELECT * FROM fleet WHERE class='$class_search' ORDER BY c_num ASC";
$result_loco=mysql_query($sql_loco)or die(mysql_error());
while($row_loco=mysql_fetch_array($result_loco)){
$previous=$row_loco['all_nums'];}
$all=str_replace(" ",",",$previous);
   $ary=explode(',',$all);
   for($i=0;$i<count($ary);$i++); 

 

Now, for each $l locomotive, I need to check if its number is in the array for 'all_nums' ($ary), and bring a result for the variable $h.  To do this, I have used

if(in_array($l,$ary)){$h="H";}else{$h="";}

This brings back a result for only one $l locomotive and the value of $h.  What I need it to do is check each $l against $ary and return $h for each.

 

Any ideas would be welcomed with open arms

Link to comment
Share on other sites

That returned

Array
(
    [0] => D6501
    [1] => 33002
)

D6501, 33002,

Array
(
    [0] => D6508
    [1] => 33008
    [2] => 
)

D6508, 33008,

Array
(
    [0] => D6515
    [1] => 33012
    [2] => 
)

D6515, 33012,

Array
(
    [0] => D6530
    [1] => 33018
    [2] => 
)

D6530, 33018,

Array
(
    [0] => D6534
    [1] => 33019
    [2] => 
)

D6534, 33019,

Array
(
    [0] => D6539
    [1] => 33021
    [2] => 
)

D6539, 33021,

Array
(
    [0] => D6543
    [1] => 33025
    [2] => 
)

D6543, 33025,

Array
(
    [0] => D6547
    [1] => 33029
    [2] => 
)

D6547, 33029,

Array
(
    [0] => D6548
    [1] => 33030
    [2] => 
)

D6548, 33030,

Array
(
    [0] => D6552
    [1] => 33034
    [2] => 
)

D6552, 33034,

Array
(
    [0] => D6553
    [1] => 33035
    [2] => 
)

D6553, 33035,

Array
(
    [0] => D6564
    [1] => 33046
    [2] => 
)

D6564, 33046,

Array
(
    [0] => D6566
    [1] => 33048
    [2] => 
)

D6566, 33048,

Array
(
    [0] => D6570
    [1] => 33052
    [2] => 
)

D6570, 33052,

Array
(
    [0] => D6571
    [1] => 33053
    [2] => 
)

D6571, 33053,

Array
(
    [0] => D6575
    [1] => 33057
    [2] => 
)

D6575, 33057,

Array
(
    [0] => D6583
    [1] => 33063
    [2] => 
)

D6583, 33063,

Array
(
    [0] => D6585
    [1] => 33065
    [2] => 
)

D6585, 33065,

Array
(
    [0] => D6513
    [1] => 33102
    [2] => 
)

D6513, 33102,

Array
(
    [0] => D6514
    [1] => 33103
    [2] => 
)

D6514, 33103,

Array
(
    [0] => D6521
    [1] => 33108
    [2] => 
)

D6521, 33108, H

Array
(
    [0] => D6525
    [1] => 33109
    [2] => 
)

D6525, 33109,

Array
(
    [0] => D6527
    [1] => 33110
    [2] => 
)

D6527, 33110,

Array
(
    [0] => D6528
    [1] => 33111
    [2] => 
)

D6528, 33111,

Array
(
    [0] => D6535
    [1] => 33116
    [2] => 
)

D6535, 33116,

Array
(
    [0] => D6536
    [1] => 33117
    [2] => 
)

D6536, 33117,

Array
(
    [0] => D6586
    [1] => 33201
)

D6586, 33201,

Array
(
    [0] => D6587
    [1] => 33202
)

D6587, 33202,

Array
(
    [0] => D6592
    [1] => 33207
)

D6592, 33207,

Array
(
    [0] => D6593
    [1] => 33208
)

 

 

Link to comment
Share on other sites

Hmmm, I did a quick test based on the information you have given me and it would appear it works for me :S

 

Where you have {$h="H"} try putting an echo statement like this... {echo("IM IN THE ARRAY!<br>");}

 

 

Link to comment
Share on other sites

Hmmm, I did a quick test based on the information you have given me and it would appear it works for me :S

 

Where you have {$h="H"} try putting an echo statement like this... {echo("IM IN THE ARRAY!<br>");}

 

 

 

I did that, but only got the echo next to one of the returned values of $l rather than next to each one.

 

This is one of the reasons I love PHP, keeps me away from the missus for hours

//echo "$l<br>";
33109
33116
33103
33117
33108

//echo("<pre>".print_r($ary,true)."</pre>");  
Array
(
    [0] => D6501
    [1] => 33002
)

D6501, 33002,

Array
(
    [0] => D6508
    [1] => 33008
    [2] => 
)

D6508, 33008,

Array
(
    [0] => D6515
    [1] => 33012
    [2] => 
)

D6515, 33012,

Array
(
    [0] => D6530
    [1] => 33018
    [2] => 
)

D6530, 33018,

Array
(
    [0] => D6534
    [1] => 33019
    [2] => 
)

D6534, 33019,

Array
(
    [0] => D6539
    [1] => 33021
    [2] => 
)

D6539, 33021,

Array
(
    [0] => D6543
    [1] => 33025
    [2] => 
)

D6543, 33025,

Array
(
    [0] => D6547
    [1] => 33029
    [2] => 
)

D6547, 33029,

Array
(
    [0] => D6548
    [1] => 33030
    [2] => 
)

D6548, 33030,

Array
(
    [0] => D6552
    [1] => 33034
    [2] => 
)

D6552, 33034,

Array
(
    [0] => D6553
    [1] => 33035
    [2] => 
)

D6553, 33035,

Array
(
    [0] => D6564
    [1] => 33046
    [2] => 
)

D6564, 33046,

Array
(
    [0] => D6566
    [1] => 33048
    [2] => 
)

D6566, 33048,

Array
(
    [0] => D6570
    [1] => 33052
    [2] => 
)

D6570, 33052,

Array
(
    [0] => D6571
    [1] => 33053
    [2] => 
)

D6571, 33053,

Array
(
    [0] => D6575
    [1] => 33057
    [2] => 
)

D6575, 33057,

Array
(
    [0] => D6583
    [1] => 33063
    [2] => 
)

D6583, 33063,

Array
(
    [0] => D6585
    [1] => 33065
    [2] => 
)

D6585, 33065,

Array
(
    [0] => D6513
    [1] => 33102
    [2] => 
)

D6513, 33102,

Array
(
    [0] => D6514
    [1] => 33103
    [2] => 
)

D6514, 33103,

Array
(
    [0] => D6521
    [1] => 33108
    [2] => 
)

IM IN THE ARRAY!
D6521, 33108,

Array
(
    [0] => D6525
    [1] => 33109
    [2] => 
)

D6525, 33109,

Array
(
    [0] => D6527
    [1] => 33110
    [2] => 
)

D6527, 33110,

Array
(
    [0] => D6528
    [1] => 33111
    [2] => 
)

D6528, 33111,

Array
(
    [0] => D6535
    [1] => 33116
    [2] => 
)

D6535, 33116,

Array
(
    [0] => D6536
    [1] => 33117
    [2] => 
)

D6536, 33117,

Array
(
    [0] => D6586
    [1] => 33201
)

D6586, 33201,

Array
(
    [0] => D6587
    [1] => 33202
)

D6587, 33202,

Array
(
    [0] => D6592
    [1] => 33207
)

D6592, 33207,

Array
(
    [0] => D6593
    [1] => 33208
)

D6593, 33208, 

.

Link to comment
Share on other sites

Ok so I am having problems figuring the problem out, my issue is it's this complexity of problem I would need infront of me to nail. On that note, your nested queries are pretty complex, and because fo this your nested loops are pretty complex.

 

Have you considered using one query to get everything? This can be achieved using JOINs and will reduce the amount of code, increase code performance as well as making it simpler to understand.

Link to comment
Share on other sites

Sometimes looking at what your doing as a example, as raw test code can help.

 

what this for please. //for($i=0;$i<count($ary);$i++);

 

example.

 

<?php

$test="
33108,
33109,
33116,
33117";

$test1=explode(',',$test);

if(in_array("33108",$test1)){

echo" That number 33108 is in the array"; 
}

echo "<br><br> Array results!<pre>";
print_r($test1);
echo "</pre>";

?>

Link to comment
Share on other sites

Sometimes looking at what your doing as a example, as raw test code can help.

 

what this for please. //for($i=0;$i<count($ary);$i++);

 

 

This is used while putting the values in column 'all_nums' into an array, it was suggested to do it this way as 'all_nums' could be between 1 and 10 different numbers.

Link to comment
Share on other sites

make it as one array.

 

example

<?php

$test="
33108,
33109,
33116,
33117,";

$test0="
23432,
45676,
67543,
56117";


$search_array="67543";

$test1=explode(',',$test.$test0);

if(in_array($search_array,$test1)){

echo" That number $search_array is in the array"; 
}

echo "<br><br> Array results!<pre>";
print_r($test1);
echo "</pre>";

?>

 

result.

 

That number 67543 is in the array<br><br> Array results!<pre>Array

(

    [0] =>

33108

    [1] =>

33109

    [2] =>

33116

    [3] =>

33117

    [4] =>

23432

    [5] =>

45676

    [6] =>

67543

    [7] =>

56117

)

</pre>

 

 

another example,also getting rid of any matching array numbers the same.

<?php


$test0="
33100,
33109,
33116,
33117,";

$test00="
33108,
33109,
33116,
33117,";

$test000="
23432,
45676,
67543,
56117";


$search_array="67543";

$test1=explode(',',$test0.$test00.$test000);

$test1=array_unique($test1);

if(in_array($search_array,$test1)){

echo" That number $search_array is in the array"; 
}

echo "<br><br> Array results!<pre>";
print_r($test1);
echo "</pre>";

?>

 

 

That number 67543 is in the array<br><br> Array results!<pre>Array

(

    [0] =>

33108

    [1] =>

33100

    [2] =>

33116

    [3] =>

33117

    [5] =>

33109

    [8] =>

23432

    [9] =>

45676

    [10] =>

67543

    [11] =>

56117

)

</pre>

 

Link to comment
Share on other sites

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.