how to combine 2 array values into single one by common value - php

1.fetching the values from a same table with different date
<?php
$date= date("Y-m-d");;
$prev_date = date('Y-m-d', strtotime($date .' -1 day'));
$result_yest=mysql_query("SELECT isp,sum(sent) AS `sent_yest` FROM red_global WHERE status_date='$prev_date' and sent > 0 group by 1",$link1);
$data_yest = array(); // create a variable to hold the information
while (($row_yest = mysql_fetch_array($result_yest, MYSQL_ASSOC)) !== false){
$data_yest[] = $row_yest; // add the row in to the results (data) array
}
print_r($data_yest);
$result_today=mysql_query("SELECT isp,sum(sent) AS `sent_today` FROM red_global WHERE status_date='$date' and sent > 0 group by 1",$link1);
$data_today = array(); // create a variable to hold the information
while (($row_today = mysql_fetch_array($result_today, MYSQL_ASSOC)) !== false){
$data_today[] = $row_today; // add the row in to the results (data) array
}
print_r($data_today);
exit;
?>
2.Array results in which one array contains yesterday's sent count and another one aray contains today's sent count both array having the common isp
Array (
[0] => Array ( [isp] => aol [sent_yest] => 46838 )
[1] => Array ( [isp] => gmail [sent_yest] => 33015 )
[2] => Array ( [isp] => juno [sent_yest] => 93544 )
[3] => Array ( [isp] => roadrunner [sent_yest] => 6181 )
[4] => Array ( [isp] => yahoo [sent_yest] => 71444 )
)
Array (
[0] => Array ( [isp] => aol [sent_today] => 14135 )
[1] => Array ( [isp] => att [sent_today] => 624 )
[2] => Array ( [isp] => gmail [sent_today] => 21263 )
[3] => Array ( [isp] => juno [sent_today] => 74934 )
[4] => Array ( [isp] => roadrunner [sent_today] => 939 )
[5] => Array ( [isp] => yahoo [sent_today] => 22059 )
)
Now i need a result like this isp name, yesterday's sent count, today's sent count
[isp, sent_yest, sent_today],
[aol, 46838, 14135],
[att, 0, 624],
[gmail, 33015, 21263],
Anyone help me to solve this.. Thanks in advance

Do it like below:-
$final_array = array();
if(count($arr1)>=count($arr2)){
foreach ($arr1 as $arr){
$key='';
foreach ($arr2 as $ar2){
if($arr['isp'] == $ar2['isp']){
$final_array[$arr['isp']] = array($arr['isp'],$arr['sent_yest'],$ar2['sent_today']);
$key ='';break;
}else{
$key = $arr['isp'];
}
}
if($key!==''){
$final_array[$key] = array($arr['isp'],$arr['sent_yest'],0);
$key ='';
}
}
}
if(count($arr2)>count($arr1)){
foreach ($arr2 as $ar2){
$key='';
foreach ($arr1 as $arr){
if($ar2['isp'] == $arr['isp']){
$final_array[$ar2['isp']] = array($ar2['isp'],$arr['sent_yest'],$ar2['sent_today']);
$key ='';break;
}else{
$key = $ar2['isp'];
}
}
if($key!==''){
$final_array[$key] = array($ar2['isp'],0,$ar2['sent_today']);
$key ='';
}
}
}
echo "<pre/>";print_r($final_array);
Output:-https://eval.in/814625

I would avoid foreach if it's possible, since it leads to code that's much harder to understand. With array operation it can look like this:
// First we merge the two arrays
$merged = array_merge($data_yest, $data_today);
// Then format it
$final = array_reduce($merged, function($final, $item) {
// Initialize for each isp if it's not present
// This way we avoid overwriting previous data and have a default value 0
if (! isset($final[ $item['isp'] ])) {
$final[ $item['isp'] ] = [
'sent_yest' => 0,
'sent_today' => 0,
];
}
// And if one of the days is present, we add it
if (isset($item['sent_yest'])) {
$final[ $item['isp'] ][ 'sent_yest' ] = $item['sent_yest'];
}
if (isset($item['sent_today'])) {
$final[ $item['isp'] ][ 'sent_today' ] = $item['sent_today'];
}
// Then return the modified array
return $final;
}, []);
print_r($final);
exit;
Result looks like this:
Array
(
[aol] => Array
(
[sent_yest] => 46838
[sent_today] => 14135
)
[gmail] => Array
(
[sent_yest] => 33015
[sent_today] => 21263
)
[juno] => Array
(
[sent_yest] => 93544
[sent_today] => 74934
)
[roadrunner] => Array
(
[sent_yest] => 6181
[sent_today] => 939
)
[yahoo] => Array
(
[sent_yest] => 71444
[sent_today] => 22059
)
[att] => Array
(
[sent_yest] => 0
[sent_today] => 624
)
)

