Solved

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

Posted on 2013-06-13
7
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 31

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 31

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

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

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 110

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 110

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Inserting results of While loop to sql server 5 38
Pull Variable from URL  Use php template 1 36
Multi line FPDF footer: 3 30
Make checkboxes execute queries on click 8 37
This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

730 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