Jump to content

Array manipulation


kid_drew

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.  8)

 

if its simply swapping keys for values:

 

foreach($Array $key => $val){

  $New_Array[$val] = $key;

}

 

that swaps it around

 

 

hope that helps

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.