Solved

compare array to string

Posted on 2008-10-30
10
986 Views
Last Modified: 2013-12-13
Hi all,
I have a php mysql app I am working on.  I have a string from a field in mysql and I also have a array from a mysql query.  The array has an ID and a Name field.  The string is a ~ delimited list of ID (the same ID's from the query.  What I am trying to do, is when I explode my string, I want to replace the ID from the string with the Name from the array using the arrays ID to match with the string ID.
Hope this makes sense... here's what I have so far.
// my array:
$query="SELECT * FROM department";
$result=mysql_query($query);
$deptArray = array();
while($row = mysql_fetch_array($result))
{
$a1 = array($row[department_id] => $row[department_name], $row[department_id]);
$deptArray = array_merge($a1, $deptArray);
}
 
//my string so far (not quite working)...
$str = $dealers_type;
$str2 = substr($str, 0, -1);
$data = explode("~",$str2);
foreach($data as $k => $v)
{
echo $deptArray[$v].' = '.[department_name.'<br>';
}

Open in new window

0
Comment
Question by:newbe101
  • 6
  • 4
10 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22847334
Wow. I'm sure if this had been explained in English it would have been simple to solve!
0
 
LVL 82

Expert Comment

by:hielo
ID: 22847344
Why do you have:
$a1 = array($row[department_id] => $row[department_name], $row[department_id]);

you already have the department_id -- it's the key.  Did you simply mean:
$a1 = array($row[department_id] => $row[department_name] );

0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22847358
my guess would be:
// my array:
$query="SELECT * FROM department";
$result=mysql_query($query);
$deptArray = array();
while($row = mysql_fetch_array($result))
{
	$a1 = array($row['department_id'] => $row['department_name'] );
	$deptArray = array_merge($a1, $deptArray);
}
 
//my string so far (not quite working)...
$str = $dealers_type;
$str2 = substr($str, 0, -1);
$data = explode("~",$str2);
foreach($data as $k => $v)
{
	echo $v . ' = '.$deptArray[$v].'<br>';
}

Open in new window

0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:newbe101
ID: 22852572
really close... the only problem is that the ID ends up being the key of the array and not the ID of the item.  Sorry about not explaining it well.
0
 

Author Comment

by:newbe101
ID: 22852579
Because $v ends up being the key for the array, rather than the ID of the item, everything is mixed up.
0
 

Author Comment

by:newbe101
ID: 22852624
Here is some code to explain what is happening now... look at the code and you will see that the array key is not the ID for the department.
//multiselect box (this is where the numbers for $dealers_type come from
 
<option value="1~">Accounting and Billing</option>
<option value="9~">Consignment Center</option>
<option value="10~">Finance</option>
<option value="2~">Human Resources</option>
<option value="6~">Merchandise Sales</option>
<option value="7~">Other Vehicle Sales</option>
<option value="5~">Porsche Sales</option>
<option value="4~">Replica Sales</option>
<option value="3~">Service and Repair</option>
 
// here is the print_r from my array:
Array ( 
[0] => Finance 
[1] => Consignment Center 
[2] => Other Vehicle Sales 
[3] => Merchandise Sales 
[4] => Porsche Sales 
[5] => Replica Sales 
[6] => Service and Repair 
[7] => Human Resources 
[8] => Accounting and Billing 
)

Open in new window

0
 

Author Comment

by:newbe101
ID: 22852833
you got me going in the right direction, thanks.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22852870
glad to help.
0
 

Author Comment

by:newbe101
ID: 22852909
take a look a the following question if you want to help me finish this up:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23866072.html
0
 

Author Comment

by:newbe101
ID: 22853116
nevermind about the other question... I figured it out.  Since the array worked perfectly if I made department_name => deparment_id, I ended up using $deptArray2 = array_flip($deptArray); to get around it.  Thanks again for your help.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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.

809 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