2

this below simple code i want to replace [3] to 2010-01-01 and [6] to edited in array fetch. i find function to edit in place with key but 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
)
  • I'm not sure I understand your question. Do you simply require something like `$aRow[3] = '2010-01-01'; $aRow[6] = 'edited';`? – Fabian Tamp May 19 '13 at 13:56
  • Are you trying to do this?... http://stackoverflow.com/questions/240660/in-php-how-do-you-change-the-key-of-an-array-element – Jimbo May 19 '13 at 14:13
  • @Jimbo, yes, but thats is not my answer –  May 19 '13 at 14:27

1 Answers1

0

Using mysql_fetch_array()'s default arguments, your SQL query returns both an indexed and associative array entry for each row.

Firstly, I strongly suggest that you stay away from mysql_* style functions. This set of tools has been deprecated for a while now, and is no longer being updated. There's plenty of resources online which explain why in more detail, and some good alternatives. Alas, let's move on.

Next, I advise that (if you need to use this function) you use one return format method or the other, by passing either MYSQL_ASSOC (associative array) or MYSQL_NUM (numbered array) into the function as it's second argument.

E.g. MYSQL_ASSOC will give you:

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

Generally, this is favored above a numbered array because you don't have to rely on the order that you have selected your columns in within your query.

E.g. MYSQL_NUM will give you:

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

Right. Now, to edit the array in your while loop, you can just add the following lines:

// Using MYSQL_ASSOC

while ( $aRow = mysql_fetch_array( $rResult , MYSQL_ASSOC ) ) {

    $aRow['date_time'] = '2010-01-01';
    $aRow['status'] = 'edited'; // Whatever key this column should be needs to be 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 you still want to stick with both, you'll need to use the lines from 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 this column should be needs to be added instead of 'status'
    $aRow[6] = 'edited';

    $output['aaData'][] = $aRow;

}
Sean
  • 2,278
  • 1
  • 24
  • 45