Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-06-13
7
Medium Priority
?
272 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 1500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

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

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 111

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 111

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

636 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