Hi Guys,

I want to take the result of a query (I'm expecting several records containing several fields as the result) and then add an extra value as a field to make a new array.

So my understanding is I can create an array of each record using something like this:


while ($row = mysqli_fetch_row($result)) {
    $array[] =$row; 
} 

which would give me something like this:

Array ( [0] => Array ( [field 1] => contents field 1 
			      [field 2] => contents field 2					
		              [field 2] => contents field 3)

  [1] => Array ( [field 1] => contents field 1 
		      [field 2] => contents field 2					
	              [field 2] => contents field 3)

How can I then add an extra field to each record in the array, i.e. a field 3? So fields 1 and 2 are from the result set but then I might say if field 2 is > 0 then set field 3 to "Yes" for example and then add it to that record within the array? So I would go from the array above to this:

Array ( [0] => Array ( [field 1] => contents field 1 
			      [field 2] => contents field 2					
		              [field 2] => contents field 3)

  [1] => Array ( [field 1] => contents field 1 
		      [field 2] => contents field 2					
	              [field 2] => contents field 3)
       [2] => Array ( [field 1] => contents field 1 
		      [field 2] => contents field 2					
	              [field 2] => contents field 3)

I hope that makes sense.
Thanks,
Tim.

    Perhaps you could do something along the lines of:

    while ($row = mysqli_fetch_row($result)) {
        $row[2] = ($row[1] > 0) ? 'Yes' : 'No';
        $array[] = $row;
    }

      You may find that using mysqli_fetch_row is confusing because it uses numeric indexes. It may be easier to understand if you use mysqli_fetch_array:

      while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        // blah blah blah
      }
        Write a Reply...