I read an XML file and want to get the values from the output array. This is the code to write the array:
$xml = simplexml_load_file('http://www.xyz.be/meteo');
$json_string = json_encode($xml);
$result_array = json_decode($json_string, TRUE);
The output array has the following structure:
Array
(
[STATION] => Array
(
[0] => Array
(
[#attributes] => Array
(
[ID] => 6407
[NAME] => Kust
)
[DAY] => Array
(
[0] => Array
(
[#attributes] => Array
(
[ID] => 20161110
)
[TMAX] => 10
[WEATHER] => 11
[DD] => NW
[FF] => 25
)
[1] => Array
(
[#attributes] => Array
(
[ID] => 20161111
)
[TMAX] => 8
[WEATHER] => 2
[DD] => ZO
[FF] => 8
)
)
)
[1] => Array
(
[#attributes] => Array
(
[ID] => 6479
[NAME] => Kempen
)
[DAY] => Array
(
[0] => Array
(
[#attributes] => Array
(
[ID] => 20161110
)
[TMAX] => 9
[WEATHER] => 6
[DD] => ZW
[FF] => 11
)
[1] => Array
(
[#attributes] => Array
(
[ID] => 20161111
)
[TMAX] => 5
[WEATHER] => 3
[DD] => NO
[FF] => 6
)
)
)
[2] => Array
(
[#attributes] => Array
(
[ID] => 6476
[NAME] => Ardennen
)
[DAY] => Array
(
...
I reayly tried a lot of things but I don't find the right solution.
** Question **
How can I put it all together in an associative array like this:
Array (
[0] => Array
(
['STAT_INDEX '] => 0
['STAT_ID'] => 6407
['STAT_NAME'] => Kust
['DAY_INDEX'] => 0
['DAY_DATE'] => 20161110
['DAY_TMAX'] => 10
['DAY_WEATHER'] => 11
['DAY_DD '] => NW
['DAY_FF '] => 25
)
[1] => Array
(
['STAT_INDEX '] => 0
['STAT_ID'] => 6407
['STAT_NAME'] => Kust
['DAY_INDEX'] => 1
['DAY_DATE'] => 20161111
['DAY_TMAX'] => 8
['DAY_WEATHER'] => 2
['DAY_DD '] => ZO
['DAY_FF '] => 8
)
[2] => Array
(
['STAT_INDEX '] => 0
['STAT_ID'] => 6451
['STAT_NAME'] => Centrum
['DAY_INDEX'] => 0
['DAY_DATE'] => 20161110
['DAY_TMAX'] => 10
['DAY_WEATHER'] => 11
['DAY_DD '] => W
['DAY_FF '] => 16
)
....
)
I get a lot of errors (notices): Array to string conversion, Undefined offset: 0, Undefined offset: 1...
Does anyone know a solution?
Thanks in advance!
I think I found it.
Maybe not the best code but it works.
$data = array();
$i = 0;
foreach($result_array['STATION'] as $station) {
$dag = 1;
foreach($station['DAY'] as $day) {
$data[$i]['ID'] = $station['#attributes']['ID'];
$data[$i]['NAME'] = $station['#attributes']['NAME'];
$data[$i]['DATUM'] = $day['#attributes']['ID'];
$data[$i]['DAG'] = $dag;
$data[$i]['TMAX'] = $day['TMAX'];
$data[$i]['WEATHER'] = $day['WEATHER'];
$data[$i]['DD'] = $day['DD'];
$data[$i]['FF'] = $day['FF'];
$i += 1;
$dag += 1;
}
}
gives me the result:
Array
(
[0] => Array
(
[ID] => 6407
[NAME] => Kust
[DATUM] => 20161110
[DAG] => 1
[TMAX] => 10
[WEATHER] => 11
[DD] => NW
[FF] => 25
)
[1] => Array
(
[ID] => 6407
[NAME] => Kust
[DATUM] => 20161111
[DAG] => 2
[TMAX] => 8
[WEATHER] => 2
[DD] => ZO
[FF] => 8
)
[2] => Array
(
[ID] => 6451
Related
I need to subtract the qt from two arrays based on id and type, keeping the array complete.
How do I do in php to be able to subtract these arrays?
I have 2 arrays:
=> this is the first array with multiple key "down"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => down
[qt] => 12
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => down
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => down
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => down
[qt] => 45
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => down
[qt] => 3
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
=> this is the second array with multiple key "up"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => up
[qt] => 5
)
[1] => Array
(
[id] => 86
[loc] => 3
[type] => up
[qt] => 27
)
[2] => Array
(
[id] => 23
[loc] => 9
[type] => up
[qt] => 3
)
)
=> I need cubtract "qt" (if "id" and "loc" are the same then subtract the "qt")
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => total
[qt] => 7
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => total
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => total
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => total
[qt] => 18
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => total
[qt] => 0
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
Try this out
function findMatch($array, $id, $loc)
{
foreach ($array as $key => $item) {
if ($item["id"] == $id and $item["loc"] == $loc) {
return $key;
}
}
return false;
}
$total = [];
foreach($down as $d) {
$matched = findMatch($up, $d["id"], $d["loc"]);
if($matched !== false){
$total[] = array_replace($d, [
"type" => "total",
"qt" => ($d["qt"] - $up[$matched]["qt"])
]);
} else {
$total[] = array_replace($d, ["type" => "total"]);
}
}
print_r($total);
This question already has an answer here:
PHP sort associative array by numeric key in asc order [duplicate]
(1 answer)
Closed 10 months ago.
So I have 3 dimensional array. I want that array to be reordered based on the keys but the value of the keys should remain as it is. Like for an example if the array keys are 5,2,4,1,3 then it should become 1,2,3,4,5. Below I'm providing the array I have and excepted array and the solutions I have tried.
This is the array I have :-
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
Following are the solutions I have tried :-
$result = ksort($result);
$result = array_values($result);
$result = array_splice($result, 0, 0);
$result = sort($result);
$result = array_splice($result, 0, count($result));
This is the expected array :-
Array
(
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
)
Nothing is working any help will be appreciated. thanks in advance.
You are using ksort as $result = ksort($result);, ksort return TRUE/FALSE. That means you are assigning that to $results.
Read here PHP ksort
Your code should be:-
ksort($results);
instead of
$result = ksort($result);
You can use ksort for the keys sorting, here is an example
$arr = [
5 => [1,3],
3 => [2,3],
2 => [0,7]
];
ksort($arr);
echo '<pre>';
print_r($arr);
Output
Array
(
[2] => Array
(
[0] => 0
[1] => 7
)
[3] => Array
(
[0] => 2
[1] => 3
)
[5] => Array
(
[0] => 1
[1] => 3
)
)
Hi guys I was wondering how can I add a incremental all elements? Because as of now I am not sure where can I include the "inc" in all elements
Ex:
MY_ARRAY = (
[id] => 4
[children] => Array
(
[0] => Array
(
[id] => 18
[children] => Array
(
[0] => Array
(
[id] => 21
)
[1] => Array
(
[id] => 22
)
)
)
[1] => Array
(
[id] => 19
)
[2] => Array
(
[id] => 20
[children] => Array
(
[0] => Array
(
[id] => 26
)
)
)
)
)
Using these code:
$in = MY_ARRAY
function generateArray($in, $parent = 0){
foreach ($in as $key => $value) {
if(is_numeric($key)){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}else{
$out[$key]=$value;
if($key=="id"){
$out['p_id'] = $parent;
$parent=$value;
}elseif($key=="children"){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}
}
}
return $out;
}
Will give me this output, not including the [inc].
[id] => 4
[P_id] => 0
[inc] => 1
[children] => Array
(
[0] => Array
(
[id] => 18
[P_id] => 4
[inc] => 2
[children] => Array
(
[0] => Array
(
[id] => 21
[P_id] => 18
[inc] => 3
)
[1] => Array
(
[id] => 22
[P_id] => 18
[inc] => 4
)
)
)
[1] => Array
(
[id] => 19
[P_id] => 4
[inc] => 5
)
[2] => Array
(
[id] => 20
[P_id] => 4
[inc] => 6
[children] => Array
(
[0] => Array
(
[id] => 26
[P_id] => 20
[inc] => 7
)
)
)
)
)
Now I'm not sure where and how can I include the [inc] or the incremental value of each element in the array using my code above.
Need really help here guys...
I have two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){
I have an array that looks similar to this,
[4] => Common_Model Object
(
[id] => 4
[name] =>
[date_created] =>
[last_updated] =>
[user_id_updated] =>
[_table] =>
[_aliases] => Array
(
[id] => 4
[name] =>
[date_created] =>
[date_updated] =>
[user_id_updated] =>
[rating] => 3
[recipe_id] => 5
)
[_nonDBAliases] => Array
(
)
[_default] => Array
(
)
[_related] => Array
(
)
[_enums] =>
[_alsoDelete] => Array
(
)
[_readOnly] => Array
(
[0] => date_updated
)
[_valArgs] => Array
(
)
[_valArgsHash] => Array
(
[default] => Array
(
)
)
[_valAliases] => Array
(
)
[_extraData] => Array
(
)
[_inputs] => Array
(
)
[_tableName] => jm_ratings
[_tablePrefix] =>
[_niceDateUpdated] => 1st Jan 70
[_niceDateCreated] => 1st Jan 70
[_fetchAdminData] =>
[_mCache] =>
[_assets] => Array
(
)
)
[3] => Common_Model Object
(
[id] => 3
[name] =>
[date_created] =>
[last_updated] =>
[user_id_updated] =>
[_table] =>
[_aliases] => Array
(
[id] => 3
[name] =>
[date_created] =>
[date_updated] =>
[user_id_updated] =>
[rating] => 1
[recipe_id] => 5
)
[_nonDBAliases] => Array
(
)
[_default] => Array
(
)
[_related] => Array
(
)
[_enums] =>
[_alsoDelete] => Array
(
)
[_readOnly] => Array
(
[0] => date_updated
)
[_valArgs] => Array
(
)
[_valArgsHash] => Array
(
[default] => Array
(
)
)
[_valAliases] => Array
(
)
[_extraData] => Array
(
)
[_inputs] => Array
(
)
[_tableName] => jm_ratings
[_tablePrefix] =>
[_niceDateUpdated] => 1st Jan 70
[_niceDateCreated] => 1st Jan 70
[_fetchAdminData] =>
[_mCache] =>
[_assets] => Array
(
)
)
[2] => Common_Model Object
(
[id] => 2
[name] =>
[date_created] =>
[last_updated] =>
[user_id_updated] =>
[_table] =>
[_aliases] => Array
(
[id] => 2
[name] =>
[date_created] =>
[date_updated] =>
[user_id_updated] =>
[rating] => 1
[recipe_id] => 5
)
[_nonDBAliases] => Array
(
)
[_default] => Array
(
)
[_related] => Array
(
)
[_enums] =>
[_alsoDelete] => Array
(
)
[_readOnly] => Array
(
[0] => date_updated
)
[_valArgs] => Array
(
)
[_valArgsHash] => Array
(
[default] => Array
(
)
)
[_valAliases] => Array
(
)
[_extraData] => Array
(
)
[_inputs] => Array
(
)
[_tableName] => jm_ratings
[_tablePrefix] =>
[_niceDateUpdated] => 1st Jan 70
[_niceDateCreated] => 1st Jan 70
[_fetchAdminData] =>
[_mCache] =>
[_assets] => Array
(
)
)
I wanting to add up the [rating] and get the mean average. But I dont know how do this with PHP, my attempt looks like this,
<?php
foreach ($rt as $rating) {
$total = $rating->rating + $rating->rating
}
$total / count($rt);
?>
Try this:
$total = 0;
foreach($rt as $elem) {
$total += $elem->_aliases['rating'];
}
echo sprintf("Average: %d", $total/count($rt));
<?php
$total = 0;
foreach ($rt as $obj) {
$total += $obj->_aliases['rating'];
}
print $total / count($rt);
?>