DarkWater Posted April 22, 2008 Share Posted April 22, 2008 I know that I should know this, because I feel like there is a function like it, but I can't remember if there is. Is there a function that takes the values of an array and turns it into keys for that array, if the values are strings? You know, now that I think about it, that does sound odd. There might not even be a function that does it. I'll code one if there isn't. Basically, I need an opposite version of array_keys(). >_> Link to comment https://forums.phpfreaks.com/topic/102342-switching-values-with-keys/ Share on other sites More sharing options...
mofm Posted April 22, 2008 Share Posted April 22, 2008 i think ur after somthing like this ? <? foreach($_GET as $key => $value) { $$key = $value; } ?> Link to comment https://forums.phpfreaks.com/topic/102342-switching-values-with-keys/#findComment-524023 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Author Share Posted April 22, 2008 i think ur after somthing like this ? <? foreach($_GET as $key => $value) { $$key = $value; } ?> Almost. But I'm going to make sure each string is shorter than a certain # of chars and some other things if I do write a function for it. Link to comment https://forums.phpfreaks.com/topic/102342-switching-values-with-keys/#findComment-524032 Share on other sites More sharing options...
jonsjava Posted April 22, 2008 Share Posted April 22, 2008 str_split splits a value into an array. Link to comment https://forums.phpfreaks.com/topic/102342-switching-values-with-keys/#findComment-524038 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Author Share Posted April 22, 2008 str_split splits a value into an array. Does it take every array value and turn it into a key, and turn the key into the value? >_> Basically flipping an array. I don't think so. Link to comment https://forums.phpfreaks.com/topic/102342-switching-values-with-keys/#findComment-524039 Share on other sites More sharing options...
mofm Posted April 22, 2008 Share Posted April 22, 2008 its not hard to make the code below a function foreach($_GET as $key => $value) { { if($value<=10) { if(is_string($value)) { $$key = $value; } } } } Link to comment https://forums.phpfreaks.com/topic/102342-switching-values-with-keys/#findComment-524040 Share on other sites More sharing options...
jonsjava Posted April 22, 2008 Share Posted April 22, 2008 and you can use <?php $number = strlen($value); if ($number > { //do something } else{ //do something else } and about str_split. nope, sry. Link to comment https://forums.phpfreaks.com/topic/102342-switching-values-with-keys/#findComment-524041 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Author Share Posted April 22, 2008 Guys, I know how to make the function, I just wanted to know if there was one already. xD I guess not. Thanks. Link to comment https://forums.phpfreaks.com/topic/102342-switching-values-with-keys/#findComment-524045 Share on other sites More sharing options...
mofm Posted April 22, 2008 Share Posted April 22, 2008 well you did post it in the "PHP HELP" section Link to comment https://forums.phpfreaks.com/topic/102342-switching-values-with-keys/#findComment-524050 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Author Share Posted April 22, 2008 And I needed help with finding out if there was a function that does it, lol. I'll write one and post it here so people can use it if they need it, and then I'll mark the topic as solved. Be right back. Link to comment https://forums.phpfreaks.com/topic/102342-switching-values-with-keys/#findComment-524055 Share on other sites More sharing options...
Fadion Posted April 22, 2008 Share Posted April 22, 2008 I know u know how to write it, but im posting my solution of swapping keys with values : <?php $arr = array('me' => 'guiltygear', 'you' => 'darkwater'); $keys = array_keys($arr); $values = array_values($arr); $arr = array_combine($values, $keys); ?> Link to comment https://forums.phpfreaks.com/topic/102342-switching-values-with-keys/#findComment-524062 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Author Share Posted April 22, 2008 OH, there is an array_values() function. That would have helped. But I finished already. =D <?php function flip_array($array_flip, $max_lengthofkey=10) { //Usage: array flip_array(array $array_flip, scalar $max_lengthofkey) if ((!is_array($array_flip)) || (!is_scalar($max_lengthofkey))) { return false; } foreach ($array_flip as $k => $v) { if (!is_string($v)) { continue; } if (strlen($v) > $max_lengthofkey) { $v = substr($v, 0, $max_lengthofkey); } $new_array[$v]=$k; } return $new_array; } $oldarray = array("test"=>"dog", "test2"=>"cat"); $newarray=flip_array($oldarray); print_r($newarray); //Outputs Array ( [dog] => test [cat] => test2 ) ?> Link to comment https://forums.phpfreaks.com/topic/102342-switching-values-with-keys/#findComment-524066 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.