I need to give output with 'floorno' and 'units' as key.But after performing array_splice operation 'floorno' and 'units' key are replaced by integer values.
How to replace integer-valued key with text floorno and unit.
Below is my code
$floor = "Ground";
$query = "SELECT id,property_no,status FROM property_details WHERE wing ='$A' AND floor ='$floor'";
$execute = $db->query($query);
$ground = $execute->fetchAll(PDO::FETCH_ASSOC);
$block_A = array("floorno"=>$floor,"units"=>$ground);
$floor = "First";
$query = "SELECT id,property_no,status FROM property_details WHERE wing ='$A' AND floor ='$floor'";
$execute = $db->query($query);
$ground = $execute->fetchAll(PDO::FETCH_ASSOC);
$first = array("floorno"=>$floor,"units"=>$ground);
array_splice($block_A,2,0,$first);
Below is my array o/p:
{
"0": "Second",
"1": [
{
"id": "396",
"property_no": "SSC.A.02.01",
"status": "0"
},
{
"id": "397",
"property_no": "SSC.A.02.02",
"status": "0"
}
],
"2": "First",
"3": [
{
"id": "388",
"property_no": "SSC.A.01.01",
"status": "0"
},
{
"id": "389",
"property_no": "SSC.A.01.02",
"status": "0"
}
],
"floorno": "Ground",
"units": [
{
"id": "379",
"property_no": "SSC.A.00.01",
"status": "0"
},
{
"id": "381",
"property_no": "SSC.A.00.02",
"status": "0"
}
]
}
Helper function for splice with assoc
function array_splice_assoc(&$input, $offset, $length, $replacement)
{
$replacement = (array)$replacement;
$key_indices = array_flip(array_keys($input));
if (isset($input[$offset]) && is_string($offset)) {
$offset = $key_indices[$offset];
}
if (isset($input[$length]) && is_string($length)) {
$length = $key_indices[$length] - $offset;
}
$input = array_slice($input, 0, $offset, TRUE)
+ $replacement
+ array_slice($input, $offset + $length, NULL, TRUE);
}
Related
I have a code like this:
$res = array ();
$i2 = 0;
for ($i = 0; $i <10; $i++) {
if ($table ['users'] [$i] ['value'] == null) {
break;
}
$res ['users'] [$i2] ['value'] = $table ['users'] [$i] ['value'];
$res ['users'] [$i2] ['user_id'] = $table ['users'] [$i] ['user_id'];
$res ['users'] [$i2] ['name'] = $table ['users'] [$i] ['name'];
$i2 ++;
}
$mesto;
for ($i3 = 0; $i3 <count ($table ['users']); $i3 ++) {
if ($user_id == $table ['users'] [$ i3] ['user_id']) {
$mesto = $ i3 + 1;
break;
}
}
$res ['currentTop'] = $mesto;
$json = json_encode ($res, JSON_UNESCAPED_UNICODE);
echo $json;
$table * - This is decoded json, table has an array users, in which objects, each with three fields * value, user_id, name *
JSON looks like this:
{
"users": [{
"value": "994954359439",
"user_id": 2,
"name": "Anton Piskorskyi"
}, {
"value": 99999,
"user_id": 99999,
"name": "Anton Piskorskyi"
}, {
"value": 99998,
"user_id": 99998,
"name": "Anton Piskorskyi"
}, {
"value": 99997,
"user_id": 99997,
"name": "Anton Piskorskyi"
}, {
"value": 99996,
"user_id": 99996,
"name": "Anton Piskorskyi"
}, {
"value": 99995,
"user_id": 99995,
"name": "Anton Piskorskyi"
}, {
"value": 99994,
"user_id": 99994,
"name": "Anton Piskorskyi"
}, {
"value": 99993,
"user_id": 99993,
"name": "Anton Piskorskyi"
}, {
"value": 99992,
"user_id": 99992,
"name": "Anton Piskorskyi"
}, {
"value": 99991,
"user_id": 99991,
"name": "Anton Piskorskyi"
}]
}
Themselves such objects can be 100,000 or more. The above code takes the first 10 objects from the array and by * user_id * looks for the index of the object in which this * user_id * is located. In short, the code is used to display the top 10 players and the current position of the user. Those. this piece of code will be called quite a few times in a small amount of time. I have intel I3-71 on my computer ... and when I start spamming the opening of the page many times, the processor is loaded by ~ 50% (this is for 100,000 objects), if you put this code on the VDS, then with a large online VDS can not to export, hence the question arises: how can all this be optimized? namely this function:
$mesto;
for ($i3 = 0; $i3 <count ($table ['users']); $i3 ++) {
if ($user_id == $table ['users'] [$i3] ['user_id']) {
$mesto = $i3 + 1;
break;
}
}
P.S.*
Objects I call this in Json:
{
"value": "994954359439",
"user_id": 2,
"name": "Anton Piskorskyi"
}
Hi i am trying to merge output of MySQL result in JSON format but i confused how i can do this so guys i need your helps please tell me how i can do this work thank you.
SQL:
$result = $db->sql_query("SELECT a.*,i.member_id,i.members_seo_name
FROM ".TBL_IPB_USER." i
LEFT JOIN ".TBL_IPB_LA." a
ON a.member_id=i.member_id
WHERE i.".$column." = '".$val."' AND a.game = '".$game."'");
while( $dbarray = $db->sql_fetchrow($result) ){
$arr[] = $dbarray;
}
return ($arr);
The normal result and output with JSON format for my query is:
{
"status": 200,
"result": [
{
"member_id": "1",
"member_name": "maxdom",
"ip_address": "177.68.246.162",
"session_onlineplay": "1",
"sid": "IR63374a32d1424b9288c5f2a5ce161d",
"xuid": "0110000100000001",
"serialnumber": "9923806a06b7f700a6ef607099cb71c6",
"game": "PlusMW3",
"members_seo_name": "maxdom"
},
{
"member_id": "1",
"member_name": "maxdom",
"ip_address": "81.254.186.210",
"session_onlineplay": "1",
"sid": "IR3cd62da2f143e7b5c8f652d32ed314",
"xuid": "0110000100000001",
"serialnumber": "978e2b2668ec26e77c40c760f89c7b31",
"game": "PlusMW3",
"members_seo_name": "maxdom"
}
],
"handle": "checkUSER"
}
But i want to merge output and result like this one:
{
"status": 200,
"result": [
{
"member_id": "1",
"member_name": "maxdom",
"ip_address": [
"177.68.246.162",
"81.254.186.210"
],
"session_onlineplay": "1",
"sid": [
"IR63374a32d1424b9288c5f2a5ce161d",
"IR3cd62da2f143e7b5c8f652d32ed314"
],
"xuid": "0110000100000001",
"serialnumber": [
"9923806a06b7f700a6ef607099cb71c6",
"978e2b2668ec26e77c40c760f89c7b31"
],
"game": "PlusMW3",
"members_seo_name": "maxdom"
}
],
"handle": "checkUSER"
}
you better use php for your parser, prevent high load for database, this is sample code
$result = $db->sql_query("SELECT a.*,i.member_id,i.members_seo_name
FROM ".TBL_IPB_USER." i
LEFT JOIN ".TBL_IPB_LA." a
ON a.member_id=i.member_id
WHERE i.".$column." = '".$val."' AND a.game = '".$game."'");
$arr = array();
while( $dbarray = $db->sql_fetchrow($result) ){
$item = $dbarray;
$item['ip_address'] = array($item['ip_address']);
$item['sid'] = array($item['sid']);
$item['serialnumber'] = array($item['serialnumber']);
$index = $dbarray['member_id'];
if(isset($arr[$index]))
{
$arr[$index]['ip_address'] = array_merge($arr[$index]['ip_address'], $item['ip_address'];
$arr[$index]['sid'] = array_merge($arr[$index]['sid'], $item['sid'];
$arr[$index]['serialnumber'] = array_merge($arr[$index]['serialnumber'], $item['serialnumber']);
} else {
$arr[$index] = $item;
}
}
return array_values($arr);
I have the following code:
$retarray = array();
$hoursarray = array();
$res = $this->mysqli->query("SELECT id, title, groupby FROM scheduler_activity WHERE gewerbe = '$this->gewerbe'");
while($row = $res->fetch_object()) {
$foo = $this->mysqli->query("SELECT dow, start, end FROM scheduler_businesshours WHERE activity = '$row->id'");
while($hours = $foo->fetch_object()){
$hoursarray[] = $hours;
}
$retarray[] = $row;
$retarray["businessHours"] = $hoursarray;
}
return $retarray;
And the following JSON output:
{
"0": {
"id": "1",
"title": "Spinning",
"groupby": "Trainingsgruppe"
},
"businessHours": [{
"dow": "[1,2,3]",
"start": "17:00:00",
"end": "18:00:00"
}, {
"dow": "[4,5]",
"start": "17:30:00",
"end": "18:30:00"
}],
"1": {
"id": "2",
"title": "Massage",
"groupby": "Trainingsgruppe"
},
"2": {
"id": "3",
"title": "Yoga",
"groupby": "Trainingsgruppe"
},
"3": {
"id": "4",
"title": "Dance Academy",
"groupby": "Trainingsgruppe"
}
}
So far it looks correct but I need to get rid of the leading numbers like 0, 1, 2, 3
That it will look like this plus the businessHours:
[{
"id": "1",
"title": "Spinning",
"groupby": "Trainingsgruppe"
}, {
"id": "2",
"title": "Massage",
"groupby": "Trainingsgruppe"
}, {
"id": "3",
"title": "Yoga",
"groupby": "Trainingsgruppe"
}, {
"id": "4",
"title": "Dance Academy",
"groupby": "Trainingsgruppe"
}]
What did I do wrong there?
Thank you!
As I understood about your code, we get business hour for each activity, so it's just a small change at line 10:
$retarray = array();
$hoursarray = array();
$res = $this->mysqli->query("SELECT id, title, groupby FROM scheduler_activity WHERE gewerbe = '$this->gewerbe'");
while($row = $res->fetch_object()) {
$foo = $this->mysqli->query("SELECT dow, start, end FROM scheduler_businesshours WHERE activity = '$row->id'");
while($hours = $foo->fetch_object()){
$hoursarray[] = $hours;
}
$row["businessHours"] = $hoursarray; // Change to this, swap line 10 & 11
$retarray[] = $row;
}
return $retarray;
Hope this helps.
You add two keys. One key is numeric the second is businessHours.
$retarray[] = $row;
$retarray["businessHours"] = $hoursarray;
For instance you can add it businessHours to $row
$row["businessHours"] = $hoursarray;
$retarray[] = $row;
Have you try array_values($array)?
$retarray = array();
$hoursarray = array();
$res = $this->mysqli->query("SELECT id, title, groupby FROM scheduler_activity WHERE gewerbe = '$this->gewerbe'");
while($row = $res->fetch_object()) {
$foo = $this->mysqli->query("SELECT dow, start, end FROM scheduler_businesshours WHERE activity = '$row->id'");
while($hours = $foo->fetch_object()){
$hoursarray[] = $hours;
}
$retarray[] = $row;
$retarray["businessHours"] = $hoursarray;
}
return array_values($retarray);
I'd encoded my result into json and I want to merge them. I wish to achieve this
[
{
"post_id": 1,
"content": "test image feature",
"date": "0000-00-00",
"category_id": 1,
"lp_title": "",
"lp_image": "",
"lp_canonicalUrl": "",
"lp_url": "",
"lp_desc": "",
"lp_iframe": "",
"lp_iframe_id": ""
"img_src" [{img_src:1.jpg},{img_src:2.jpg}]
}
]
http://pastebin.com/7jKp9BUn
the result of each statement
[
{
"img_src": "1.jpg"
},
{
"img_src": "2.jpg"
}
]
the next one
[
{
"post_id": 1,
"content": "test image feature",
"date": "0000-00-00",
"category_id": 1,
"lp_title": "",
"lp_image": "",
"lp_canonicalUrl": "",
"lp_url": "",
"lp_desc": "",
"lp_iframe": "",
"lp_iframe_id": ""
}
]
You can decode the JSON into arrays, append one to the other, then re-encode:
$s1 = '[
{
"img_src": "1.jpg"
},
{
"img_src": "2.jpg"
}
]';
$s2 = '[
{
"post_id": 1,
"content": "test image feature",
"date": "0000-00-00",
"category_id": 1,
"lp_title": "",
"lp_image": "",
"lp_canonicalUrl": "",
"lp_url": "",
"lp_desc": "",
"lp_iframe": "",
"lp_iframe_id": ""
}
]';
//decode each
$arr1 = json_decode($s1, true);
$arr2 = json_decode($s2, true);
$arr2[0]['img_src'] = $arr1;
//re-encode as a JSON string and show output
$sf = json_encode($arr2);
echo $sf;
This should give the result you described, with "img_src"... as a sub-array of the larger set.
Looking at your php code you could do it this way as well in one shot.
$stmt = $db->prepare("
SELECT post_id,content,date,category_id,lp_title,lp_image,lp_canonicalUrl,lp_url,lp_desc,lp_iframe,lp_iframe_id
FROM post_items inner JOIN user ON post_items.user_id = user.user_id
WHERE post_items.user_id = ? order by post_items.post_id desc LIMIT ?,?");
$stmt->bind_param('iii', $userid, $itemStart, $itemEnd);
$stmt2 = $db->prepare("SELECT photo_upload.img_src FROM photo_upload inner JOIN post_items ON post_items.photo_set_id = photo_upload.photo_set_id
WHERE post_items.photo_set_id = photo_upload.photo_set_id ");
//store the images in a separate array
$imgArray = array();
if ($stmt2->execute()) {
$photo_items = $stmt2->get_result();
while ($obj2 = $photo_items->fetch_object()) {
$imgArray[] = $obj2;
}
}
if($stmt->execute()){
$post_items = $stmt->get_result();
if(mysqli_num_rows($post_items) > 0){
while ($obj = $post_items->fetch_object()) {
$jsonData[] = $obj;
// check if there are any elements in the array
if(count($imgArray) > 0){
// set the img_src index of the jsonData array with the elements of the other array
$jsonData['img_src'] = $imgArray;
}
}
$i = json_encode($jsonData);
echo $i;
}
}
So i have the following array structure:
{
"_": {
"APP_ID": "server_tracked"
},
"success": true,
"requestTime": "2013-09-14T15:05:28-07:00",
"shard": "North_America:OTg0ZGYzNjA0OGYxNjAyNWUzZjVlNTQwZDk4YTdjNTYzMGE3NTA4Ng",
"player": {
"accountId": xxx,
"summonerId": xx,
"name": "xx",
"icon": xx,
"internalName": "xx",
"level": xx
},
"data": {
"lifetimeStatistics": {
"array": [
{
"count": 1,
"statType": "TOTAL_SESSIONS_PLAYED",
"dataVersion": 0,
"value": 1,
"championId": 111,
"futureData": null
},
{
"count": 0,
"statType": "TOTAL_SESSIONS_LOST",
"dataVersion": 0,
"value": 0,
"championId": 111,
"futureData": null
},
[...]
And i want to search for a "sub array" where the value "championId" = x and statType = y.
I would then have the [x] if that array, and i should then be able to return any value in that array.
here is some of the PHP code i currently have:
$result = json_decode($response -> raw_body);
$array = $result->data->lifetimeStatistics->array;
$search1 = x;
$search2 = y;
$cluster=false;
foreach ($array as $n=>$c) {
if (in_array($search, $c)) {
$cluster=$n; break;
}
}
As additional information then i am using $response = Unirest::get to get the array.
EDIT with full code:
$result = json_decode($response -> raw_body);
$haystack = $result->data->lifetimeStatistics->array;
$searchx = x;
$searchy = y;
$arrayNumber = null;
foreach ($haystack as $n=>$c) {
if ($c->championId === $searchx && $c->statType === $searchy) {
$arrayNumber = $n;
}
}
// We now get the value from that array
$array = $result->data->lifetimeStatistics->array[$arrayNumber]->value;
return $array;
You found the answer, however needs some tweaks.
$result = json_decode($response -> raw_body);
$haystack = $result->data->lifetimeStatistics->array;
$searchx = "x"; // fill as required
$searchy = "y"; // fill as required
$cluster=null;
foreach ($haystack as $n=>$c) {
if ($c["championId"] === $searchx && $c["statType"] === $searchy) {
$cluster=$n; break;
}
}
if (is_null($cluster)) die("Not found?!"); //do whatever you want if not found