Solved

compare array to string

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

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Backup Your Microsoft Windows Server®

Backup 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.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to count occurrences of each item in an array.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now