php - JSON return 2 array with 1 element -
i have function:
public function findfavoritemerchant($userid) { // grab merchant_id favoritemerchant user_id = $userid $q = $this->_db->prepare("select * favoritemerchant user_id = ?"); $q->bindparam(1, $userid, pdo::param_int); if ($q->execute()){ $result = $q->fetchall(pdo::fetch_assoc); $i = -1; foreach ($result $row) { $merchant = $this->_db->prepare("select * merchant merchant_id = ?"); $merchant->bindparam(1, $row["merchant_id"], pdo::param_int); if ($merchant->execute()){ $i++; $merchantarray[$i] = $merchant->fetchall(pdo::fetch_assoc); } } return $merchantarray; } }
that return :
[ [ { "merchant_id": "17", "business_name": "nails spa", "merchant_first_name": "jonh ", "merchant_last_name": "lampard", "merchant_email": "email@me.com", "merchant_address": "123 lampard street", "merchant_country": "united states", "merchant_city": "san francisco ", "merchant_state": "dubai", "merchant_postalcode": "92", "merchant_telephone": "043251653", "merchant_industry": "beauty", "merchant_type": "spa", "merchant_service": "other", "merchant_lat": "37.804512", "merchant_long": "-122.432335" } ], [ { "merchant_id": "19", "business_name": "massage spa", "merchant_first_name": "carl", "merchant_last_name": "smith", "merchant_email": "email@me.com", "merchant_address": "", "merchant_country": "united states", "merchant_city": "san francisco ", "merchant_state": "dubai", "merchant_postalcode": "92", "merchant_telephone": "043278439", "merchant_industry": "beauty", "merchant_type": "spa", "merchant_service": "other", "merchant_lat": "25.037189", "merchant_long": "55.251812" } ] ]
but this:
[ { "merchant_id": "17", "business_name": "nails spa", "merchant_first_name": "jonh ", "merchant_last_name": "lampard", "merchant_email": "email@me.com", // , on....
****update****
$merchant = new merchant(); echo json_encode($merchant->findfavoritemerchant($userid),json_pretty_print);
print r returns:
array ( [0] => array ( [merchant_id] => 17 [business_name] => massage spa [merchant_first_name] => jonh [merchant_last_name] => lampard [merchant_email] => email@me.com [merchant_address] => 123 lampard street [merchant_country] => united states [merchant_city] => san francisco [merchant_state] => dubai [merchant_postalcode] => 92 [merchant_telephone] => 043251653 [merchant_industry] => beauty [merchant_type] => spa [merchant_service] => other [merchant_lat] => 37.804512 [merchant_long] => -122.432335 [merchant_username] => aviation [password] => cfc10a708f01fe27750e3be246ca1daa ) ) array ( [0] => array ( [merchant_id] => 19 [business_name] => angsana spa [merchant_first_name] => carl [merchant_last_name] => smith [merchant_email] => email@me.com [merchant_address] => [merchant_country] => united states [merchant_city] => san francisco [merchant_state] => dubai [merchant_postalcode] => 92 [merchant_telephone] => 043278439 [merchant_industry] => beauty [merchant_type] => spa [merchant_service] => other [merchant_lat] => 25.037189 [merchant_long] => 55.251812 ) )
i tried several approaches don't
you need take individual elements return of fetchall
since returns array.
use simple foreach
populate final array.
else stays same, return $merchantarray
, json_encode
print out.
$results = $merchant->fetchall(pdo::fetch_assoc); foreach($results $result){ $merchantarray[$i] = $result; }
Comments
Post a Comment