Solved

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

Posted on 2013-06-13
7
261 Views
Last Modified: 2013-07-24
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
Comment
Question by:Mohit_t
  • 3
  • 2
  • 2
7 Comments
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 39246636
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
 

Author Comment

by:Mohit_t
ID: 39246690
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
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39246705
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Mohit_t
ID: 39246787
Hi,
Tried the solution still getting the same N value for every record.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39246895
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39246912
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39352456
@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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now