Link to home
Start Free TrialLog in
Avatar of Mohit_t
Mohit_t

asked on

Finding a value in a multi-dimensional associative array - PHP

Hi,
I am creating a multidimensional array (from sql query using mysql_fetch_assoc),
I get the following array, see attached array_file. Now from this array I have to find if a particular field exists in the array or not, particulary i need to find if the value for element [M] exists or not ([M] => 757).
I have the following code, but always get a negative value even if the record exists, just need to find if the record exists with [M] => 757 or not,
some elements of the array might have null values.

$search_value = "757"; 

foreach ($map as $key => $row) 
{ 
foreach($row as $cell) 
{ 
if (trim($cell) == trim($search_value)) 
print $key; 
} 
}

Open in new window

I am stuck at this point, appreciate any help.

Thanks
array-file.txt
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mohit_t
Mohit_t

ASKER

Hi  marqusG,
Thanks for such a quick reply. I realize that I did not mention a key part of the issue.
I have two such multidimentional arrays. So I need to find if the value of M in array1 exists in array2
I wrapped your code around a for loop like below, but this always gives be N for recordExists
for($h=0; $h < count($surveyRpt1); $h++){
$to_search = $surveyRpt1[$h]['M'];
foreach ($surveyRpt3 as $arr) 
{ 
foreach($arr as $key=>$cell) 
{ 
if (trim($cell) == trim($to_search)) 
$surveyRpt1[$h]['recordExists'] = 'Y';
else
$surveyRpt1[$h]['recordExists'] = 'N';
} 
}
}

Open in new window

I always get the value of recordExists as N
[149] => [h] => abcd
            [NS] => q3j
            [B] => H345ty
            [Name] => John,E
            [M] => 757
            [A] => 55
            [S] => F
            [Admin_Date] => 2013-06-10
            [Admin_test] => Jun 10 2013  7:10PM
            [Adm_test_score] => 10
            [Adm_last_score] => 2
            [adm_test_attempts] => 14
            [Qualified_for_admin_test] =>  
            [previous_certifications] => Maintained
            [redistribution_office] => Yes
            [position] => Independnt
            [b] => 2
            [c] =>  
            [d] =>  
            [recordExists] => N

Open in new window

Try this:

for($h=0; $h < count($surveyRpt1); $h++){
$to_search = $surveyRpt1[$h]['M'];
foreach ($surveyRpt3 as $arr) 
{ 
  if (in_array($to_search, $arr)){ 
    $surveyRpt1[$h]['recordExists'] = 'Y';
  }else{
    $surveyRpt1[$h]['recordExists'] = 'N';
  } 
}
                                           

Open in new window


In addition your code has a syntax error (missing brackets):

for($h=0; $h < count($surveyRpt1); $h++){
$to_search = $surveyRpt1[$h]['M'];
foreach ($surveyRpt3 as $arr) 
{ 
  foreach($arr as $key=>$cell) 
  { 
    if (trim($cell) == trim($to_search)) 
    {
      $surveyRpt1[$h]['recordExists'] = 'Y';
     }else
    {
      $surveyRpt1[$h]['recordExists'] = 'N';
    } 
  }
}
                                            

Open in new window


Cheers
Avatar of Mohit_t

ASKER

Hi,
Tried the solution still getting the same N value for every record.
If you're using the MySQL extension, you have a much bigger problem than just finding something in an array.  PHP is removing MySQL support.  This article explains why and what you must do to keep your scripts running.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
Here is the test data.  It's helpful to use the code snippet feature for things like this because we get a unispace font and line numbers that facilitate discussion.

Array
(
    [0] => Array
        (
            [h] => abcd
            [NS] => q3j
            [B] => H345ty
            [Name] => John,E
            [M] => 757
            [A] => 55
            [S] => F
            [Admin_Date] => 2013-06-10
            [Admin_test] => Jun 10 2013  7:10PM
            [Adm_test_score] => 10
            [Adm_last_score] => 2
            [adm_test_attempts] => 14
            [Qualified_for_admin_test] =>  
            [previous_certifications] => Maintained
            [redistribution_office] => Yes
            [position] => Independnt
            [b] => 2
            [c] =>  
            [d] =>  
        )

    [1] => Array
        (
            [h] => abcd
            [NS] => q3j
            [B] => H345ty
            [Name] => John,B
            [M_no] => 758
            [A] => 55
            [S] => F
            [Admin_Date] => 2013-06-10
            [Admin_test] => Jun 10 2013  7:10PM
            [Adm_test_score] => 10
            [Adm_last_score] => 2
            [adm_test_attempts] => 14
            [Qualified_for_admin_test] =>  
            [previous_certifications] => Maintained
            [redistribution_office] => Yes
            [position] => Independnt
            [b] => 2
            [c] =>  
            [d] =>  
        )

    [2] => Array
        (
            [h] => abcd
            [NS] => q3j
            [B] => H345ty
            [Name] => John,C
            [M] => 759
            [A] => 55
            [S] => F
            [Admin_Date] => 2013-06-10
            [Admin_test] => Jun 10 2013  7:10PM
            [Adm_test_score] => 10
            [Adm_last_score] => 2
            [adm_test_attempts] => 14
            [Qualified_for_admin_test] =>  
            [previous_certifications] => Maintained
            [redistribution_office] => Yes
            [position] => Independnt
            [b] => 2
            [c] =>  
            [d] => 
        )

    [3] => Array
        (
            [h] => abcd
            [NS] => q3j
            [B] => H345ty
            [Name] => John,Q
            [M] => 456
            [A] => 55
            [S] => F
            [Admin_Date] => 2013-06-10
            [Admin_test] => Jun 10 2013  7:10PM
            [Adm_test_score] => 10
            [Adm_last_score] => 2
            [adm_test_attempts] => 14
            [Qualified_for_admin_test] =>  
            [previous_certifications] => Maintained
            [redistribution_office] => Yes
            [position] => Independnt
            [b] => 2
            [c] =>  
            [d] => 
        )

Open in new window

From that we can see that we have an array consisting of associative arrays.  If this data came from a query results set, each of the "sub-arrays" would be expected to have the same keys.  

In array position zero we have [h], [NS], [Name], [M], [A], etc.
In array position one we have [h], [NS], [Name], [M_no], [A], etc.

You mentioned a key named recordExists, but this is nowhere in the sample data.  This causes me to think that we are not looking at the actual results set from the query.  Maybe the data has been copied and redacted?  Can you please create the true SSCCE and post it here?  Thank you.
@Mohit_t... Did you test the solution you accepted?  If you did and it worked, I cannot understand why you would give a grade of "B" to a working code sample.  If you did not test the solution, I do not understand why you accepted the answer, especially since you said that the question omitted important information.

Here are the grading guidelines.  I think you owe us an explanation.  Thanks!
http://support.experts-exchange.com/customer/portal/articles/481419