Jump to content

comparing two arrays


Go to solution Solved by Ch0cu3r,

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/291310-comparing-two-arrays/
Share on other sites

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:

10
11
12
13
14

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?

  • Solution

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 />';
}

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.

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

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.

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 by rahjiggah

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

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.

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.