0

ok..I'm trying to re-map the keynames of a key-value array in php using a fieldmap array ie. i want the $outRow array to hold $inRow['name1'] = 10 to $outRow['name_1'] = 10 for a large set of pre-mapped values..

 $fieldmap=array("name1"=>"name_1","name2"=>"name_2");
 private function mapRow($inRow) {
 $outRow = array();
 foreach($inRow as $key => $value) {
 $outRow[$this->fieldmap[$key]][] = $value;
 }
 return $outRow;
 } // end mapRow
 public function getListings($inSql) {
 // get data from new table
 $result = mysql_query($inSql);
 if (!result) {
 throw new exception("retsTranslate SQL Error: $inSql");
 }
 while ($row = mysql_fetch_assoc($result)) {
 $outResult[] = $this->mapRow($row);
 }
 return $outResult;
 } // end getListings

this is not working..I'm getting the array but its using $outResult[0][keyname]...I hope this is clear enough :)

Ram Sharma
8,8157 gold badges48 silver badges59 bronze badges
asked Sep 12, 2013 at 17:54
5
  • What goes wrong with your code? Commented Sep 12, 2013 at 17:55
  • see the last line...sorry i forgot to add that part duh Commented Sep 12, 2013 at 17:57
  • hold on..its looking like the way im calling this function is the culprit. im posting some more code. Commented Sep 12, 2013 at 17:59
  • You're missing a $. See this line: if (!result) {. Commented Sep 12, 2013 at 18:20
  • thank you Jason...good find! Commented Sep 12, 2013 at 19:37

3 Answers 3

1
$fieldmap=array("name1"=>"name_1","name2"=>"name_2");
 private function mapRow($inRow) {
 $outRow = array();
 foreach($inRow as $key => $value) {
 $outRow[$this->fieldmap[$key]][] = $value;
 }
 return $outRow;
 } // end mapRow
while ($row = mysql_fetch_assoc($result)) {
 //$outResult[] = $this->mapRow($row);
 $outResult[= $this->mapRow($row);
}

I commented your line of code and added new one..it definitely got what you mentioned in question.

Ram Sharma
8,8157 gold badges48 silver badges59 bronze badges
answered Sep 12, 2013 at 18:20

Comments

0

If you can structure your arrays to where the keys align with the values (see example below) you can use PHP array_combine(). Just know that you will need to make absolutely sure the array is ordered correctly.

<?php
 $fieldmap = array( 'name_1', 'name_2', 'name_3' );
 private function mapRow($inRow)
 {
 $outRow = array_combine( $this->fieldmap, $inRow );
 return $outRow;
 }


For example, if your array was:
array( 'name1' => 10, 'name2' => 20, 'name3' => 30 );

The new result would be:
array( 'name_1' => 10, 'name_2' => 20, 'name_3' => 30 );

Let me know if this helps.

answered Sep 12, 2013 at 18:17

Comments

0

Try this:

function mapRow($inRow) {
$outRow = array();
foreach($inRow as $key => $value) {
 $outRow[preg_replace('/\d/', '_0ドル', $key,1)] = $value;
}
return $outRow;
}
Ram Sharma
8,8157 gold badges48 silver badges59 bronze badges
answered Sep 12, 2013 at 18:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.