• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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
0
Mohit_t
Asked:
Mohit_t
  • 3
  • 2
  • 2
1 Solution
 
Marco GasiFreelancerCommented:
What do you want to print? 'M'? Try this:

$search_value = "757"; 

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

Open in new window


If you want the index of the array which holds the searched value then you can try this:

$search_value = "757"; 

for ($i=0; $i < count($map); $i++) 
{ 
foreach($map[$i] as $key=>$cell) 
{ 
if (trim($cell) == trim($search_value)) 
print $i; 
} 
}

Open in new window


I've not tested this code but hope it helps
0
 
Mohit_tAuthor Commented:
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

0
 
Marco GasiFreelancerCommented:
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
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Mohit_tAuthor Commented:
Hi,
Tried the solution still getting the same N value for every record.
0
 
Ray PaseurCommented:
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
Ray PaseurCommented:
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.
0
 
Ray PaseurCommented:
@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
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now