loudog Posted August 2, 2011 Share Posted August 2, 2011 hey guy's Im having trouble with writing my own function for array_flip. What im trying to get the same output that the original array_flip does but im not sure if im on the right direction. Here is what i got so far. Plz keep in mind im a newbie. thanks in advance. <?php function dz_flip($trans){ $temp=array("a"=>0,"b"=>1,"c"=>2); $arr=''; for($i=-1; $i<$temp; $i--){ $arr.=$temp[$i]; $temp=array_flip($trans); } return $temp; } Link to comment https://forums.phpfreaks.com/topic/243561-help-am-i-on-the-right-path/ Share on other sites More sharing options...
Nodral Posted August 2, 2011 Share Posted August 2, 2011 Why would you need to do this? Surely array_flip is already a function to do this? Link to comment https://forums.phpfreaks.com/topic/243561-help-am-i-on-the-right-path/#findComment-1250562 Share on other sites More sharing options...
voip03 Posted August 2, 2011 Share Posted August 2, 2011 You will get error Warning: array_flip() [function.array-flip]: The argument should be an array in Link to comment https://forums.phpfreaks.com/topic/243561-help-am-i-on-the-right-path/#findComment-1250585 Share on other sites More sharing options...
voip03 Posted August 2, 2011 Share Posted August 2, 2011 http://www.w3schools.com/php/func_array_flip.asp let me know Link to comment https://forums.phpfreaks.com/topic/243561-help-am-i-on-the-right-path/#findComment-1250638 Share on other sites More sharing options...
loudog Posted August 3, 2011 Author Share Posted August 3, 2011 I need to do this cuz is part of assignment. So im going to try my darnest to do it if not i'll be back here begging for help. :'( Link to comment https://forums.phpfreaks.com/topic/243561-help-am-i-on-the-right-path/#findComment-1251039 Share on other sites More sharing options...
xyph Posted August 3, 2011 Share Posted August 3, 2011 Well, using array_flip in a clone of array_flip seems kind of bad though. You should look at foreach(). It's a great way to loop through arrays. http://php.net/manual/en/control-structures.foreach.php Link to comment https://forums.phpfreaks.com/topic/243561-help-am-i-on-the-right-path/#findComment-1251069 Share on other sites More sharing options...
loudog Posted August 3, 2011 Author Share Posted August 3, 2011 Alright guy's, I think just got me into a situation here. I was able to flip them around with out using the array_flip function but here is the situation. i get both array's the regular and the flip one now i just don't know how to get rid of the regular one and just keep the array that i flip. :-\ Here is the code. thanks in advance. <?php function dz_flip($temp){ /*$arr=''; for($i=0; $i>$temp; $i--){ $arr.=$temp[$i]; }*/ foreach($temp as $key => $value){ $temp[$value]=$key; } return $temp; The output is this Array ( [horse] => 1 [cat] => 2 [dog] => 3 [1] => horse [2] => cat [3] => dog ) } $temp=array("horse"=>1,"cat"=>2,"dog"=>3); print_r (dz_flip($temp)); //print_r(array_flip($temp)); ?> Link to comment https://forums.phpfreaks.com/topic/243561-help-am-i-on-the-right-path/#findComment-1251095 Share on other sites More sharing options...
Buddski Posted August 3, 2011 Share Posted August 3, 2011 Just create an array inside your function that you can add the new "flipped" elements to function dz_flip($temp){ $flipped = array(); foreach($temp as $key=>$value){ $flipped[$value] = $key; } return $flipped; } Link to comment https://forums.phpfreaks.com/topic/243561-help-am-i-on-the-right-path/#findComment-1251227 Share on other sites More sharing options...
loudog Posted August 3, 2011 Author Share Posted August 3, 2011 Its the array_flip is done and Thank you guys for helping me.......I love this site. Link to comment https://forums.phpfreaks.com/topic/243561-help-am-i-on-the-right-path/#findComment-1251320 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.