Json operations in PHP -
i have mysql database reading php:
$con = mysqli_connect($host, $user, $pwd, $db); if(mysqli_connect_errno($con)) { die("failed connect mysql: " . mysqli_connect_error()); } $sql = "select grad lista"; $result = mysqli_query($con, $sql); $rows = array(); while($row = mysqli_fetch_array($result, mysqli_assoc)) { $rows[] = $row; } mysqli_close($con); $arr = array_flip(array_map('serialize', $rows)); $lista = array_map('unserialize', array_flip($arr)); echo json_encode((object) array('lista' => array_values($lista)));
and localhost getting this:
{"lista":[{"grad":"beograd"},{"grad":"novi_sad"},{"grad":"kragujevac"}]}
is there way this:
{"lista":[{"grad":"odaberite grad"},{"grad":"beograd"},{"grad":"novi_sad"},{"grad":"kragujevac"}]}
i add {"grad":"odaberite grad"}
in front of everything. thank in advance.
edit: please not {"grad":"odaberite grad"} not part of mysql table
have tried adding want after declaring $arr
follows:
$rows = array(); $rows[] = array('grad' => 'odaberite grad');
update:
alternatively other answerers have pointed out can use array_unshift
follows:
array_unshift($rows, array('grad' => 'odaberite grad'));
Comments
Post a Comment