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(). >_> Quote Link to comment 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; } ?> Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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; } } } } Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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); ?> Quote Link to comment 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 ) ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.