rahjiggah Posted September 26, 2014 Share Posted September 26, 2014 Hello I have two arrays of mysql values $a1, $b1 I am trying to loop through each $a value, no problem, but I want to see if each element in $a = one of the values in $b so: $a = array(1, 2, 3, 4, 5, 6); $b = array(4, 6); $y = ''; foreach ($a1 as $a){ foreach ($b1 as $b){ if ($a[val] == $b[val]){ $y = '*'; } } echo $y.$a.'<br />'; } I was kinna hoping it would give me 1 2 3 *4 5 *6 Its most definitely not though... Im sure Im flawed in my logic but have coded so much other crap I just can't see the answer! ty for the look/help. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 26, 2014 Share Posted September 26, 2014 Rather have the second foreach use in_array to check to see if the current value is in the second array. Example foreach($a as $aVal) { if(in_array($aVal, $b)) echo "*"; echo "$aVal<br />"; } Quote Link to comment Share on other sites More sharing options...
rahjiggah Posted September 26, 2014 Author Share Posted September 26, 2014 hmmmm ya I tried that one as well, didnt work Here is the actual array via print_r $uc_sid = Array ( [0] => Array ( [sid] => 11 [0] => 11 ) [1] => Array ( [sid] => 12 [0] => 12 ) ) $sectors = Array ( [0] => Array ( [sid] => 10 [0] => 10 [sSector] => Pharma [1] => Pharma ) [1] => Array ( [sid] => 11 [0] => 11 [sSector] => Energy [1] => Energy ) [2] => Array ( [sid] => 12 [0] => 12 [sSector] => Mining [1] => Mining ) [3] => Array ( [sid] => 13 [0] => 13 [sSector] => Special Situations [1] => Special Situations ) [4] => Array ( [sid] => 14 [0] => 14 [sSector] => Technology [1] => Technology ) ) foreach ($sectors as $s){ if(in_array($s[sid], $uc_sid)){ $y='*'; } echo $y.$s[sid].'<br />'; } just gives me: 1011121314 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 26, 2014 Share Posted September 26, 2014 Why did you show us two simple one dimensional arrays and ask for help with them? Your latest post shows you are using two multi-dimensional arrays so OF COURSE the solution presented didn't work. Rather silly of you to waste people's time, isn't it? Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted September 26, 2014 Solution Share Posted September 26, 2014 Yea it does help if you showed your actual array structure when you posted orginally But anyway. If are using PHP5.5 or greater you could use $uc_sids = array_column($uc_sid, 'sid'); foreach ($sectors as $sector) { $y = in_array($sector['sid'], $uc_sids) ? '*' : ''; echo $y.$sector['sid'].'<br />'; } Otherwise you'd need to do something like foreach ($sectors as $sector) { $y = ''; // reset $y to empty string for each value foreach($uc_sid as $uc) { if($sector['sid'] == $uc['sid']) $y = '*'; } echo $y.$sector['sid'].'<br />'; } Quote Link to comment Share on other sites More sharing options...
Barand Posted September 26, 2014 Share Posted September 26, 2014 Also: firstly, you could have simplified your arrays by using xxx_fetch_assoc() or xxx_fetch_row. By using xxx_fetch_array() you get the values twice. second, you could probably have done the matching with your query instead of having to create the two arrays. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 26, 2014 Share Posted September 26, 2014 second, you could probably have done the matching with your query instead of having to create the two arrays. Yeah, but that would have probably required a JOIN! Quote Link to comment Share on other sites More sharing options...
rahjiggah Posted September 27, 2014 Author Share Posted September 27, 2014 Great, thanks for the help, and the snarky answers I prefaced this whole thing at the top... so go ahead and give'r Im so tired and batsh!t crazy right now its all good. Again thanks for taking the time to answer the question. Quote Link to comment Share on other sites More sharing options...
Frank_b Posted September 27, 2014 Share Posted September 27, 2014 (edited) The whole point is that the results from the database (if we are talking about multiple records) are stored in two dimensional arrays. Therefor you are not able to simply use 'in_array()' . This will do: foreach($sectors as $sector) { foreach($uc_sid as $uc) { if($sector['sid'] == $uc['sid']) { echo 'found: ' . $sector['sid'] . '<br>'; } } } Edited September 27, 2014 by Frank_b Quote Link to comment Share on other sites More sharing options...
rahjiggah Posted September 27, 2014 Author Share Posted September 27, 2014 foreach ($sectors as $sector) { $y = ''; // reset $y to empty string for each value foreach($uc_sid as $uc) { if($sector['sid'] == $uc['sid']) $y = '*'; } echo $y.$sector['sid'].'<br />'; } This did it, and was what I had before I asked, Im an idiot, it didnt work because I was declaring $y=''; outside the code block! thank you guys.... Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 27, 2014 Share Posted September 27, 2014 if you do this using php code, rather than in the query, i would either fetch or convert the $uc_sid values to a one dimensional array of values before the start of the loop (it could contain a substantial number of values, up to and including the entire $sectors array), rather than to repeatedly loop over it inside of another loop. Quote Link to comment Share on other sites More sharing options...
rahjiggah Posted September 27, 2014 Author Share Posted September 27, 2014 (edited) Ya I really couldnt figure out a way to do this with the sql query to be honest, I know that would be a more efficient way to do this its for editing choices in a form, there are 5 sectors, so query 1 calls the sector_id and name from that table, query 2 calls the persons choices from the choice table. then I just display all the sectors and used the second foreach like Ch0cu3r showed, but it would check all the sector boxes, It was because I was declaring $y=''; outside the first foreach block leaving meaning if it hit a match it was just showing checked for all. Edited September 27, 2014 by rahjiggah Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2014 Share Posted September 27, 2014 The query would look like this. $sql = "SELECT s.sector_id s.sector_name c.person_id FROM sector s LEFT JOIN choice c ON s.sector_id = c.sector_id AND c.person_id = $person"; Where person_id has a value (ie not null) then that person has chosen the sector Quote Link to comment Share on other sites More sharing options...
rahjiggah Posted September 28, 2014 Author Share Posted September 28, 2014 The query would look like this. $sql = "SELECT s.sector_id s.sector_name c.person_id FROM sector s LEFT JOIN choice c ON s.sector_id = c.sector_id AND c.person_id = $person"; Where person_id has a value (ie not null) then that person has chosen the sector Awesome! Just use the join, I get it now, thanx. Quote Link to comment 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.