mysql - Name-value and key-value pairs both being created in PHP file (for json encode) -
mysql - Name-value and key-value pairs both being created in PHP file (for json encode) -
i generating php arrays mysql database, output converted json using json_encode (ultimately utilize twitter typeahead).
based upon i've read php arrays, had expected select
query generate - record - single key-value pair each column.
instead, seem getting both name-value pair , key-value pair.
for instance, table 5 columns, set of values single record in form:
[fieldname1] => value1 [0] => value1 [fieldname2] => value2 [1] => value2 [fieldname3]] => value3 [2] => value3 [fieldname4]=> value4 [3] => value4 [fieldname5] => value5 [4] => value5
when utilize json_encode generates next json string record - twice long needs because getting both name-value pair , key-value pair (so the value repeated line).
"fieldname1":"value1","0":"value1", "fieldname2":"value2","1":"value2", "fieldname3":"value3","2":"value3", "fieldname4":"value4","3":"value4", "fieldname5":"value5","4":"value5",
what prefer 'fieldname1' => 'value1'
or json equivalent.
here illustration of code utilize in php select info database.
<?php $sql = "select * `tr_trades_anzsco` `trade_alias_id` > 898"; $trade = $dbh->query($sql); foreach($trade $trade): $trade_alias_id = $trade['trade_alias_id']; $trade_alias = trim($trade['trade_alias']); print_r($trade); endforeach; ?>
here resultant dataset 3 records.
array ( [trade_alias_id] => 899 [0] => 899 [trade_alias] => boilermaker [1] => boilermaker [anzsco_title] => structural steel , welding trades workers [2] => structural steel , welding trades workers [anzsco_code] => 322300 [3] => 322300 [source] => raj [4] => raj ) array ( [trade_alias_id] => 900 [0] => 900 [trade_alias] => welder [1] => welder [anzsco_title] => structural steel , welding trades workers [2] => structural steel , welding trades workers [anzsco_code] => 322300 [3] => 322300 [source] => tom [4] => tom ) array ( [trade_alias_id] => 901 [0] => 901 [trade_alias] => rigger [1] => rigger [anzsco_title] => construction rigger [2] => construction rigger [anzsco_code] => 821711 [3] => 821711 [source] => jack [4] => jack )
finally, here json string final record (broken construction more visible).
array ( [0] => array ( [trade_alias_id] => 901 [0] => 901 [trade_alias] => rigger [1] => rigger [anzsco_title] => construction rigger [2] => construction rigger [anzsco_code] => 821711 [3] => 821711 [source] => jack [4] => jack ) )
the duplicate values matter considerably performance reasons - because info extracted utilize on mobile devices (with twitter typeahead , perchance angulularjs). our datasets big ... twice json double time fetching data.
key question: how can remove duplicates either @ php stage or json_encode stage generate name-value pair not both name-value , key-value pair?
thank you!
presuming using pdo:
$trade = $dbh->query($sql, pdo::fetch_assoc);
php mysql arrays json key-value
Comments
Post a Comment