highcharts php array unset/reset keys after sorting - php

I have the following array:
Array
(
[0] => Array
(
[0] => 2015-07-18
[1] => 22 SSH
)
[1] => Array
(
[0] => 2015-07-18
[1] => 80 HTTP
)
[2] => Array
(
[0] => 2015-07-18
[1] => 3389 Remote Desktop
)
[3] => Array
(
[0] => 2015-07-19
[1] => 3389 Remote Desktop
)
[4] => Array
(
[0] => 2015-07-19
[1] => 3389 Remote Desktop
)
)
and the following function to bring the data in the needed format/array for highcharts:
$result = array();
$result[0] = array();
$result[0][data] = array();
foreach ($stack_stats_timeline as $key => &$value) {
if(!in_array($value[0], $result[0][data], true)) array_push($result[0][data], $value[0]);
$hash = $value[1];
$result[$hash][name] = $value[1];
$result[$hash][data][$value[0]] += 1;
}
so far so good... hoever the problem is that when i do
$result = json_encode($result);
print_r($result);
I get
[{"data":["2015-07-01","2015-07-02","2015-07-03"]},{"name":"8080 Unknown","data":{"2015-07-01":4,"2015-07-02":8,"2015-07-03":5}},{"name":"8118 Unknown","data":{"2015-07-01":3}},{"name":"3389 Remote Desktop","data":{"2015-07-01":14,"2015-07-02":52,"2015-07-03":65}},{"name":"80 HTTP","data":{"2015-07-01":3,"2015-07-02":12,"2015-07-03":7}},{"name":"8228 Unknown","data":{"2015-07-01":3}}]
the problem is in data when the format is:
{"key":number,"key":number}
this should be only:
{number,number}
QUESTION: How can I remove the array keys after I sorted the occurences by date?

I would probably do something along the lines of:
$headings = $result[0]['data'];
for ($i = 0; $i < count($result[1]); $i ++) {
$data = $result[1][$i]['data'];
$newdata = array();
foreach($headings as $key)
$newdata[] = isset($data[$key]) ? $data[$key] : 0;
$result[1][$i]['data'] = $newdata;
}

Related

Combining Arrays while merging the values with the same key

I have two arrays with same amount of values. I need to combine them ( array1 value to key, array2 value as value) without losing the values of the second array due to duplicate key. when I use combine_array() as expected it just gets the last value of the second array with the same key.
Array
(
[0] => 1
[1] => 2
[2] => 2
[3] => 3
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Desired result
Array
(
[1] => 1
[2] => Array(
[0]=>2
[1]=>3
)
[3] => 2
)
This code will meet your request
$array1 = array("0"=>1,"1"=>2,"2"=>2,"3"=>3);
$array2 = array("0"=>1,"1"=>2,"2"=>3,"3"=>4);
$array = array();
foreach($array1 as $key => $value){
if($value != $array2[$key]){
$array[$key][] = $value;
$array[$key][] = $array2[$key];
}else{
$array[$key] = $value;
}
}
print_r($array);
The desired result is
Array
(
[0] => 1
[1] => 2
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 3
[1] => 4
)
)
I'm sure there are way better solutions than this, but it does the job for now. I would appreciate if someone can send a better written solution.
$combined = array();
$tempAr = array();
$firstMatch = array();
$count = 0;
foreach ($array1 as $index => $key) {
if (array_key_exists($key, $combined)) {
$tempAr[] = $array2[$index];
$count++;
} else {
$totalCount = $count;
}
if (!array_key_exists($key, $firstMatch)) {
$firstMatch[$key] = $array2[$index];
}
$output = array_slice($tempAr, $totalCount);
$combined[$key] = $output;
}
$combined = array_merge_recursive($firstMatch, $combined);

Need to find the all nearby node elements for every node on the graph using PHP

