Solved

compare array to string

Posted on 2008-10-30
10
992 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
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.

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

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