How to add custom value to mysql fetch array

sohal07

I have an array fetched from mysql.

while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
    $array[] = $row;
}
var_dump($array);

It returns the values as below:

array (size=2)
  0 => 
    array (size=4)
      'itemId' => string '4' (length=1)
      'name' => string 'Ice Break' (length=9)
      'size' => string '500ml' (length=5)
      'supplier' => string 'Parmalat' (length=9)
  1 => 
    array (size=4)
      'itemId' => string '6' (length=1)
      'name' => string 'Red bull' (length=9)
      'size' => string '250ml' (length=5)
      'supplier' => string 'Red Bull' (length=9)

Now, I want to add a customised key and value to this array so that the result is as below:

array (size=2)
  0 => 
    array (size=5)
      'itemId' => string '4' (length=1)
      'name' => string 'Ice Break' (length=9)
      'size' => string '500ml' (length=5)
      'supplier' => string 'Parmalat' (length=8)
      'newName' => string 'Ice Break (500ml) (Parmalat)' (length=28)
  1 => 
    array (size=5)
      'itemId' => string '6' (length=1)
      'name' => string 'Red Bull' (length=8)
      'size' => string '250ml' (length=5)
      'supplier' => string 'Red Bull' (length=8)
      'newName' => string 'Red bull (250ml) (Red Bull)' (length=26)

I have tried this so far, but no luck:

while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
        $array[] = $row;
        $array['newName'] = $row["name"].' ('.$row["size"].') ('.$row["supplier"].')';
    }
Mike

You're close. Add it to $row and then append it to the array.

while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
    $row['newName'] = $row["name"].' ('.$row["size"].') ('.$row["supplier"].')';
    $array[] = $row;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysql_fetch_array() recieving null value

From Dev

How to Add Custom Timezones to MySQL

From Dev

How to fetch value from mysql to dropdown list?

From Dev

How to fetch a not so random value from an array?

From Dev

How to fetch and store value as coordinate from array

From Dev

how to fetch array value in textbox not working

From Dev

How does mysql_fetch_array() works

From Dev

How Fetch Array String From MySQL And PHP

From Dev

Fetch data from database and shift and add new value in an array

From Dev

Mysql: fetch value from database and php array in single query

From Dev

mysql_fetch_array is including previous value in next one

From Dev

Mysql: fetch value from database and php array in single query

From Dev

How to add a value into the middle of an array?

From Dev

How to add link in to array value ?

From Dev

how to add additional value to array

From Dev

How to fetch each value from Array of strings inside an Array

From Dev

fetch value from an array

From Dev

Adding Array to MySQL Fetch

From Dev

PHP fetch array mysql

From Dev

How To fetch a specific value from all children in firebase database and then add it?

From Dev

How to add a custom array to a polydata in paraview?

From Dev

How to add elements to an array of custom objects in Swift

From Dev

How to add a custom array to a polydata in paraview?

From Dev

How to add elements to an array of custom objects in Swift

From Dev

How to fetch value from JSON document inside MySQL column?

From Dev

How to fetch the last value in a column from mysql database using python?

From Dev

Javascript - How can I fetch a non zero value from an array?

From Dev

hash with array as a key, how to fetch by fist value in the hash

From Dev

How to fetch Array value from hidden field in Servlet?

Related Related

  1. 1

    mysql_fetch_array() recieving null value

  2. 2

    How to Add Custom Timezones to MySQL

  3. 3

    How to fetch value from mysql to dropdown list?

  4. 4

    How to fetch a not so random value from an array?

  5. 5

    How to fetch and store value as coordinate from array

  6. 6

    how to fetch array value in textbox not working

  7. 7

    How does mysql_fetch_array() works

  8. 8

    How Fetch Array String From MySQL And PHP

  9. 9

    Fetch data from database and shift and add new value in an array

  10. 10

    Mysql: fetch value from database and php array in single query

  11. 11

    mysql_fetch_array is including previous value in next one

  12. 12

    Mysql: fetch value from database and php array in single query

  13. 13

    How to add a value into the middle of an array?

  14. 14

    How to add link in to array value ?

  15. 15

    how to add additional value to array

  16. 16

    How to fetch each value from Array of strings inside an Array

  17. 17

    fetch value from an array

  18. 18

    Adding Array to MySQL Fetch

  19. 19

    PHP fetch array mysql

  20. 20

    How To fetch a specific value from all children in firebase database and then add it?

  21. 21

    How to add a custom array to a polydata in paraview?

  22. 22

    How to add elements to an array of custom objects in Swift

  23. 23

    How to add a custom array to a polydata in paraview?

  24. 24

    How to add elements to an array of custom objects in Swift

  25. 25

    How to fetch value from JSON document inside MySQL column?

  26. 26

    How to fetch the last value in a column from mysql database using python?

  27. 27

    Javascript - How can I fetch a non zero value from an array?

  28. 28

    hash with array as a key, how to fetch by fist value in the hash

  29. 29

    How to fetch Array value from hidden field in Servlet?

HotTag

Archive