Problem
Need to find the all nearby node elements for every node on the following graph using PHP
Graph
Input String
$string_arr = array (‘c1#c2#6’, ‘c2#c3#12’, ‘c2#c4#3’, ‘c3#c5#22’, ‘c3#c6#23’, ‘c4#c7#13’, ‘c5#c8#16’, ‘c6#c8#11’, ‘c6#c9#9’, ‘c7#c9#12’, ‘c9#c10#15’, ‘c8#c10#7’);
(Use above variable as input parameter for your code. And you can treat it as string or array or array of string)
Required OUTPUT:
Please print the array showing all node elements with their nearby node.
e.g.
Array(
[c1] => Array
(
[0] => c2
)
[c2] => Array
(
[0] => c1
[1] => c3
[2] => c4
)
)
My Code
<?php
$string_arr = array('c1#c2#6', 'c2#c3#12', 'c2#c4#3', 'c3#c5#22', 'c3#c6#23', 'c4#c7#13', 'c5#c8#16', 'c6#c8#11', 'c6#c9#9',
'c7#c9#12', 'c9#c10#15', 'c8#c10#7');
foreach ($string_arr as $k => $v) {
$tmp = explode('#', $v);
$new_array[$tmp[0]][] = $tmp[1];
#$new_array2[$tmp[0]][] = $tmp[2];
}
print_r($new_array);
?>
My Output
Array (
[c1] => Array
(
[0] => c2
)
[c2] => Array
(
[0] => c3
[1] => c4
)
[c3] => Array
(
[0] => c5
[1] => c6
)
[c4] => Array
(
[0] => c7
)
[c5] => Array
(
[0] => c8
)
[c6] => Array
(
[0] => c8
[1] => c9
)
[c7] => Array
(
[0] => c9
)
[c9] => Array
(
[0] => c10
)
[c8] => Array
(
[0] => c10
)
)
I think all you need to do is also add the items in the other way round, so you currently add...
$new_array[$tmp[0]][] = $tmp[1];
so also add with the values swapped
$new_array[$tmp[1]][] = $tmp[0];
To give...
foreach ($string_arr as $k => $v) {
$tmp = explode('#', $v);
$new_array[$tmp[0]][] = $tmp[1];
$new_array[$tmp[1]][] = $tmp[0];
}
<?php
$string_arr = array ('c1#c2#6', 'c2#c3#12', 'c2#c4#3', 'c3#c5#22', 'c3#c6#23', 'c4#c7#13', 'c5#c8#16', 'c6#c8#11', 'c6#c9#9', 'c7#c9#12', 'c9#c10#15', 'c8#c10#7');
$simplifyArr = array();
foreach ($string_arr as $key => $value) {
$simplifyArr[] = getnode($value);
}
$keysArr = array_unique(array_column($simplifyArr, 'parent'));
$outputArr = array();
foreach ($keysArr as $key) {
foreach ($simplifyArr as $value) {
if($value['parent'] == $key){
$outputArr[$key][] = $value['child'];
}
if($value['child'] == $key){
$outputArr[$key][] = $value['parent'];
}
}
}
echo "<pre>";
print_r($outputArr);
echo "</pre>";
function getnode($string)
{
$extract = explode('#', $string);
$arr['parent'] = $extract[0];
$arr['child'] = $extract[1];
$arr['weight'] = $extract[2];
return $arr;
}
?>

How to build custom array in multi level foreach?

Here is the raw data
Array
(
[name] => me
[tickets] => Array
(
[1] => Array
(
[equipment] => Array
(
[1] => Array
(
[name] => DVR
[received] => 10
)
[2] => Array
(
[name] => DCT
[received] => 3
)
)
)
[2] => Array
(
[equipment] => Array
(
[1] => Array
(
[name] => DVR
[received] => 4
)
[2] => Array
(
[name] => DCT
[received] => 6
)
)
)
)
)
Users have multiple tickets, but each ticket has the same item with different 'received' amounts. I would like to sum the received amount into one variable/array.
Here is a demo of how I would like to get it to work like
Array
(
[name] => me
[equipment] => Array
(
[DVR] => 14
[DCT] => 9
)
)
Here is my most recent failed attempt at building my own array from a multidimensional array.
foreach($data as $user){
$sum = [];
$sum['name'] = $user->name;
$sum['equipment'] = [];
foreach($user->tickets as $ticket){
foreach($ticket->equipments as $eqpt){
$sum['equipment'][$eqpt['name']] += $eqpt['pivot']['received'];
}
}
print_r($sum);
}
Please try the following code. There's only a single user in your $data, though, so you need to do $data = [$data]; first.
foreach ($data as $user) {
$sum = [];
$sum['name'] = $user['name'];
$sum['equipment'] = [];
foreach($user['tickets'] as $ticket){
foreach($ticket['equipment'] as $eqpt){
$sum['equipment'][$eqpt['name']] += $eqpt['received'];
}
}
print_r($sum);
}
PHP arrays are accessed with square bracket syntax
There's probably a typo in $ticket->equipments.
Try with this:
$array = $data; //$data is your array
$sum = array('name' => $array['name'], 'equipment' => array());
foreach($array['tickets'] as $row) {
for($i = 0; $i < count($row); $i++) {
foreach($row['equipment'] as $infos) {
$sum['equipment'][$infos['name']] += $infos['received'];
//print_r($infos);
}
}
}
print_r($sum);
well, after much googling and trial and error this appears to work
$sum = [];
// $data is a collection returned by Laravel
// I am converting it to an array
foreach($data->toArray() as $user){
$items = [];
foreach($user['tickets'] as $ticket){
foreach($ticket['equipments'] as $eqpt){
$name = $eqpt['name'];
if (! isset($items[$name]))
{
$items[$name] = $eqpt['received'];
} else {
$items[$name] += $eqpt['received'];
}
}
}
$sum[] = [
'name' => $user['name'],
'equipment' => $items
];
}
#tsnorri #Adrian Cid Almaguer

Formatting an Array in PHP

I'm pulling an array from the database and it looks like so:
Array
(
[0] => Array
(
[tracker_id] => 28
[tracking_numbers] => hdkshwuy47937892hd
)
[1] => Array
(
[tracker_id] => 28
[tracking_numbers] => 797825464411
)
)
I need to reformat it to look like this:
Array
(
[0] => Array
(
[tracker_id] => 28
[tracking_numbers] => Array
(
[0] => hdkshwuy47937892hd
[1] => 797825464411
)
)
)
I can't seem find the right search in the array or keys to create an array of numbers for the single tracker id.
Use array_column() for < php V5.5
<?php
$a=array
( array
('tracker_id' => 28,
'tracking_numbers'=> "hdkshwuy47937892hd"
),
array('tracker_id' => 28,
'tracking_numbers' => "797825464411",
) );
$a[0]['tracking_numbers']=array_column($a,"tracking_numbers");
unset($a[1]);
print_r($a);
Demo
try this
$arr_output = array();
foreach($arr_input as $arr)
{
$tracker_id = $arr['tracker_id'];
$tracking_numbers = $arr['tracking_numbers'];
$arr_output[$traker_id][] = $tracking_numbers;
}
print_r($arr_output);
UPDATE 2:
$arr_output = array();
$arr_output1 = array();
foreach($arr_input as $arr)
{
$tracker_id = $arr['tracker_id'];
$tracking_numbers = $arr['tracking_numbers'];
$arr_output[$traker_id][] = $tracking_numbers;
}
$i=0;
foreach($arr_output as $key=>$value)
{
$arr_output1[$i]['tracker_id']=$key
$arr_output1[$i]['tracking_numbers']=$value
$i+=1;
}
print_r($arr_output1);

PHP Group array by values

I have an array like this:
Array (
[0] => ing_1_ing
[1] => ing_1_amount
[2] => ing_1_det
[3] => ing_1_meas
[4] => ing_2_ing
[5] => ing_2_amount
[6] => ing_2_det
[7] => ing_2_meas
)
And I want to group the values into an array like this:
Array (
[0] => Array(
[0] => ing_1_ing
[1] => ing_1_amount
[2] => ing_1_det
[3] => ing_1_meas
)
[1] => Array(
[0] => ing_2_ing
[1] => ing_2_amount
[2] => ing_2_det
[3] => ing_2_meas
)
)
There may be many other items named like that: ing_NUMBER_type
How do I group the first array to the way I want it? I tried this, but for some reason, strpos() sometimes fails:
$i = 1;
foreach ($firstArray as $t) {
if (strpos($t, (string)$i)) {
$secondArray[--$i][] = $t;
} else {
$i++;
}
}
What is wrong? Can you advice?
It depends what you are trying to achieve, if you want to split array by chunks use array_chunk method and if you are trying to create multidimensional array based on number you can use sscanf method in your loop to parse values:
$result = array();
foreach ($firstArray as $value)
{
$n = sscanf($value, 'ing_%d_%s', $id, $string);
if ($n > 1)
{
$result[$id][] = $value;
}
}
<?php
$ary1 = array("ing_1_ing","ing_1_amount","ing_1_det","ing_1_meas","ing_2_ing","ing_2_amount","ing_2_det","ing_2_meas");
foreach($ary1 as $val)
{
$parts = explode("_",$val);
$ary2[$parts[1]][]=$val;
}
?>
This creates:
Array
(
[1] => Array
(
[0] => ing_1_ing
[1] => ing_1_amount
[2] => ing_1_det
[3] => ing_1_meas
)
[2] => Array
(
[0] => ing_2_ing
[1] => ing_2_amount
[2] => ing_2_det
[3] => ing_2_meas
)
)
What I'd do is something like this:
$result = array();
foreach ($firstArray as $value)
{
preg_match('/^ing_(\d+)_/', $value, $matches);
$number = $matches[1];
if (!array_key_exists($number, $result))
$result[$number] = array();
$result[$number][] = $value;
}
Basically you iterate through your first array, see what number is there, and put it in the right location in your final array.
EDIT. If you know you'll always have the numbers start from 1, you can replace $number = $matches[1]; for $number = $matches[1] - 1;, this way you'll get exactly the same result you posted as your example.

Categories