I apologize for not being word-perfect in English.
I have this result from a foreach loop in php.
my file is jason.
Merging more value into one array
Array
(
[777565] => Array
(
[0] => Array
(
[0] => 777565-1
[1] => 777565-2
)
[1] => Array
(
[0] => 777565-3
[1] => 777565-4
)
[2] => Array
(
[0] => 777565-5
[1] => 777565-6
)
)
[777566] => Array
(
[0] => Array
(
[0] => 777566-1
[1] => 777566-2
)
[1] => Array
(
[0] => 777566-3
[1] => 777566-4
)
[2] => Array
(
[0] => 777566-5
[1] => 777566-6
)
)
)
but, I want Something like this:
Array
(
[777565] => Array
(
[0] => 777565-1
[1] => 777565-2
[2] => 777565-3
[3] => 777565-4
[4] => 777565-5
[5] => 777565-6
)
[777566] => Array
(
[0] => 777566-1
[1] => 777566-2
[2] => 777566-3
[3] => 777566-4
[4] => 777566-5
[5] => 777566-6
)
)
I tried hard and searched the internet but I could not find any way.
Of course, I have the ability to move it to the database first and then to the array, but I think there should be a faster way. What do you think?
thanks for reply.
If you have no problem looping through it and flatten the array according to your desire then you can try this:
$parent =
Array
(
[777565] => Array
(
[0] => Array
(
[0] => 777565-1
[1] => 777565-2
)
[1] => Array
(
[0] => 777565-3
[1] => 777565-4
)
[2] => Array
(
[0] => 777565-5
[1] => 777565-6
)
)
[777566] => Array
(
[0] => Array
(
[0] => 777566-1
[1] => 777566-2
)
[1] => Array
(
[0] => 777566-3
[1] => 777566-4
)
[2] => Array
(
[0] => 777566-5
[1] => 777566-6
)
)
);
$length = count($parent);
$result=[];
for($i=0; $i<$length; $i++){
for($j=0; $j<3; $j++){
$l=0;
for($k=0; $k<2; $k++){
$result[777565+$i][$j][$l++] = $parent[777565+$i][$j][$k];
}
}
}
Related
Im trying to insert the $hour_prices array in to MySQL. But im not sure how to do that with an array in a loop.
As i understand i will have to implode the array because MySql does not not understand the array data. But nothing is inserted to the database.
As i understand i will have to implode the array because MySql does not not understand the array data. But nothing is inserted to the database. I do have it working with an associative array though.
My arrays looks like this:
Array ( [0] => Array ( [0] => 2023-01-22T23:00:00 [1] => DK2 [2] => 1103.27002 [3] => 0.1397 ) )
Array ( [0] => Array ( [0] => 2023-01-22T22:00:00 [1] => DK2 [2] => 1170.599976 [3] => 0.1397 ) )
Array ( [0] => Array ( [0] => 2023-01-22T21:00:00 [1] => DK2 [2] => 1237.920044 [3] => 0.1397 ) )
Array ( [0] => Array ( [0] => 2023-01-22T20:00:00 [1] => DK2 [2] => 1299.73999 [3] => 0.1397 ) )
Array ( [0] => Array ( [0] => 2023-01-22T19:00:00 [1] => DK2 [2] => 1481.709961 [3] => 0.1397 ) )
Array ( [0] => Array ( [0] => 2023-01-22T18:00:00 [1] => DK2 [2] => 1503.290039 [3] => 0.1397 ) )
Array ( [0] => Array ( [0] => 2023-01-22T17:00:00 [1] => DK2 [2] => 1428.300049 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T16:00:00 [1] => DK2 [2] => 1272.369995 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T15:00:00 [1] => DK2 [2] => 1143.52002 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T14:00:00 [1] => DK2 [2] => 1124.77002 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T13:00:00 [1] => DK2 [2] => 892.580017 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T12:00:00 [1] => DK2 [2] => 807.849976 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T11:00:00 [1] => DK2 [2] => 925.390015 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T10:00:00 [1] => DK2 [2] => 1023.960022 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T09:00:00 [1] => DK2 [2] => 900.099976 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T08:00:00 [1] => DK2 [2] => 639.869995 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T07:00:00 [1] => DK2 [2] => 482.970001 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T06:00:00 [1] => DK2 [2] => 456.929993 [3] => 1.2576 ) )
Array ( [0] => Array ( [0] => 2023-01-22T05:00:00 [1] => DK2 [2] => 465.630005 [3] => 1.2576 ) )
Array ( [0] => Array ( [0] => 2023-01-22T04:00:00 [1] => DK2 [2] => 520.840027 [3] => 1.2576 ) )
Array ( [0] => Array ( [0] => 2023-01-22T03:00:00 [1] => DK2 [2] => 531.549988 [3] => 1.2576 ) )
Array ( [0] => Array ( [0] => 2023-01-22T02:00:00 [1] => DK2 [2] => 543.530029 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T01:00:00 [1] => DK2 [2] => 588.97998 [3] => 0.4192 ) )
Array ( [0] => Array ( [0] => 2023-01-22T00:00:00 [1] => DK2 [2] => 590.690002 [3] => 0.4192 ) )
<?php
// Brooker pricelist
$url1 = 'https://api.energidataservice.dk/dataset/Elspotprices?start=2023-01-22T00%3A00&end=2023-01-23T00%3A00&columns=HourDK%2C%20PriceArea%2C%20SpotPriceDKK&filter=%7B%22PriceArea%22%3A%20%22DK2%22%7D';
// Grid pricelist
$url2 = 'https://api.energidataservice.dk/dataset/DatahubPricelist?start=2023-01-22T00%3A00&end=2023-01-23T00%3A00&filter=%7B%22ChargeOwner%22%3A%20%22TREFOR%20El-net%20A%2FS%22%2C%20%22Note%22%3A%20%22Nettarif%20C%20time%22%7D&limit=1&timezone=DK';
$json1 = file_get_contents($url1);
$json2 = file_get_contents($url2);
$dataset_1 = json_decode($json1, true);
$dataset_2 = json_decode($json2, true);
for ($hour = 0; $hour < 24; $hour++) {
$hour_prices = array(
$dataset_1['records'][$hour]['HourDK'] // HourDK
, $dataset_1['records'][$hour]['PriceArea'] // PriceArea
, $dataset_1['records'][$hour]['SpotPriceDKK'] // SpotPriceDKK
, $dataset_2['records'][0]['Price' . ($hour + 1)] // GridPrice
);
echo "</br>";
print_r(array($hour_prices));
include "config.php";
}
$sql = "INSERT INTO elpriser (HourDK, PriceArea, SpotPriceDKK, GridPrice) values ";
$sql .= implode(',', $hour_prices);
mysqli_query($con, $sql);
echo "</br>";
echo "</br>";
print_r(array($hour_prices));
$sql = mysqli_query($con,"SELECT * FROM elpriser");
while($row = mysqli_fetch_assoc($sql)){
$HourDK = $row['HourDK'];
$PriceArea = $row['PriceArea'];
$SpotPriceDKK = $row['SpotPriceDKK'];
$GridPrice = $row['GridPrice'];
echo "Timepris : ".$HourDK.", Region : ".$PriceArea.", Pris elbørs : ".$SpotPriceDKK.", Pris elnet : ".$GridPrice."<br>";
}
?>
Use a prepared statement with bound parameters, then assign the variables that the parameters are bound to in a loop.
$stmt = $con->prepare("INSERT INTO elpriser (HourDK, PriceArea, SpotPriceDKK, GridPrice) values (?, ?, ?, ?)");
$stmt->bind_param("ssff", $hour, $area, $spotprice, $price);
foreach ($hour_prices as $row) {
list($hour, $area, $spotprice, $price) = $row;
$stmt->execute();
}
As I can see and If I understand your answer correctly the query should be executed inside the $hour loop.
Also you missed Parentheses on your query.
if include "config.php"; include db configuration it better be outside the loop
...
for ($hour = 0; $hour < 24; $hour++) {
$hour_prices = array(
$dataset_1['records'][$hour]['HourDK'] // HourDK
, $dataset_1['records'][$hour]['PriceArea'] // PriceArea
, $dataset_1['records'][$hour]['SpotPriceDKK'] // SpotPriceDKK
, $dataset_2['records'][0]['Price' . ($hour + 1)] // GridPrice
);
echo "</br>";
print_r(array($hour_prices));
include "config.php";
$sql = "INSERT INTO elpriser (HourDK, PriceArea, SpotPriceDKK, GridPrice) values ";
$sql .= '("' .implode('","', $hour_prices) . '")';
}
...
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 7 months ago.
There are many references on S/O showing various methods to flatten a multidimensional recursive array (with more than two levels). I have been through dozens (and tried most) but I'm still running into an odd problem with every one I've tried. What I am getting as a result is:
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000043
[1] => 1000045
[2] => 1000050
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000030
[1] => 1000032
[2] => 1000058
[3] => 1000064
) ...
But what I'm expecting is a truly flattened single array:
Array
[0] => 1000043
[1] => 1000045
[2] => 1000050
[3] => 1000030
[4] => 1000032
[5] => 1000058
[6] => 1000064
)
The method I found on S/O is supposed to handle an "empty array" (which I assume is the problem) but I'm still getting the wrong output. Here is my code:
function array_flatten5(array $array)
{
$flat = array(); // initialize return array
$stack = array_values($array); // initialize stack
while($stack) // process stack until done
{
$value = array_shift($stack);
if (is_array($value)) // a value to further process
{
$stack = array_merge(array_values($value), $stack);
}
else // a value to take
{
$flat[] = $value;
}
}
return $flat;
}
Could someone point out what I missing here because I'm thinking it's something simple but at this point my eyes are crossed with the number of attempts I've made. Thank you for any help you can provide.
Here is the original array. It is 4-deep:
Array ( [0] => 1000043 [1] => 1000045 [2] => 1000050 ) Array ( [0] => 1000030 [1] => 1000032 [2] => 1000058 [3] => 1000064 ) Array ( [0] => 1000041 [1] => 1000059 [2] => 1000069 ) Array ( [0] => 1000021 [1] => 1000044 [2] => 1000049 [3] => 1000071 ) Array ( [0] => 1000009 [1] => 1000013 [2] => 1000015 [3] => 1000017 [4] => 1000053 ) Array ( [0] => 1000022 [1] => 1000034 [2] => 1000070 ) Array ( [0] => 1000038 [1] => 1000047 [2] => 1000055 [3] => 1000063 ) Array ( [0] => 1000019 [1] => 1000054 [2] => 1000060 [3] => 1000066 [4] => 1000068 ) Array ( [0] => 1000006 [1] => 1000014 [2] => 1000016 [3] => 1000072 ) Array ( [0] => 1000024 [1] => 1000025 [2] => 1000046 [3] => 1000061 [4] => 1000067 ) Array ( [0] => 1000028 [1] => 1000039 [2] => 1000048 ) Array ( [0] => 1000042 [1] => 1000057 ) Array ( [0] => 1000027 [1] => 1000033 [2] => 1000036 [3] => 1000037 ) Array ( [0] => 1000008 [1] => 1000010 [2] => 1000012 [3] => 1000018 ) Array ( [0] => 1000026 [1] => 1000062 [2] => 1000065 ) Array ( [0] => 1000020 [1] => 1000023 [2] => 1000031 [3] => 1000035 [4] => 1000040 ) Array ( [0] => 1000007 [1] => 1000011 [2] => 1000029 ) Array ( [0] => 1000051 [1] => 1000052 [2] => 1000056 ) Array ( [0] => 1000001 [1] => 1000002 [2] => 1000003 [3] => 1000004 [4] => 1000005 ) Array ( [0] => 1000073 )
And here is the outcome using the array_walk_recursive suggestion ...
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000111
[1] => 1000113
[2] => 1000129
[3] => 1000134
)
Array
(
)
Array
(
)
Array
(
[0] => 1000012
[1] => 1000085
)
Array
(
) ...
You didn't prepare suitable array, but looking on this code you need probably just array_walk_recursive() function.
$array = [
[1, 2, 3, 4],
[[5, 6], [7, 8]],
[[[9], [10]], [11]]
];
$result = [];
array_walk_recursive($array, function ($tempV) use (&$result) {
$result[] = $tempV;
});
print_r($result);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
)
-----problem solved, see the update 2 below----
I put all MySQL query results into a 2D array:
$suppDescription=mysql_query($query);
$rows = mysql_num_rows($results);
$allSupplierInfo=array();
for($i=0; $i<$rows; $i++){
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
But now I cannot access the $allSuppliersInfo fields.
echo $allSupplierInfo[1][1]; // prints out 'Array'
echo $allSupplierInfo[1]['id']; //prints out nothing
What am I doing wrong?
------- UPDATE-----
print_r($allSupplierInfo) prints the following, so it looks like the loop is not working as I wanted it to:
Array (
[0] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) )
[1] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) )
[2] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) )
[3] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) )
[4] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) [4] => Array ( [0] =>ID_E[1] => Name_E [2] => Address_E [3] => Address_E ) ) )
------ UPDATE 2-----
Using the while loop, as suggested by RiggsFolly, solved the problem and I can access the fields as I initially wanted. I still do not understand why the for loop I used did not loop as I thought it would - any explanation would be greatly appreciated.
I think you got confused with your mysql result processing, you are using $result when checking for the number of rows returned but it should be $suppDescription.
This means that your for loop will not run as you will be getting 0 or probbaly FALSE as a response to mysql_num_rows($suppDescription);
$suppDescription=mysql_query($query);
$rows = mysql_num_rows($suppDescription);
$allSupplierInfo=array();
for($i=0; $i<$rows; $i++){
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
Also this is easier done with a while loop, then you just process whatever is returned and dont need to bother getting the number of rows.
$suppDescription=mysql_query($query);
$allSupplierInfo=array();
while ( $rows = mysql_fetch_row($suppDescription) ) {
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.
I have the following nested array:
Array (
[0] => Array ( [0] => [1] => 51.212342,6.7834665 )
[1] => Array ( [0] => 28.8.2013 01:14:06 [1] => 51.2123822,6.7834572 )
[2] => Array ( [0] => 28.8.2013 15:11:53 [1] => 0,0 )
[3] => Array ( [0] => 28.8.2013 15:12:16 [1] => 0,0 )
[4] => Array ( [0] => 28.8.2013 15:36:06 [1] => 0,0 )
[5] => Array ( [0] => 28.8.2013 15:40:13 [1] => 41.117143,16.871871 )
[6] => Array ( [0] => 28.8.2013 15:40:14 [1] => 0,0 )
[7] => Array ( [0] => 28.8.2013 16:03:13 [1] => 0,0 )
[8] => Array ( [0] => 28.8.2013 16:11:19 [1] => 40.8205315914286,16.5500314957143 )
[9] => Array ( [0] => 28.8.2013 16:11:20 [1] => 0,0 )
[10] => Array ( [0] => 28.8.2013 16:11:40 [1] => 40.8205315914286,16.5500314957143 )
[11] => Array ( [0] => 28.8.2013 18:11:33 [1] => 45.4304359,12.3290189 )
[12] => Array ( [0] => 28.8.2013 18:11:34 [1] => 0,0 )
[13] => Array ( [0] => 28.8.2013 18:11:54 [1] => 45.4304456,12.3289609 )
[14] => Array ( [0] => 28.8.2013 18:11:55 [1] => 0,0 )
[15] => Array ( [0] => 29.8.2013 10:07:21 [1] => 51.212394,6.7834843 )
...
);
Here I need to remove all the elements that have "0,0" as their [$n][1] value. I tried this but some of the "0,0" are still there. Why?
for ($i = 0; $i < sizeof($locations); $i++) {
$key = array_search('0,0', $locations[$i]);
if ($key !== false) {
unset($locations[$i]);
$locations = array_values($locations);
}
}
Array (
[0] => Array ( [0] => [1] => 51.212342,6.7834665 )
[1] => Array ( [0] => 28.8.2013 01:14:06 [1] => 51.2123822,6.7834572 )
[2] => Array ( [0] => 28.8.2013 15:12:16 [1] => 0,0 )
[3] => Array ( [0] => 28.8.2013 15:40:13 [1] => 41.117143,16.871871 )
[4] => Array ( [0] => 28.8.2013 16:03:13 [1] => 0,0 )
[5] => Array ( [0] => 28.8.2013 16:11:19 [1] => 40.8205315914286,16.5500314957143 )
[6] => Array ( [0] => 28.8.2013 16:11:40 [1] => 40.8205315914286,16.5500314957143 )
[7] => Array ( [0] => 28.8.2013 18:11:33 [1] => 45.4304359,12.3290189 )
[8] => Array ( [0] => 28.8.2013 18:11:54 [1] => 45.4304456,12.3289609 )
[9] => Array ( [0] => 29.8.2013 10:07:21 [1] => 51.212394,6.7834843 )
[10] => Array ( [0] => 29.8.2013 10:07:56 [1] => 51.2123948,6.7834622 )
[11] => Array ( [0] => 29.8.2013 11:57:45 [1] => 51.21244537,6.78355515 )
[12] => Array ( [0] => 29.8.2013 11:58:27 [1] => 51.21238401,6.78352698 )
[13] => Array ( [0] => 29.8.2013 12:01:17 [1] => 51.2124044633333,6.78353637 )
[14] => Array ( [0] => 29.8.2013 12:11:18 [1] => 51.2124044633333,0.783536 )
[15] => Array ( [0] => 29.8.2013 12:12:39 [1] => 51.212416045,6.783523 )
...
);
One problem with your code is that sizeof($locations) changes every time you do a unset. So whenever you have two consecutive [$n][1] having "0,0", you are not able to detect that. Also your code is not looking at just[$n][1], its looking at all indexes in [$n][]
Use the below code:
$count = count($locations);
for ($i = 0; $i < $count; $i++) {
if ($locations[$i][1] == "0,0") {
unset($locations[$index]);
}
}
$locations = array_values($locations);
foreach ($locations as $index => $row) {
if ($locations[$index][1] == "0,0") unset($locations[$index]);
}
$locations = array_values($locations);