Create Json from a mysql Result Set Resembling a Tree [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
The problem is to get the following result set:
'org1', 'unit1', 'proj1'
'org1', 'unit1', 'proj2'
'org1', 'unit2', 'proj1'
'org1', 'unit2', 'proj2'
'org2', 'unit1', 'proj1'
'org2', 'unit1', 'proj2'
to the following in php:
[{"units": [{"name": "unit1", "projects": ["project1", "project2"]}, {"name": "unit2", "projects": ["project1", "project2"]}], "name": "org1"}, {"units": [{"name": "unit1", "projects": ["project1", "project2"]}], "name": "org2"}]
Any suggestions?

You can use something like
$final_array = array();
foreach($array as $row){
$final_array[$row[0]][$row[1]][] = $row[2];
}
Now this will "convert the SQL result array into a multidimentional array/tree" but not like what you want yet.
So we shall have to process the array again..
$final_array_2 = array(); // Lets go deep
foreach ($final_array as $name => $units) {
$quarterfinal_array = array(); // Not deep enough
$semi_final_array = array();
foreach ($units as $proj_name => $projects) {
$nano_final_array = array(); // Lets dig deeper ;)
$nano_final_array['name'] = $proj_name;
$nano_final_array['projects'] = $projects;
$semi_final_array[] = $nano_final_array;
}
$quarterfinal_array['units'] = $semi_final_array;
$quarterfinal_array['name'] = $name;
$final_array_2[] = $quarterfinal_array;
}
echo json_encode($final_array_2);
PS: Sorry my choice of variable names are not the most ideal, but they get the work done ;) This is a P.O.C, You can always improve on it.
DEMO

Related

