kid_drew Posted April 8, 2007 Share Posted April 8, 2007 Hey guys, Ok, so I'm extracting data from my DB and it comes in an array of the following form: Array( [ 0 ] => Array( ['index'] = 'foo' ['value'] = 'foofoo') [ 1 ] => Array( ['index'] = 'bar' ['value'] = 'barbar') ....... This is just an example, and my actual data has many more fields and many more rows. Anyway, what I want is to extract data from this array and put it in another array in the following form: Array( ['foo'] => 'foofoo' ['bar'] =>'barbar') So I want to create an array where the key is 'index' and the only value is 'value'. Is there a php function for that? Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted April 8, 2007 Share Posted April 8, 2007 are you trying to set the value as the key????? im not sure what your looking for... please expand Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 8, 2007 Share Posted April 8, 2007 Try this: <?php $db_array = Array(Array('index' => 'foo', 'value' => 'foofoo'), Array('index' => 'bar', 'value' => 'barbar')); $new_array = array(); for($i=0;$i<count($db_array);$i++) $new_array[$db_array[$i]['index']] = $db_array[$i]['value']; echo '<pre>' . print_r($new_array,true) . '</pre>'; //show what's in the new array ?> Ken Quote Link to comment Share on other sites More sharing options...
kid_drew Posted April 8, 2007 Author Share Posted April 8, 2007 Yeah, I thought there might be a pre-made php function to clean up the code. Some kind of an intersection array flip. Guess not, huh? Try this: <?php $db_array = Array(Array('index' => 'foo', 'value' => 'foofoo'), Array('index' => 'bar', 'value' => 'barbar')); $new_array = array(); for($i=0;$i<count($db_array);$i++) $new_array[$db_array[$i]['index']] = $db_array[$i]['value']; echo '<pre>' . print_r($new_array,true) . '</pre>'; //show what's in the new array ?> Ken Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted April 8, 2007 Share Posted April 8, 2007 if its simply swapping keys for values: foreach($Array $key => $val){ $New_Array[$val] = $key; } that swaps it around hope that helps Quote Link to comment Share on other sites More sharing options...
kid_drew Posted April 8, 2007 Author Share Posted April 8, 2007 That's not it. It's grabbing one column in an assoc array and making it the key list and grabbing another column and making it the values list. Besides that, there is an array swap function in php that does your function. Thanks for the reply, though. if its simply swapping keys for values: foreach($Array $key => $val){ $New_Array[$val] = $key; } that swaps it around hope that helps Quote Link to comment Share on other sites More sharing options...
per1os Posted April 8, 2007 Share Posted April 8, 2007 Try this: <?php $array = array(0 => array('index' => 'foo', 'value' => 'foofoo'), 1 => array('index' => 'bar', 'value' => 'barbar')); // note this should be the array from the db, just did for testing foreach ($array as $arr) { for ($i=0;$i<count($arr);$i++) { if ($arr[$i] == 'index') { $newArr[$arr[$i]] = $arr[$i+1]; $i++; // add one due to the fact we already got the value; } } } print_r($arr); I do not think there is a function in PHP that does what you want because the indexes can be anything. you are asking php to interpret the human language and assign anything with the index to a new array as an index and anything with a value to the new array as that indexes value. That would be some smart code to automatically know what you want it to do. Quote Link to comment Share on other sites More sharing options...
kid_drew Posted April 8, 2007 Author Share Posted April 8, 2007 Well, it seems logical to me that I'm not the first person to need this kind of a function. And I'm not claiming that php would automatically figure out what fields you want. I was thinking that there might be some kind of a function like: $temp_array = array_intersect_flip($myarray, 'index', 'value'); That could grab the 'index' column as the index and the 'value' column as the value and populate an array. I guess there isn't though, which means I just write one myself. Thanks for the help guys. Try this: <?php $array = array(0 => array('index' => 'foo', 'value' => 'foofoo'), 1 => array('index' => 'bar', 'value' => 'barbar')); // note this should be the array from the db, just did for testing foreach ($array as $arr) { for ($i=0;$i<count($arr);$i++) { if ($arr[$i] == 'index') { $newArr[$arr[$i]] = $arr[$i+1]; $i++; // add one due to the fact we already got the value; } } } print_r($arr); I do not think there is a function in PHP that does what you want because the indexes can be anything. you are asking php to interpret the human language and assign anything with the index to a new array as an index and anything with a value to the new array as that indexes value. That would be some smart code to automatically know what you want it to do. 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.