General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 7th, 2003, 06:38 PM
Mojoman's Avatar
Mojoman Mojoman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: Washington, DC
Posts: 39 Mojoman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 34 m 34 sec
Reputation Power: 6
How do I use 1 array to get values from another?

Hello all!

PHP not-so-newbie here! I've got two arrays. One array holds numeric values that I want to correspond with the numeric position of text values in another array - i.e.:

$array1 = array(0, apples, oranges, pears, grapes, bananas, peaches);
$array2 = array(2, 4, 5);

I would like to arrive at this result:

$newarray-or-string = (oranges, grapes, bananas);

any suggestions?

tia - Greg
__________________
$mybrain = "mush";
-------------------------------------------
http://www.loftsboston.com
http://www.gregdawsondesign.com

Reply With Quote
  #2  
Old May 8th, 2003, 02:58 AM
avit avit is offline
Not Yet Perfect
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Squamish, BC
Posts: 111 avit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to avit
PHP Code:
<?php
$array1 
= array(
  
=> 'z'
  
'apples' => 'ap',
  
'oranges' => 'or',
  
'pears' => 'pe'
  
'grapes' => 'gr'
  
'bananas' => 'ba'
  
'peaches' => 'pc'
);
$array2 = array(245);
$array1_keys  array_keys($array1);
foreach (
$array2 as $i) {
  
$new_array[{$array1_keys[$i]}] = $array1[{$array1_keys[$i]}];
}
print_r($new_array);
?>

Not tested... Have a look at the array functions on php.net. There are 2 interesting ones called array_intersect and array_intersect_assoc, which might achieve what you are trying to do more directly. Not sure what your actual application is, though.

Reply With Quote
  #3  
Old May 8th, 2003, 06:41 PM
Mojoman's Avatar
Mojoman Mojoman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: Washington, DC
Posts: 39 Mojoman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 34 m 34 sec
Reputation Power: 6
Hey, avit! Thanks for the response!

Close, but not quite! Your code returns the correct keys, but in a format that I cannot use (I just need the values):

Array
(
[Finnish] =>
[German] =>
[Italian] =>
)

I've looked at php.net - array_intersect and array_intersect_assoc do look as though they might apply. Is there any one who is familiar with these?

Reply With Quote
  #4  
Old May 8th, 2003, 07:58 PM
avit avit is offline
Not Yet Perfect
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Squamish, BC
Posts: 111 avit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to avit
Simple, change one line:
PHP Code:
// This one will be numbered 0,1,2,... sequentially:
$new_array[] = $array1[{$array1_keys[$i]}];

// This one will be numbered 2,4,5 same as $array2:
$new_array[$i] = $array1[{$array1_keys[$i]}]; 

Reply With Quote
  #5  
Old May 9th, 2003, 12:56 PM
Mojoman's Avatar
Mojoman Mojoman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: Washington, DC
Posts: 39 Mojoman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 34 m 34 sec
Reputation Power: 6
I am now an avid avit fan!

Dude, you are the man, thanks!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > How do I use 1 array to get values from another?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway