PHP change array by key in place -


this below simple code want replace [3] 2010-01-01 , [6] edited in array fetch. find function edit in place key thats dont work correctly php:

while ( $arow = mysql_fetch_array( $rresult ) ) {     replace_key('id', '00', $arow);     $output['aadata'][] = $arow; } 

result:

array (     [0] =>aaaaaaa     [title] => bbbbbb     [1] => 86     [id] => 86     [2] => rewr     [subject] => rewr     [3] => 0000-00-00     [date_time] => 0000-00-00     [4] => admin     [username] => admin     [5] =>cccc     [6] =>ddddd ) 

using mysql_fetch_array()'s default arguments, sql query returns both indexed , associative array entry each row.

firstly, suggest stay away mysql_* style functions. set of tools has been deprecated while now, , no longer being updated. there's plenty of resources online explain why in more detail, , alternatives. alas, let's move on.

next, advise (if need use function) use 1 return format method or other, passing either mysql_assoc (associative array) or mysql_num (numbered array) function it's second argument.

e.g. mysql_assoc give you:

array (     [title] => bbbbbb     [id] => 86     [subject] => rewr     [date_time] => 0000-00-00     [username] => admin ) 

generally, favored above numbered array because don't have rely on order have selected columns in within query.

e.g. mysql_num give you:

array (     [0] =>aaaaaaa     [1] => 86     [2] => rewr     [3] => 0000-00-00     [4] => admin ) 

right. now, edit array in while loop, can add following lines:

// using mysql_assoc  while ( $arow = mysql_fetch_array( $rresult , mysql_assoc ) ) {      $arow['date_time'] = '2010-01-01';     $arow['status'] = 'edited'; // whatever key column should needs added instead of 'status'      $output['aadata'][] = $arow;  } 

or:

// using mysql_num  while ( $arow = mysql_fetch_array( $rresult , mysql_num ) ) {      $arow[3] = '2010-01-01';     $arow[6] = 'edited';      $output['aadata'][] = $arow;  } 

if still want stick both, you'll need use lines both examples:

// using default / mysql_both  while ( $arow = mysql_fetch_array( $rresult ) ) {      $arow['date_time'] = '2010-01-01';     $arow[3] = '2010-01-01';      $arow['status'] = 'edited'; // whatever key column should needs added instead of 'status'     $arow[6] = 'edited';      $output['aadata'][] = $arow;  } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -