I have this current structure:
{"resultset":[
{"IM":[2],"SR":[2],"TA":[4],"PMT":[2],"IMT":[2]},
{"IM":[1],"SR":[0],"TA":[4],"PMT":[1],"IMT":[0]},
{"IM":[2],"SR":[5],"TA":[17],"PMT":[2],"IMT":[5]}
]
}
I need to generate a foreach loop (I think) searching an each key and transforming the structure to look like this instead:
{"resultset":[ {"IM":[2,1,2],"SR":[2,0,5],"TA":[4,4,17],"PMT":[2,1,2],"IMT":[2,0,5]} ]}
I know I have to use a foreach loop and search on the key and then push the data back into a new array with this structure. but im having difficulty getting my head around it. any help would be appreciated.
I might need to change my strategy here:
this is my current code that is generated the first array:
for($i=0; $i<sizeof($teams);$i++)
{
$openResultSet['resultset'][] = $DAL->runGenericSQL(TotalTicketsInQueue($teams[$i]),"open",$i);
}
this calls the following (in a nutshell)
while ($row = odbc_fetch_array($resultSet))
{
$OpenTicketResultArray["IM"][] = intval($row["IM"]);
$OpenTicketResultArray["SR"][] = intval($row["SR"]);
$OpenTicketResultArray["TA"][] = intval($row["TA"]);
$OpenTicketResultArray["PMT"][] = intval($row["IM"]);
$OpenTicketResultArray["IMT"][] = intval($row["SR"]);
}
return $OpenTicketResultArray;
I'm thinking at each iteration when the first array comes back store the results in a new key'd array and then continue to push the new results. but I'm new to this :(
Related
I trying to test display a nested Json structure but the result is in wrong format.
php
while($row=$statement->fetch()){
$value=str_replace('"','',$row['item_option_value']);
$option[$row['item_option_name']]=explode(',',$value);
$all[$row['oid']]=$option;
}
echo json_encode($all);
mysql databas structure
Here is the result when i run the script.
I want the json structure to be the right side of the screenshot. Anyone knows what's wrong?
You would need to empty the $option arrays then like this:
$option = []; //or $option = array(); in PHP < 5.4
This is needed in order not to keep storing the data from the previous iteration.
So:
while($row=$statement->fetch()){
$value=str_replace('"','',$row['item_option_value']);
$option = [];
$option[$row['item_option_name']]=explode(',',$value);
$all[$row['oid']]=$option;
}
echo json_encode($all);
The problem is because of this line here,
$option[$row['item_option_name']]=explode(',',$value);
In each iteration of while() loop, you're appending the previously calculated $options array to $all. Instead, create an temporary array to hold intermediate result and append that to $all in each iteration, like this:
while($row=$statement->fetch()){
$opArray = array();
$value=str_replace('"','',$row['item_option_value']);
$opArray[$row['item_option_name']]=explode(',',$value);
$all[$row['oid']]=$opArray;
}
echo json_encode($all);
I'm executing a cursor. I have cutted off the code how the procedure is called and executed. This part is efficient.
At last I have a a not big cursor. I'm calling the procedure, which returns this cursor many times on the page and I need to create a multidimensional array from it. This array should look like like the following:
$ret = oci_execute($outrefc) ;
while ($row = #oci_fetch_array($outrefc))
{
foreach (array_keys($row) as $key)
{
$res[$i][$key] = $row[$key];
}
$i++;
}
Is there any way to make the upper snippet faster?
The multidimensional array should stay as it is. I only wonder if I could create it in any more efficient way.
Thank you!
Alright, So I'm redoing my question so people can understand what I'm trying to do.
Search.php
<?php
$getItems = file_get_contents('Items.json');
if(isset($_GET['searchVal'])){
$getItems2 = json_decode($getItems, true);
$data2 = array("items" => array(array()));
foreach($getItems2['items'] as $data){
if(strpos($data['name'], $_GET['searchVal'])){
$data2 = array("items" => array(array($data)));
}
}
echo json_encode($data2,JSON_UNESCAPED_SLASHES);
} else{
echo $getItems;
}
Problem: Doesn't get all items which have that name, gets only one.
Search is done, now I have to fix somehow to get all items which match the name. How could I do that?
You have the following inside a loop:
$data2 = array(...)
...and then you reference $data2 outside the loop.
Of course, it will only contain the last entry, because that line is overwriting $data2 with new data each time the loop iterates.
If you want to keep all the records from the loop, use
$data2[] = array(...)
instead.
[EDIT]
Further guessing as to what you actually want your JSON to look like, I guess you want all the records to be in the items array?
So in that case, let's rewrite your $data2 line, as follows:
$data2['items'][] = array($data);
This will add all the data arrays to your items array in $data2. I will note that your array structure is really convoluted -- too many nested arrays, which makes it difficult to be sure I'm giving you the answer you want even now, but hopefully if it isn't exactly right, it will show you the direction you need to go.
You'll also want to have an additional line at the top of your code to initialise $data2, like this:
$data2 = array('items'=>array());
This should be somewhere at the top of the code, before $data2 is used, and outside of the loop.
I recently started using JSTREE to display my json data (I am showing it as an array in my sample code here). But, some of the nodes are repeating. I was thinking of adding an If loop to check if the node is already a child node but not sure how to do that. I also think my code is also not very well performing because of the many for loops. I am a PHP newbie so not sure if there might be a faster way to recurse through an array. Here is my code.
$relation = array(
"parent"=>array("item"=>array("cs","ls")),
"cs"=>array("item"=>array("cs_1")),
"ls"=>array("item"=>array("ls_1")),
"cs_1"=>array("item"=>array("cs_1_1"))
);
$tree = array();
$i=0;
foreach($relation as $key_parent=>$value_parent) {
#$children = array_keys($value_parent);
$tree[$i++] = array('id'=>$key_parent,'text'=>$key_parent,'state'=>array('opened'=>true),'parent'=>'#');
foreach($value_parent['item'] as $key_child_1=>$value_child_1) {
$tree[$i++]= array('id'=>$value_child_1,'text'=>$value_child_1,'state'=>array('opened'=>true),'parent'=>$key_parent);
}
}
Your input structure is far from optimal, but it can still work. If I understand correctly, this is what you need:
$relation = array(
"parent"=>array("item"=>array("cs","ls")),
"cs"=>array("item"=>array("cs_1")),
"ls"=>array("item"=>array("ls_1")),
"cs_1"=>array("item"=>array("cs_1_1"))
);
$tree = array();
foreach($relation as $key_parent=>$value_parent) {
$tree[$key_parent] = array('id'=>$key_parent,'text'=>$key_parent,'state'=>array('opened'=>true),'parent'=>'#');
foreach($value_parent['item'] as $key_child_1=>$value_child_1) {
$tree[$value_child_1]= array('id'=>$value_child_1,'text'=>$value_child_1,'state'=>array('opened'=>true),'parent'=>$key_parent);
}
}
$tree = array_values($tree);
The only fix is using the actual node ID as a key in the $tree array, and then calling array_values to convert it back.
I've done a fair bit of googling and couldn't find anything that works, I'm just getting nothing back, this is probably something simple but there's a lot of variations that don't seem to match what I'm doing.
To give you an overall idea what I'm at, I'm accessing an API and getting back info as an object. There are comments and attachments, these are in separate arrays.
What i want to do is display the comments and attachments all together in the order of the date and time not separately.
I figured the best way is to create a loop through the comments array, then create a loop through the attachment array, then join both and sort by the date (epoch) and then loop through the whole merged loop echoing what i want. That should provide some context, right now i just want to create the multidimensional array for comments and i can figure out the rest.
$comments_holder = array();
//total number of comments in the array
$comment_total = $issue_json->fields->comment->total -1;
$i=1;
while ($i <= $comment_total)
{
//this is the date,time and timezone info for each comment
$raw_date = $issue_json->fields->comment->comments[$i]->updated;
$comments_holder[$i] = array();
//convert_sql_time just converts from 2012-11-04T16:33:00.936+600 into epoch time so i can sort the results later based on date
$comments_holder[$i]['comments_date'] = convert_sql_time($raw_date);
$comments_holder[$i]['comments_displayName'] = $issue_json->fields->comment->comments[$i]->author->displayName;
$comments_holder[$i]['comments_body'] = $issue_json->fields->comment->comments[$i]->body;
}
if everything is okay with data, this code will be enough for building such array:
$comments = $issue_json->fields->comment->comments;
$result = array();
foreach ($comments as $comment) {
$result[] = array(
'comments_date' => convert_sql_time($comment->updated),
'comments_displayName' => $comment->author->displayName,
'comments_body' => $comment->body,
);
}
print_r($result);
if comment->comments is an array, there is no need to keep it's count separately;
foreach is enough for iterating through the array and there is no need to keep separate variable for calculating array index;
[] notation will automatically increase array index and assigning array directly will do the trick(i.e. will result to multi dim array)