?
Solved

compare array to string

Posted on 2008-10-30
10
Medium Priority
?
1,008 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
[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
  • 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 2000 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
In this article, we’ll look at how to deploy ProxySQL.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses

771 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