I have a MYSQL query which returns the following PHP array $result:
[0] => Array (
[resourceKey] => cloth
[date] => 2020-09-06
[quantity] => 8
)
[1] => Array (
[resourceKey] => ebony
[date] => 2020-09-06
[quantity] => 4
)
[2] => Array (
[resourceKey] => gems
[date] => 2020-09-06
[quantity] => 0
)
[3] => Array (
[resourceKey] => lead
[date] => 2020-09-06
[quantity] => 0
)
[4] => Array (
[resourceKey] => limestone
[date] => 2020-09-06
[quantity] => 8
)
[5] => Array (
[resourceKey] => cloth
[date] => 2020-09-05
[quantity] => 6
)
[6] => Array (
[resourceKey] => ebony
[date] => 2020-09-05
[quantity] => 3
)
[7] => Array (
[resourceKey] => gems
[date] => 2020-09-05
[quantity] => 0
)
[8] => Array (
[resourceKey] => lead
[date] => 2020-09-05
[quantity] => 0
)
[9] => Array (
[resourceKey] => limestone
[date] => 2020-09-05
[quantity] => 6
)
And I need to convert it is a certain way to display it with jquery Datatables:
$data= array(
array("Date" => "2020-09-05","cloth"=>"6","ebony"=>"3","gems"=>"0","lead"=>"0","limestone"=>"6"),
array("Date" => "2020-09-06","cloth"=>"8","ebony"=>"4","gems"=>"0","lead"=>"0","limestone"=>"8")
);
I've been trying for a while now but can't get a good way to iterate through the results...
I have already taken care of building the columns to pass to Datatables:
$columns = array();
$columns[] = array('data' => 'Date', 'title' => 'Date');
foreach ($result as $key => $row_data) {
$newResourceKey = $myUtilities->in_array_recursive($row_data['resourceKey'], $columns);
if ($newResourceKey === FALSE) { // Only if column name not yet in array
$columns[] = array('data' => $row_data['resourceKey'], 'title' => $row_data['resourceKey']);
}
}
This is an example of the many things I've tried:
$isDateInArray = $myUtilities->in_array_recursive($row_data['date'], $data);
if ($isDateInArray === FALSE) { // Only if Date is not in array yet
array_push($data, array('Date' => $row_data['date'], $row_data['resourceKey'] => $row_data['quantity'])); // Push date and data
}
else {
array_push($data, array($row_data['resourceKey'] => $row_data['quantity'])); // Only push data. Can't find a way to add it to existing Date array
}
Update:
public function in_array_recursive($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && $this->in_array_recursive($needle, $item, $strict))) {
return true;
}
}
return false;
}
You can get the desired result by next simple array transformation:
$res = [];
foreach ($result as $row) {
$res[$row['date']][$row['resourceKey']] = $row['quantity'];
}
$final = [];
foreach ($res as $d=>$r) {
array_push($final, array_merge(['date'=>$d], $r));
}
print_r($final);
Here you can try the PHP code
Related
Need to make recursive function from the below code. tried multiple ways but failed. because I don't know how much depth will the tree have. depth may be varying according to the data.
$tot_terms = [];
foreach ($tree as $key => $val) {
$tot_terms[$val->depth][] = $val->tid;
if (!empty($val->children)) {
foreach ($val->children as $key1 => $val1) {
$tot_terms[$val1->depth][] = $val1->tid;
if ($val1->children) {
foreach ($val1->children as $key2 => $val2) {
$tot_terms[$val2->depth][] = $val2->tid;
if ($val2->children) {
foreach ($val2->children as $key3 => $val3) {
$tot_terms[$val3->depth][] = $val3->tid;
if ($val3->children) {
foreach ($val3->children as $key4 => $val4) {
$tot_terms[$val4->depth][] = $val4->tid;
if ($val4->children) {
foreach ($val4->children as $key5 => $val5) {
$tot_terms[$val5->depth][] = $val5->tid;
}
}
}
}
}
}
}
}
}
}
}
and the result should be like this
Array
(
[0] => Array
(
[0] => 20011
[1] => 19991
[2] => 20000
)
[1] => Array
(
[0] => 20010
[1] => 19995
[2] => 19994
[3] => 19990
[4] => 20005
[5] => 19985
[6] => 19999
[7] => 19998
)
[2] => Array
(
[0] => 20012
[1] => 20006
[2] => 19996
[3] => 19993
[4] => 19989
[5] => 19988
[6] => 20004
[7] => 19984
[8] => 20001
)
[3] => Array
(
[0] => 20007
[1] => 20009
[2] => 20008
[3] => 19992
[4] => 19987
[5] => 19986
[6] => 19983
[7] => 19982
[8] => 20003
[9] => 20002
)
[4] => Array
(
[0] => 19997
)
[5] => Array
(
[0] => 19981
)
)
Tried according to other post but it is failing. input $tree is something that is array and an object. will post in next comment if required as total body count is more than its expected
We can use a handler function to store data. Note that we are passing the array by reference.
function func( $item, &$data ) {
if ( is_empty( $item ) ) return;
if ( ! is_object( $item ) ) return;
foreach ($item as $key => $val) {
$data[$val->depth][] = $val->tid;
func( $val->children, $data );
}
}
function func_handler( $item ) {
$data = [];
func( $item, $data );
return $data;
}
This is the result of array after i build it using array_push function from mssql result.
Array
(
[0] => Array
(
[STICKER] => FALCON
[MONTH] => 1
[JUM] => 65826210.00
)
[1] => Array
(
[STICKER] => FALCON
[MONTH] => 2
[JUM] => 68070573.00
)
[2] => Array
(
[STICKER] => FALCON
[MONTH] => 3
[JUM] => 99053067.60
)
[3] =>
[4] => Array
(
[STICKER] => HRD
[MONTH] => 2
[JUM] => 1521400.00
)
[5] => Array
(
[STICKER] => HRD
[MONTH] => 3
[JUM] => 2093200.00
)
)
I need to convert array above into this structure:
Array
(
[0] => Array
(
[0] =>
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => FALCON
[1] => 65826210.00
[2] => 68070573.00
[3] => 99053067.60
)
[2] => Array
(
[0] => HRD
[1] => 0
[2] => 1521400.00
[3] => 2093200.00
)
)
Note:
Array[0] values would be 1,2,3 (this is actualy month, i just input up to 3 in order to get the code not too long. but it will be up to 12 (Jan - Dec)).
If from original array, there is none value (example from array[3]), then it will be convert to new array[2]->[1] with value 0.
Any help would be very appreciated.
Thanks all!.
Try this,
If any month is not mentioned from stickers the jum considered as 0,
$array = array(
array('STICKER' => 'FALCON', 'MONTH' => 1, 'JUM' => 65826210.00),
array('STICKER' => 'FALCON', 'MONTH' => 2, 'JUM' => 68070573.00),
array('STICKER' => 'FALCON', 'MONTH' => 3, 'JUM' => 99053067.60),
array(),
array('STICKER' => 'HRD', 'MONTH' => 2, 'JUM' => 1521400.00),
array('STICKER' => 'HRD', 'MONTH' => 3, 'JUM' => 2093200.00),
);
$result[0][] = '';
foreach ($array as $key => $res) {
if (!empty($res)) {
$result[0][$res['MONTH']] = $res['MONTH'];
$result1[$res['STICKER']][$res['MONTH']] = $res['JUM'];
}
}
foreach ($result1 as $key => $res) {
$fin = array();
foreach ($res as $key1 => $re) {
foreach ($result[0] as $key2 => $month) {
if ($month != '') {
if (array_key_exists($month, $res)) {
if (!array_key_exists($month, $fin) && $month == $key1) {
$fin[$month] = $re;
}
} else {
$fin[$month] = 0;
}
}
}
}
$result[] = array_merge(array($key), $fin);
}
echo'<pre>';
print_r($result);
echo'<pre>';
Result:
Array
(
[0] => Array
(
[0] =>
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => FALCON
[1] => 65826210
[2] => 68070573
[3] => 99053067.6
)
[2] => Array
(
[0] => HRD
[1] => 0
[2] => 1521400
[3] => 2093200
)
)
I hope this is used to achieve your output(you mentioned in above question)!!
try something like this
not the issue with the empty array i could not understand how the program would know that it is STICKER HRD so it will be filled with zero.But you could later check if every month "isset" and if it is not act as it is zero
$arr1=array(
array('STICKER'=>'FALCON','MONTH'=>1,'JUM'=>65826210.00),
array('STICKER'=>'FALCON','MONTH'=>2,'JUM'=>68070573.00),
array('STICKER'=>'FALCON','MONTH'=>3,'JUM'=>99053067.60),
array(),
array('STICKER'=>'HRD','MONTH'=>2,'JUM'=>1521400.00),
array('STICKER'=>'HRD','MONTH'=>3,'JUM'=>2093200.00),
);
$arr2=array();
$arr3=array();
$arr2[0][0]="";
foreach($arr1 as $key => $val){
if(!empty($val)){
$arr2[0][$val['MONTH']]=$val['MONTH'];
//group each STICKER
$arr3[$val['STICKER']][]=$val['JUM'];
}
}
//transfer the grouped data to arr2
foreach($arr3 as $key => $val){
$tmp_arr=array($key);
$arr2[]=array_merge($tmp_arr,$val);
}
print_r($arr2);
I have tried many things but could not get the output, would really appreciate any help
Thank you
Array (
[0] => Array ( [Toolrepos] =>
Array (
[id] => 28
[created] => 2014-12-13
[tool_type] => new1
[tool_partnum] => new3
[tool_vernum] => 57.0.5
[box_id] => 28
[request_date] => 2014-12-14
[delivered_date] => 2014-12-14 ) )
[1] => Array ( [Toolrepos] =>
Array (
[id] => 29
[created] => 2014-12-13
[tool_type] => new4
[tool_partnum] => new5
[tool_vernum] => 1.2.56
[box_id] => 28
[request_date] => 2014-12-14
[delivered_date] => 2014-12-14 ) )
[2] => Array ( [Toolrepos] =>
Array ( [id] => 29
[created] => 2014-12-13
[tool_type] => SeatApp
[tool_partnum] => sw2
[tool_vernum] => 1.1.2
[box_id] => 34
[request_date] => 2014-12-13
[delivered_date] => 2014-12-13 ) ) )
I need the output like below
if box_id = '28' then i need their corresponding values for 'created','tool_type','tool_vernum'. Sometimes I need only 'created' value for matching box_id. Thank you
$box28s = array();
$i=0;
if (! empty($arr)) {
foreach ($arr as $elem) {
$curr = ! empty($elem['Toolrepos']) ? $elem['Toolrepos'] : NULL;
if (! empty($curr)) {
foreach ($curr as $k => $v) {
if ($k == 'id' && $v == 28) {
$box28s[$i] = $curr;
}
}
}
++$i;
}
}
In array count all true where category is 1 after counting the category will move to 2 thn count all true and so on?
Array generated:
foreach($_POST as $key => $data)
{
$a = explode("_", $key);
$f = array(
'category' => $a[1],
'answer' => $data
);
$f_data[] = $f;
}
FROM
Array
(
[0] => Array
(
)
[1] => Array
(
[category] => 1
[answer] => true
)
[2] => Array
(
[category] => 1
[answer] => true
)
[3] => Array
(
[category] => 1
[answer] => true
)
[4] => Array
(
[category] => 1
[answer] => false
)
[5] => Array
(
[category] => 1
[answer] => false
)
[6] => Array
(
[category] => 1
[answer] => true
)
[7] => Array
(
[category] => 1
[answer] => true
)
[8] => Array
(
[category] => 2
[answer] => true
)
[9] => Array
(
[category] => 2
[answer] => true
)
[10] => Array
(
[category] => 2
[answer] => true
)
[11] => Array
(
[category] => 2
[answer] => false
)
[12] => Array
(
[category] => 2
[answer] => true
)
[13] => Array
(
[category] => 2
[answer] => true
)
)
To
array (
category: 1,
count: 5
),
array(
category: 2,
count: 5
)
This is the solution :
$return = array();
foreach($cont as $item) {
if($item['answer'] == 'true') {
$verify = false;
foreach($return as $key => $value) {
if($value['category'] == $item['category']) {
$return[$key]['count']++;
$verify = true;
break;
}
}
if(!$verify)
$return[] = array('category' => $item['category'], 'count' => 1);
}
}
And this is better solution...
$return = array();
foreach($cont as $item) {
if($item['answer'] == 'true') {
if(array_key_exists($item['category'], $return))
$return[$item['category']]++;
else
$return[$item['category']] = 1;
}
}
Untested, but I believe this is what you are after (pending any minor mistakes):
foreach($_POST as $key => $data)
{
$a = explode("_", $key);
$f[$a[1]]['category'] = $a[1];
if ($data == "true") {
$f[$a[1]]['answer']++;
}
}
how can i count an element if it appears more than once in the same array?
I already tried with array_count_values, but it did not work, is it beacuse i got more than one key and value in my array?
This is my output from my array (restlist)
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla)
[1] => Array ( [restaurant_id] => 32144 [title] => test5)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
[3] => Array ( [restaurant_id] => 32144 [title] => test5)
[4] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
)
I want it to count how many times the same element appears in my array and then add the counted value to my newly created 'key' called hits in the same array.
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla [hits] => 1)
[1] => Array ( [restaurant_id] => 32144 [title] => test5 [hits] => 2)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 [hits] => 2)
)
This is how i tried to do what i wanted.
foreach ($cooltransactions as $key)
{
$tempArrayOverRestaurants[]= $key['restaurant_id'];
}
$wordsRestaruants = array_count_values($tempArrayOverRestaurants);
arsort($wordsRestaruants);
foreach ($wordsRestaruants as $key1 => $value1)
{
$temprestaurantswithhits[] = array(
'restaurant_id' => $key1,
'hits' => $value1);
}
foreach ($restlistas $key)
{
foreach ($temprestaurantswithhits as $key1)
{
if($key['restaurant_id'] === $key1['restaurant_id'])
{
$nyspisestedsliste[] = array(
'restaurant_id' => $key['restaurant_id'],
'title' => $key['title'],
'hits' => $key1['hits']);
}
}
}
I know this is probably a noob way to do what i want but i am still new at php..I hope you can help
Just try with associative array:
$input = array( /* your input data*/ );
$output = array();
foreach ( $input as $item ) {
$id = $item['restaurant_id'];
if ( !isset($output[$id]) ) {
$output[$id] = $item;
$output[$id]['hits'] = 1;
} else {
$output[$id]['hits']++;
}
}
And if you want to reset keys, do:
$outputWithoutKeys = array_values($output);