Laravel Helper to merge Period arrays [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Laravel Version:7.0
I would like to know how to create this helper.
Input is date range array. For example:
$input1 = [2020-07-19, 2020-07-25];
$input2 = [2020-07-26, 2020-08-01];
$input3 = [2020-08-01, 2020-08-07];
$input4 = [2020-10-01, 2020-10-07];
$input5 = [2020-10-19, 2020-10-25];
I would like to make one helper function.
function mergeDate($array)
{
...
}
So when I use this helper, I would like to get as following result.
$array = [$input1, $input2, $input3, $input4, $input5];
$mergedResult = mergeDate($array);
$mergedResult[0] = [2020-07-19, 2020-08-07];
$mergedResult[1] = [2020-10-01, 2020-10-07];
$mergedResult[2] = [2020-10-19, 2020-10-25];
Can anyone help me how to make mergeDate function?
Input period elements aren't overlapped.
Thank you!
This would be my first guess at how to solve it.
public function merge($array){
$results = [];
foreach ($array as $element){
if (sizeof($results) == 0){
array_push($results,$element);
}else{
$found = null;
foreach ($results as $key => $r){
if (Carbon::parse($element[0])->equalTo(Carbon::parse($r[1])))
{
$found = $key;
break;
}
}
if (!is_null($found)){
$results[$found][1] = $element[1];
}else{
array_push($results, $element);
}
}
}
return $results;
}
It is a simple take on the problem. If our resulting array is empty we add the first element otherwise we iterate over the results to find a matching pair of the elements end date and the start date of the item in the results array. If we find a matching start end pair we replace the results end value with the elements end value. Otherwise we have no overlap and we can add it as a new item to the results array.
An interesting library to use would be the Spatie/Period library.
https://github.com/spatie/period
#edit
since the array is not sorted as mentioned in a comment above, you would have to sort it prior.

Looping through a php array => weird [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Need a sanity check on this one, I think I just confused myself. I am trying to loop throught the below array and get the fields back for multiple people that are register.
{
[12]=>
array(79) {
["Event Number"]=> int(466226)
["Event Info"]=> string(134) “Event ABC”
["Event Acct Code"]=> NULL
["email"] => string(12)"email#email.com
}
}
How can i best accomplish this?
This is what I tried
$associativeEventInfo=[];
foreach ($res as $eventInfo)
{
$associativeEventInfo[]=$eventInfo;
}
var dumping $associativeEventInfo returns the results
Id like to get each field out according to the array, for example
["EvenNumber"] =466226 so that I can then pass that over to database and do stuff.
So the goal is to remap the data to presumably database fields. First off, accessing the fields is quite easy:
// Assuming $returnedData is an array of assoc array containing the data;
foreach ($returnedData as $eventData)
{
$eventNumber = $eventData['Event Number'];
$eventInfo = $eventData['Event Info'];
// And so on
}
Remapping it is as simple as creating a new array with the correct format (one can also use array_map but I leave that as a readers exercise):
$remappedData = [];
// Assuming $returnedData is an array of assoc array containing the data;
foreach ($returnedData as $eventData)
{
$remappedData[] = [
'eventNumber' => $eventData['Event Number'],
'eventInfo' => $eventData['Event Info'],
// And so on
];
$eventNumber = $eventData['Event Number'];
$eventInfo = $eventData['Event Info'];
}

Php combine an array of values into an array with double combinations string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How i can combine ad array of values into an array with double combination string without duplications ?
For example, if i have something like:
array('one','two','tre','four','five');
I want to obtain an array of combinations like this ('one / two') just one time and not also ('two / one').
In this way i wanto to get something similar to:
array('one/two', 'one/tre', 'one/four', 'one/five', 'two/tree','two/four' .......
Suggestions ?
You can do it with this code. It won't show two/one, three/two, etc (that's how I understood it):
<?php
$array = array('one','two','tre','four','five');
$newArray = [];
foreach ($array as $el) {
foreach ($array as $el2) {
if ($el === $el2) continue;
$newArray[] = $el."/".$el2;
}
array_shift($array); // remove the element we just went through
}
print_r($newArray);
Demo

I can not create PHP array from JSON [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a complicated JSON and I need create array from this JSON.
I already parsed JSON and create a variablies like this:
$name = $json[response][docs][$i][name][0];
$osm_id = $json[response][docs][$i][osm_id][0];
$place = $json[response][docs][$i][place][0];
$population= $json[response][docs][$i][population][0];
now I need a array, with this variablies, where the $i is changing, like this:
$array = [array_1(name,osm_id,place,population),array_2(name_2,osm_id_2)]
Can you help me with the cycle to fill this array?
If my understanding is correct,
$expected_arr = array();
foreach($json[response][docs] as $inc => $values){
$data = array();
foreach($values as $key => $val){
$data[$key] = $val[0];
}
$expected_arr[$inc] = $data;
}
So you would get something like
array(0 => array( 'name'=>'xxx', 'osm_id'=>'yy',..), 1=> array('name'=>'',.. ,),...)

Get data from mysql as array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Sorry for my english..
in mysql has row name iurl.. There is data: 1365269423.jpg,1365270586.jpg,1365270666.jpg,1365270683.jpg
i get its as:
<?php $s=mysql_query("select iurl from points where id='".$_GET['id']."' ");
if($s){
$array = array();
while($t=mysql_fetch_array($s)) {
$array[] = $t['iurl'];
}
print_r($array);
?>
it gives me result: Array ( [0] => 1365269423.jpg,1365270586.jpg,1365270666.jpg,1365270683.jpg )
And i`m need get it and print like a link
how can i do it?
Thanks..
You can use explode(); to split the string into an array and then loop over to print each item:
$images = explode(",", $t["iurl"]);
foreach ($images as $image) {
echo "{$image}";
}
I think you are asking to have result or in your case a .jpg file be the href of your link? If so do this:
......
The result you get from your database as you already figured out is an Array
The only thing you need to do is loop trough the array.
ps: consider using mysqli more info at php.net http://www.php.net/manual/en/book.mysqli.php
<?php
$s=mysql_query("select iurl from points where id='".$_GET['id']."' ");
while ($row = mysql_fetch_object($s)) {
echo '<img="http://yourhost.com/images/'.$row->image.' alt=""/>';
}
?>

Categories