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 3 years ago.
Improve this question
Array output generated from database values.
Array ( [0] => Array ( [seldate] => 2019-04-28 [count] => 268 )
[1] => Array ( [seldate] => 2019-04-29 [count] => 366 )
[2] => Array ( [seldate] => 2019-04-30 [count] => 85 )
[3] => Array ( [seldate] => 2019-04-28 [count] => 93 )
[4] => Array ( [seldate] => 2019-04-29 [count] => 82 )
[5] => Array ( [seldate] => 2019-04-30 [count] => 31 )
[6] => Array ( [seldate] => 2019-04-28 [count] => 44 )
[7] => Array ( [seldate] => 2019-04-29 [count] => 44 )
[8] => Array ( [seldate] => 2019-04-30 [count] => 22 ) )
I need to create below
string output from above array for google LineChart.
"['2019-04-28', 268, 93, 44],
['2019-04-29', 366, 82, 44],
['2019-04-30', 85, 31, 22]"
Please help to create PHP Code logic.
First prepare your array as key being the date and the values being the values.
Then loop it again and write the lines to a new array with the imploded values.
Lastly output the lines with implode on comma and new line.
foreach($rows as $r){
$dates[$r['seldate']][] = $r['count'];
}
foreach($dates as $date => $vals){
$lines[] = "['" . $date . "', " . implode(", ", $vals) . "]";
}
echo implode(",\n", $lines);
Output:
['2019-04-28', 268, 93],
['2019-04-29', 366],
['2019-04-30', 85]
https://3v4l.org/14smE
You can traverse array by using array_walk as below
$result = [];
array_walk($arr, function($item) use(&$result){
if(empty($result[$item['seldate']])){ // if empty add date and count for initialisation
$result[$item['seldate']] = [$item['seldate'], $item['count']];
}else{
$result[$item['seldate']][] = $item['count']; // just append value to same key as date as we are grouping it by date
}
});
$result = array_values($result); // to remove date keys
print_r($result);
Output
Array
(
[0] => Array
(
[0] => 2019-04-28
[1] => 268
[2] => 93
[3] => 44
)
[1] => Array
(
[0] => 2019-04-29
[1] => 366
[2] => 82
[3] => 44
)
[2] => Array
(
[0] => 2019-04-30
[1] => 85
[2] => 31
[3] => 22
)
)
Demo
Note: If you want to use this data for linechart, json_encode above output.
Related
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 3 years ago.
Improve this question
I would like to order $ArrayToOrder according to column SecondArrayField2 in $SecondArray, where the link between the two arrays are Field_3 (in $ArrayToOrder) and SecondArrayField1 in $SecondArray.
$ArrayToOrder=Array
(
[0] => Array
(
[Field_1] => 13
[Field_2] => 15
[Field_3] => 3
)
[1] => Array
(
[Field_1] => 25
[Field_2] => 17
[Field_3] => 2
)
[2] => Array
(
[Field_1] => 121
[Field_2] => 20
[Field_3] => 11
)
)
$SecondArray=Array
(
[0] => Array
(
[SecondArrayField1] => 11
[SecondArrayField2] => Bruce
)
[1] => Array
(
[SecondArrayField1] => 3
[SecondArrayField2] => Arthur
)
[2] => Array
(
[SecondArrayField1] => 2
[SecondArrayField2] => Mary
)
)
Desired result as follows:
$ArrayToOrder=Array
(
[0] => Array
(
[Field_1] => 13
[Field_2] => 15
[Field_3] => 3 //(Arthur)
)
[1] => Array
(
[Field_1] => 121
[Field_2] => 20
[Field_3] => 11 //(Bruce)
)
[2] => Array
(
[Field_1] => 25
[Field_2] => 17
[Field_3] => 2 //(Mary)
)
)
Here is snippet you can use,
// first fetching key as SecondArrayField2 and SecondArrayField1 as value
$Field_3 = array_column($SecondArray, "SecondArrayField1","SecondArrayField2");
// sort by key alphabetically
ksort($Field_3);
//
$sorted = array_values(array_map(function($v) use ($ArrayToOrder) {
// first fetched Field_3 matching with current value of $Field_3 in the order
// get the index of matching Field_3
// save it to sorted array its sub array
return $ArrayToOrder[array_search($v, array_column($ArrayToOrder,'Field_3'))];
}, $Field_3));
print_r($sorted);die;
Demo.
Output:
Array
(
[0] => Array
(
[Field_1] => 13
[Field_2] => 15
[Field_3] => 3 //(Arthur)
)
[1] => Array
(
[Field_1] => 121
[Field_2] => 20
[Field_3] => 11 // (Bruce)
)
[2] => Array
(
[Field_1] => 25
[Field_2] => 17
[Field_3] => 2 // (Mary)
)
)
How I can remove duplicate entries based on Year and Month when my date format is YYYY-MM-DD? I tried removing days, but then I need to add the last day in the array, so my approach was wrong.
My array looks like this:
Array
(
[0] => Array
(
[id] => 9240399
[time] => 2018-01-01
[pages_indexed] => 942
)
[1] => Array
(
[id] => 9240322
[time] => 2018-01-02
[pages_indexed] => 940
)
[2] => Array
(
[id] => 9240344
[time] => 2018-01-03
[pages_indexed] => 947
)
[30] => Array
(
[id] => 9240344
[time] => 2018-01-31
[pages_indexed] => 947
)
[31] => Array
(
[id] => 9240344
[time] => 2018-02-01
[pages_indexed] => 1999
)
[32] => Array
(
[id] => 9240344
[time] => 2018-02-02
[pages_indexed] => 13339
)
Notice that I skipped some entries, so my dates are 2018-01-01, 2018-01-02, etc.
Array_unique would not work here since the day is different.
I tried this: ( $entries['time'] is like ex: 2018-01-01. )
$remove = DATE("Y-m",$entries['time']);
$entriesa = array_unique($remove);
$entries['time'] = $entriesa;
Well... you could loop through your results and index each key as the Year and Month, and then update this index with the row that fits the pattern, meaning you would only have the rows you expect (but you would only have the last reference of them).
Like this:
$expectedArray = [];
foreach ($arrayDuplicated as $item) {
$indexKey = substr($item['time'], 0, 7);
$expectedArray[$indexKey] = $item;
}
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 PHP array like the following one and I would like to combine a multi array to one array:
Array
(
[0] => ADB_DW2017
[1] => LM9
[2] => MS_OF2013
)
Array
(
[0] => NK
[1] => PV
[2] => NK
)
Array
(
[0] => 15
[1] => 25
[2] => 10
)
Array
(
[0] => 250
[1] => 111
[2] => 150
)
Array
(
[0] => 450
[1] => 123
[2] => 250
)
Array
(
[0] => 0
[1] => Mien thue
[2] => 5
)
Array
(
[0] => 200
[1] => 12
[2] => 100
)
Array
(
[0] => 6.750
[1] => 3.075
[2] => 2.500
)
I want to combine like this, with the highest performance :
Array
(
[ADB_DW2017]=>array([suppiler]=>NK
[min_pro]=>15
[max_pro]=>250
[avg_pro]=>450
[tax]=>0
[com]=>200
[sum]=>6.750
)
)
My question is : How to combine a multi array to one array in PHP with the highest performance ?
I assume array names are $first_array,$second_array,...... (so change varabile names accordingly).
Do like below:-
$final_array = array();
foreach ($first_array as $key=> $arr){
$final_array[$arr] =array(
'suppiler'=>(isset($second_array[$key]))? $second_array[$key]: '',
'min_pro'=>(isset($third_array[$key]))? $third_array[$key]: 0,
'max_pro'=>(isset($fourth_array[$key]))? $fourth_array[$key]: 0,
'avg_pro'=>(isset($fifth_array[$key]))? $fifth_array[$key]: 0,
'tax'=>(isset($sixth_array[$key]))? $sixth_array[$key]: '',
'com'=>(isset($seventh_array[$key]))? $seventh_array[$key]: 0,
'sum'=>(isset($eigth_array[$key]))? $eigth_array[$key]: 0
);
}
echo "<pre/>";print_r($final_array);
Output:-https://eval.in/829938
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
I would like to split the below array into 3 single arrays. However, the size of the array can be any number. The below array size is 3.
Array (
[0] => Array
(
[0] => a
[1] => bb
[2] => c
[3] => dd
[4] => ee
)
[1] => Array
(
[0] => dd
[1] => ff
[2] => hh
[3] => iji
[4] => kkk
[5] => a
[6] => cc
)
[2] => Array
(
[0] => ee
[1] => kk
[2] => iji
[3] => a
[4] => bb
[5] => lmn
[6] => ppq
[7] => xyz
)
)
Expected output:
Array1 (
[0] => a
[1] => bb
[2] => c
[3] => dd
[4] => ee
)
Array2 (
[0] => dd
[1] => ff
[2] => hh
[3] => iji
[4] => kkk
[5] => a
[6] => cc
)
Array3 (
[0] => ee
[1] => kk
[2] => iji
[3] => a
[4] => bb
[5] => lmn
[6] => ppq
[7] => xyz
)
The name of the individual array should be followed by a number that increments for each array.
You can break a 3d array into 3 separate arrays using list().
list($array1, $array2, $array3) = $mainArray;
Here's an example: https://3v4l.org/hQ1Te
Now if you don't know how many arrays are going to be in the input array you can do something like this...
for($i=0; $i<count($mainArray); $i++){
$variableName = "array$i";
$$variableName = $mainArray[$i];
}
var_dump($array1, $array2, $array3, $array4, ...);
So are you looking for something like this?
for ($i = 0; $i < count($inputArray); $i++) {
echo "Array ". $i;
echo "<pre>";
print_r($inputArray[$i]);
echo "</pre>";
}
Edit: ok now I get it, you want:
for ($i = 0; $i < count($inputArray); $i++) {
$name = "array" . ($i + 1);
$$name = $inputArray[$i];
}
I have the following two arrays of objects:
First Array: $array1
Array
(
[0] => Array
(
[match] => 1
[when] => 2013-10-13 15:00:00
[a] => AD
[b] => NiP
[winner] => c
[closed] => 1
[event] => Fragbite Masters
[format] => 3
)
[1] => Array
(
[match] => 2
[when] => 2013-10-13 15:00:00
[a] => VG
[b] => AD
[winner] => a
[closed] => 1
[event] => Starseries
[format] => 5
)
[2] => Array
(
[match] => 3
[when] => 2013-10-13 21:15:00
[a] => Serbia
[b] => Portugal
[winner] => a
[closed] => 1
[event] => ESEC
[format] => 1
)
)
Second Array: $array2
Array
(
[0] => Array
(
[match] => 1
[a] => 58
[b] => 107
)
[1] => Array
(
[match] => 2
[a] => 174
[b] => 162
)
[2] => Array
(
[match] => 3
[a] => 64
[b] => 59
)
)
I would like to get something like this:
Array
(
[0] => Array
(
[match] => 1
[when] => 2013-10-13 15:00:00
[a] => AD
[b] => NiP
[winner] => c
[closed] => 1
[event] => Fragbite Masters
[format] => 3
[per_a] => 58
[per_b] => 107
)
[1] => Array
(
[match] => 2
[when] => 2013-10-13 15:00:00
[a] => VG
[b] => AD
[winner] => a
[closed] => 1
[event] => Starseries
[format] => 5
[per_a] => 174
[per_b] => 162
)
[2] => Array
(
[match] => 3
[when] => 2013-10-13 21:15:00
[a] => Serbia
[b] => Portugal
[winner] => a
[closed] => 1
[event] => ESEC
[format] => 1
[per_a] => 64
[per_b] => 59
)
)
Where the key name [a] and [b] from the second array have been modified to [per_a] and [per_b].
Things i tried to merge both files:
array_merge & array_merge_recursive: both get me a result where the merged values of $array2 are appended to the end of $array1.
array_combine: Wont work because $array1 and $array2 haven't an equal number of elements.
This is just a part of each file, both are not equal in term of elements.
If you have an answer for me it would be apreciated ! thx in advance !
Try this you need to modify your keys in function. see Demo
$match : will check key exists in both arrays. $whereKey1
$whereKey2 : pick up the values from second array and put it in your
new key which $newkey1 and $newkey2
function key_compare_func($arr1, $arr2)
{
$newarray = array();
$match = "match";
$whereKey1 = "a";
$whereKey2 = "b";
$newKey1 = "per_a";
$newKey2 = "per_b";
if(is_array($arr1) && is_array($arr2)){
if($arr1[$match] == $arr2[$match] ){
$newarray = array_merge($arr1, array($newKey1 => $arr2[$whereKey1], $newKey2 => $arr2[$whereKey2]));
}
}
return $newarray;
}
$modifiedArray = array_map("key_compare_func",$A1, $A2);
echo '<pre>';print_r($modifiedArray);echo '</pre>';
Output:
[0] => Array
(
[match] => 1
[when] => 2013-10-13 15:00:00
[a] => AD
[b] => NiP
[winner] => c
[closed] => 1
[event] => Fragbite Masters
[format] => 3
[per_a] => 58
[per_b] => 107
)
[1] => Array
(
[match] => 2
[when] => 2013-10-13 15:00:00
[a] => VG
[b] => AD
[winner] => a
[closed] => 1
[event] => Starseries
[format] => 5
[per_a] => 174
[per_b] => 162
)
Assuming that your second array only hold unique match ids, then you can use it as a lookup array after applying the match values as first level keys. array_column() makes this task very simple.
Then simply iterate the over the rows in the first array (modifying by reference so that changes to rows are applied to the original array).
Using this technique, it won't matter if the corresponding rows between the two arrays have different original first level keys (the same cannot be said for #Noman's answer which requires each row from the first array to have a cooresponding row with the exact same position in the second array).
Code: (Demo)
$lookup = array_column($A2, null, 'match');
foreach ($A1 as &$row) {
if (isset($lookup[$row['match']])) {
$row['per_a'] = $lookup[$row['match']]['a'];
$row['per_b'] = $lookup[$row['match']]['b'];
}
}
var_export($A1);