Related

looping through multi dimension array

I am parsing A JSON file in PHP using PHP Decode
$json= file_get_contents ("resultate.json");
$json = json_decode($json, true);
and then I am trying to loop through the results and display them
foreach($json as $zh){
$name = $zh[0]["VORLAGEN"][0]["JA_PROZENT"];
$JA = $zh[0]["VORLAGEN"][0]["VORLAGE_NAME"];
$kreis = $zh[0]["NAME"];
$kreis1 = $zh[22]["NAME"];
echo $name;
echo $JA;
echo $kreis;
echo $kreis1;
...}
but I receive only one element e.g. position 0 or position 22. I would like to receive all the results and display them in a list. Below you can see the array
Array
(
[GEBIETE] => Array
(
[0] => Array
(
[VORLAGEN] => Array
(
[0] => Array
(
[VORLAGE_NAME] => Kantonale Volksabstimmung über die Vorlage Steuergesetz (StG) (Änderung vom 1. April 2019; Steuervorlage 17)
[JA_STIMMEN_ABSOLUT] => 205
[STIMMBETEILIGUNG] => 28.11
[NEIN_STIMMEN_ABSOLUT] => 183
[VORLAGE_ID] => 2491
[JA_PROZENT] => 52.84
)
)
[NAME] => Aeugst a.A.
[WAHLKREIS] => 0
[BFS] => 1
)
[1] => Array
(
[VORLAGEN] => Array
(
[0] => Array
(
[VORLAGE_NAME] => Kantonale Volksabstimmung über die Vorlage Steuergesetz (StG) (Änderung vom 1. April 2019; Steuervorlage 17)
[JA_STIMMEN_ABSOLUT] => 1000
[STIMMBETEILIGUNG] => 26.15
[NEIN_STIMMEN_ABSOLUT] => 851
[VORLAGE_ID] => 2491
[JA_PROZENT] => 54.02
)
)
[NAME] => Affoltern a.A.
[WAHLKREIS] => 0
[BFS] => 2
)
[2] => Array
(
[VORLAGEN] => Array
(
[0] => Array
(
[VORLAGE_NAME] => Kantonale Volksabstimmung über die Vorlage Steuergesetz (StG) (Änderung vom 1. April 2019; Steuervorlage 17)
[JA_STIMMEN_ABSOLUT] => 661
[STIMMBETEILIGUNG] => 30.98
[NEIN_STIMMEN_ABSOLUT] => 454
[VORLAGE_ID] => 2491
[JA_PROZENT] => 59.28
)
)
[NAME] => Bonstetten
[WAHLKREIS] => 0
[BFS] => 3
)
can you please tell me how to print all the elements of this array?
There is GEBIETE layer in your json, so change foreach($json as $zh) to foreach($json["GEBIETE"] as $zh) will works.
You can nest multiple foreach:
$root = $json['GEBIETE'];
foreach ($root as $elem) {
foreach ($elem as $key => $value) {
// VORLAGEN is an array of its own so if you want to print the keys you should foreach this value as well
if(is_array($value)) {
foreach ($value[0] as $k => $v) {
echo $k."\t".$v;
}
}
else {
echo $value;
}
}
}

Extract values only from an array to encode them in JSON using PHP

This has probably been asked many times but I can't find a solution for my case.
This is my array :
$request=Array (
[0] => Array ( [staName] => Auditorium Stravinsky 2m2c )
[1] => Array ( [staName] => Geneva Arena )
[2] => Array ( [staName] => Les Docks )
[3] => Array ( [staName] => Kheops )
)
And i need an output as follows as JSON:
"Auditorium Stravinsky 2m2c ","Geneva Arena","Les Docks","Kheops"
My current code is as follows:
foreach($request as $value)
{
$names[]=$value;
}
$jsonValue = json_encode(array_values($names));
print_r($jsonValue);
And my current output is as follows in JSON format:
[{"staName":"Auditorium Stravinsky 2m2c "},{"staName":"Geneva Arena"},{"staName":"Les Docks"},{"staName":"Kheops"}]
How can i stop "staName " from being outputed?
Many thanks in advance and please be considerate of my post as this is only the second one I make on this site.
<?php
$request=Array (
0 => Array ( 'staName' => 'Auditorium Stravinsky 2m2c' ) ,
1 => Array ( 'staName' => 'Geneva Arena' ) ,
2 => Array ( 'staName' => 'Les Docks' ) ,
3 => Array ( 'staName' => 'Kheops' )
);
$newArray=array();
for($i=0;$i<count($request);$i++){
$newArray[$i]=$request[$i]['staName'];
}
$newArray=json_encode($newArray,true);
print_r($newArray);
And the output is a merged json:
["Auditorium Stravinsky 2m2c","Geneva Arena","Les Docks","Kheops"]
You can achieve this from,
Code
$a = array();
foreach($request as $key =>$val){
foreach($val as $k => $v){
$a[] = $v;
}
}
print_r(json_encode($a));
Check this Demo link
Output
["Auditorium Stravinsky 2m2c","Geneva Arena","Les Docks","Kheops"]
fist of all your array definition are not correct. and your output is simple string not a JSON
<?php
$request=Array (
0 => Array ( 'staName' => 'Auditorium Stravinsky 2m2c' ),
1 => Array ( 'staName' => 'Geneva Arena' ) ,
2 => Array ( 'staName' => 'Les Docks' ) ,
3 => Array ( 'staName' => 'Kheops' )
);
$name = '';
foreach($request as $value)
{
foreach($value as $value2)
{
$name = $name . ' ' . $value2;
}
}
echo $name;
Output
Auditorium Stravinsky 2m2c Geneva Arena Les Docks Kheops

group php array by subarray value

I want to group an array by a subarray's value. If I have an array like this:
Array
(
[0] => Array
(
[userID] => 591407753
[propertyA] => 'text1'
[propertyB] => 205
)
[1] => Array
(
[userID] => 989201004
[propertyA] =>'text2'
[propertyB] => 1407
)
[2] => Array
(
[userID] => 989201004
[propertyA] => 'text3'
[propertyB] => 1407
)
)
I want to sort to group this array by a subarray's value so I can have an array like this:
Array
(
[0]=>Array
(
[userID]=>59140775
[properties]=>Array
(
[0]=>text1
)
[propertyB]=>205
)
[1]=>Array
(
[userID]=>989201004
[properties]=>Array
(
[0]=>'text2'
[1]=>'text3'
)
[propertyB]=>1047
)
)
How can I make this?
Before I had tried this:
$result = array();
foreach ($userArray as $record)
{
$id=$record['userID'];
if(isset($result[$id]))
{
$result[$id]['propertyA'][]=array($record['propertyA']);
}
else {
$record["propertyA"]=array($record['propertyA']);
unset($record['tweet']);
$result[$id]=$record;
}
}
the problem was for the propertyA. I was an the result an additional property propertyA with the table like this:
Array
(
[0]=>Array (
[userID]=>989201004
[propertyA]=>'text2'
[properties]=>Array(
[0]=>'text2'
[1]=>'text3'
)
)
)
The following code should do the job. I hope it is self-explanatory:
$result = array();
foreach ($array as $record) {
if (!isset($result[$record['userID']])) {
$result[$record['userID']] = array(
'userID' => $record['userID'],
'properties' => array($record['propertyA']),
'propertyB' => $record['propertyB'],
);
}
else {
$result[$record['userID']]['properties'][] = $record['propertyA'];
}
}
$result = array_values($result);

Grouping multidimensional array by two values

I have arrays like this.
[11] => Array
(
[transactioncurrencyid] => Array
(
[!name] => US Dollar
[!] => {041E3DC9-D938-DD11-982C-0013724C58B7}
)
[smi_processingmonth] => Array
(
[!date] => 6/1/2011
[!time] => 2:27 PM
[!] => 2011-06-01T14:27:00-07:00
)
[smi_cchistoryid] => {678C5036-9EAA-E111-88E0-00155D010302}
[smi_includeindeal] => Array
(
[!name] => No
[!] => 0
)
[smi_locationid] => Array
(
[!name] => 1134 Hooksett Rd
[!] => {5CC1585B-91AA-E111-88E0-00155D010302}
)
)
[12] => Array
(
[transactioncurrencyid] => Array
(
[!name] => US Dollar
[!] => {041E3DC9-D938-DD11-982C-0013724C58B7}
)
[smi_processingmonth] => Array
(
[!date] => 5/1/2011
[!time] => 2:27 PM
[!] => 2011-05-01T14:27:00-07:00
)
[smi_cchistoryid] => {688C5036-9EAA-E111-88E0-00155D010302}
[smi_includeindeal] => Array
(
[!name] => No
[!] => 0
)
[smi_locationid] => Array
(
[!name] => 1134 Hooksett Rd
[!] => {5CC1585B-91AA-E111-88E0-00155D010302}
)
)
How can i group them by location id then by smi_processingmonth
So I get something like this
[1134 Hooksett Rd] => Array
(
[ 5/1/2011] = array(
[transactioncurrencyid] => Array
(
[!name] => US Dollar
[!] => {041E3DC9-D938-DD11-982C-0013724C58B7}
)
[smi_processingmonth] => Array
(
[!date] => 5/1/2011
[!time] => 2:27 PM
[!] => 2011-05-01T14:27:00-07:00
)
[smi_cchistoryid] => {688C5036-9EAA-E111-88E0-00155D010302}
[smi_includeindeal] => Array
(
[!name] => No
[!] => 0
)
[smi_locationid] => Array
(
[!name] => 1134 Hooksett Rd
[!] => {5CC1585B-91AA-E111-88E0-00155D010302}
)
)
[1/1/2011] = array(
[transactioncurrencyid] => Array
(
[!name] => US Dollar
[!] => {041E3DC9-D938-DD11-982C-0013724C58B7}
)
[smi_processingmonth] => Array
(
[!date] => 6/1/2011
[!time] => 2:27 PM
[!] => 2011-06-01T14:27:00-07:00
)
[smi_cchistoryid] => {678C5036-9EAA-E111-88E0-00155D010302}
[smi_includeindeal] => Array
(
[!name] => No
[!] => 0
)
[smi_locationid] => Array
(
[!name] => 1134 Hooksett Rd
[!] => {5CC1585B-91AA-E111-88E0-00155D010302}
)
)
)
I have tried
foreach($array as $keys)
{
$key = $keys['smi_processingmonth']['!date'];;
if (!isset($groups[$key]))
{
$groups[$key] = array($keys);
} else {
$groups[$key][] = $keys;
}
}
$newGroups = array();
if(is_array($groups))
{
foreach($groups as $cats => $values)
{
foreach($values as $itemValues){
$st = rtrim(trim($itemValues['smi_locationid']['!']));
$key = $st;
if (!isset($newGroups[$key]))
{
$newGroups[$key] = array($groups);
} else {
$newGroups[$key][] = $itemValues;
}
}
}
}
Thanks!
Taking from Johan with a few modifications:
avoid ltrim if already using trim
put in a function, I can use temporary variables to maintain readability
add a final [], to avoid nasty $name/$date collision, out of original request, but safer
function compactArray($data)
{
$new_structure = array();
foreach ( $data as $row )
{
$name = trim($row['smi_locationid']['!name']);
$date = trim($row['smi_processingmonth']['!date']);
$new_structure[ $name ][ $date ][] = $row;
}
return $new_structure;
}
Ehm, i have not tested it. But isn't it just:
$new_structure = array();
foreach ( $data as $row ) {
$key1 = rtrim(trim($row['smi_locationid']['!name']));
$key2 = rtrim(trim($row['smi_processingmonth']['!date']));
$new_structure[$key1][$key2] = $row;
}
This code could use some isset()s, but i decided to keep them out for clarity.
The following produces your desired output array, correctly sorted:
function transaction_datecmp($tran1, $tran2)
{
return strtotime($tran1['smi_processingmonth']['!']) -
strtotime($tran2['smi_processingmonth']['!']);
}
$output_arr = array();
foreach ($input_arr as $transaction) {
$location = $transaction['smi_locationid'] ['!name'];
$date = $transaction['smi_processingmonth']['!date'];
$output_arr[$location][$date] = $transaction;
}
ksort($output_arr); // sorts by location
foreach ($output_arr as &$transaction_arr) {
uasort($transaction_arr, 'transaction_datecmp');
}
Your data structure relies on the assumption that there cannot be two transactions on the same day, which is a somewhat dangerous assumption. Also using a location as a key is far from ideal (because of spelling, location changes, etc.) - unless it really should group things, like for paper mailing.
Data cleanup, like trimming strings, should be done beforehand, ideally at data input time.

Grouping items in assoc. array

I have an associative array with the following contents:
Array
(
[L_TIMESTAMP0] => 2011%2d10%2d16T16%3a57%3a38Z
[L_TIMESTAMP1] => 2011%2d10%2d16T16%3a45%3a23Z
[L_TIMESTAMP2] => 2011%2d10%2d16T16%3a35%3a54Z
[L_TIMEZONE0] => GMT
[L_TIMEZONE1] => GMT
[L_TIMEZONE2] => GMT
[L_STATUS0] => Completed
[L_STATUS1] => Completed
[L_STATUS2] => Completed
[TIMESTAMP] => 2011%2d10%2d16T17%3a58%3a39Z
)
What I want to do is to 'group' all instances of L_TIMESTAMP0, L_TIMEZONE0 and L_STATUS0 (and L_TIMESTAMP1, L_TIMEZONE1, L_STATUS1) into their own array, or as part of a multidimensional array. But I simply can't seem to figure out how I would go about accomplishing this.
The expected results would be:
Array
(
[L_TIMESTAMP0] => 2011%2d10%2d16T16%3a57%3a38Z
[L_TIMEZONE0] => GMT
[L_STATUS0] => Completed
)
Array
(
[L_TIMESTAMP1] => 2011%2d10%2d16T16%3a45%3a23Z
[L_TIMEZONE1] => GMT
[L_STATUS1] => Completed
)
I figured this would be simple with a simple for-loop, but that doesn't give me the results I'm looking for.
Any ideas?
It's likely something very obvious, but I've spent the last few days with this and I can't figure it out (although that probably says more about my skills than the problem)
edit:
To add; I don't need the exact code for my particular question, but any pointers as to what direction I should look into would be greatly appreciated.
<?php
$groupRegex = '/([0-9]+)$/';
$multiArray = array();
$assocArray = array (
"L_TIMESTAMP0" => "2011%2d10%2d16T16%3a57%3a38Z",
"L_TIMESTAMP1" => "2011%2d10%2d16T16%3a45%3a23Z",
"L_TIMESTAMP2" => "2011%2d10%2d16T16%3a35%3a54Z",
"L_TIMEZONE0" => "GMT",
"L_TIMEZONE1" => "GMT",
"L_TIMEZONE2" => "GMT",
"L_STATUS0" => "Completed",
"L_STATUS1" => "Completed",
"L_STATUS2" => "Completed",
"TIMESTAMP" => "2011%2d10%2d16T17%3a58%3a39Z"
);
foreach( $assocArray as $key => $value ) {
preg_match( $groupRegex, $key, $group );
if( !isset($group[1]) ){
continue;
}
$group = intval( $group[1] );
if( is_array( $multiArray[$group] ) ) {
$multiArray[$group][$key] = $value;
}
else {
$multiArray[$group] = array( $key => $value );
}
}
echo "<pre>";
print_r( $multiArray);
echo "</pre>";
?>
result
Array
(
[0] => Array
(
[L_TIMESTAMP0] => 2011%2d10%2d16T16%3a57%3a38Z
[L_TIMEZONE0] => GMT
[L_STATUS0] => Completed
)
[1] => Array
(
[L_TIMESTAMP1] => 2011%2d10%2d16T16%3a45%3a23Z
[L_TIMEZONE1] => GMT
[L_STATUS1] => Completed
)
[2] => Array
(
[L_TIMESTAMP2] => 2011%2d10%2d16T16%3a35%3a54Z
[L_TIMEZONE2] => GMT
[L_STATUS2] => Completed
)
)
$restructuring = true;
$count = 0;
$new_array = array();
while($restructuring)
{
$restructuring = false;
foreach($original_array as $oa)
{
if(!array_key_exists($count, $new_array) && array_key_exists('L_TIMESTAMP'.$count, $original_array))
{
$new_element = array();
foreach(array('TIMESTAMP', 'TIMEZONE', 'STATUS') as $e)
if(!empty($original_array['L_'.$e.$count])) $new_element[$e.$count] = $original_array['L_'.$e.$count];
$new_array[] = $new_element;
$count++;
$restructuring = true;
}
}
}
Given $original_array as defined in the question, the $new_array will be:
Array
(
[0] => Array
(
[TIMESTAMP0] => 2011%2d10%2d16T16%3a57%3a38Z
[TIMEZONE0] => GMT
[STATUS0] => Completed
)
[1] => Array
(
[TIMESTAMP1] => 2011%2d10%2d16T16%3a45%3a23Z
[TIMEZONE1] => GMT
[STATUS1] => Completed
)
[2] => Array
(
[TIMESTAMP2] => 2011%2d10%2d16T16%3a35%3a54Z
[TIMEZONE2] => GMT
[STATUS2] => Completed
)
)

Categories