I have an array $minus

array(3) { [0]=> string(6) "people" 
           [1]=> string(7) "friends" 
           [2]=> string(8) "siblings" 
         }

And I have an array $user

array(3) { ["people"]=> string(3) "100" 
           ["friends"]=> string(2) "10" 
           ["siblings"]=> string(2) "57" 
         }

I can get the values of $user by using the values of $minus like,

echo $user[$minus[0]] . ', ' . $user[$minus[1]] . ', ' . $user[$minus[2]];
// Would echo: 100, 10, 57

But how can I get the values of $user by using the values of $minus into a new array, the new array should be like,

array(3) { [0]=> string(3) "100" 
           [1]=> string(2) "10" 
           [2]=> string(2) "57" 
         }

I have tried using foreach loops but can never get it right?

share|improve this question

feedback

5 Answers

up vote 4 down vote accepted
foreach($minus as $key=>$value) {
  $new_array[$key] = $user[$value];
}
share|improve this answer
Will try this thank you for your answer. – novactown.com Jan 24 at 16:54
Worked brilliantly, thank you for this. Is there any good links for documentation on this? – novactown.com Jan 24 at 16:55
The PHP documentation is always brilliant - php.net/manual/en/control-structures.foreach.php. – Blowski Jan 24 at 16:56
1  
It is better to initialize the array before the foreach. $new_array= array(); – makriria Jan 24 at 16:56
@Michael - yes, I agree. – Blowski Jan 24 at 16:57
show 2 more comments
feedback

Use array_map, PHP >= 5.3 only

$new_array = array_map(function($item) use ($user) {return $user[$item];}, $minus);
share|improve this answer
This solution only works in PHP >= 3.5 – afuzzyllama Jan 24 at 16:52
It returns NULL thanks though. – novactown.com Jan 24 at 16:53
@novactown.com what's your PHP version? – Dor Shemer Jan 24 at 16:53
It does work now thanks – novactown.com Jan 24 at 17:04
Thanks for this, in terms of speed would this be better or a for each loop? I am guessing this as it doesn't need to iterate through the items or does it? – novactown.com Jan 24 at 18:06
show 2 more comments
feedback
$new_array= array();
foreach ($minus as $key => $value){
 $new_array[$key] =  $user[$value];
    }
print_r($new_array);
share|improve this answer
feedback
$new_array = array($user[$minus[0]], $user[$minus[1]], $user[$minus[2]]);
share|improve this answer
I didn't downvote, but it's not the best solution - it only works as long as the keys have that name, and there are exactly 3 members of the array. If the key names or the size of the array change, this will have to be changed. – Blowski Jan 24 at 16:55
feedback
$minus = array(0 => "people",
               1 => "friends",
               2 => "siblings"
              );

$user = array("people"   => "100",
              "friends"  => "10",
              "siblings" => "57"
             );


$newArray = $minus;
array_walk($newArray,function(&$item, $key, $prefix) { $item = $prefix[$item]; },$user);

var_dump($newArray);
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.