Solved

compare array to string

Posted on 2008-10-30
10
1,005 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 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

728 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