I have an array variable $data and I am trying to sorting it alphabetically. I am going through a foreach loop and trying to sort of the key.
The array in the middle is not being sorted to match the other arrays. Yelp should be the last one, instead DealerRater is showing last.
I am trying this:
foreach ($data as $key=>$value) {
ksort($key);
}
My $data Array:
Array
(
[1] => Array
(
[Cars.com] => Array
(
[rooftop_id] => 1
[rooftop_name] => Norm Reeves Honda - Cerritos
[name] => Cars.com
[review_site_id] => 30
[review_count] => 289
[review_average] => 4.80
)
[Dealer Rater] => Array
(
[rooftop_id] => 1
[rooftop_name] => Norm Reeves Honda - Cerritos
[name] => Dealer Rater
[review_site_id] => 10
[review_count] => 1231
[review_average] => 4.90
)
[Google+ Local] => Array
(
[rooftop_id] => 1
[rooftop_name] => Norm Reeves Honda - Cerritos
[name] => Google+ Local
[review_site_id] => 31
[review_count] => 556
[review_average] => 4.80
)
[Yelp] => Array
(
[rooftop_id] => 1
[rooftop_name] => Norm Reeves Honda - Cerritos
[name] => Yelp
[review_site_id] => 29
[review_count] => 423
[review_average] => 3.50
)
)
[45] => Array
(
[Cars.com] => Array
(
[rooftop_id] => 45
[rooftop_name] => Leith Volkswagen of Raleigh
[name] => Cars.com
[review_site_id] => 30
[review_count] => 95
[review_average] => 4.90
)
[Google+ Local] => Array
(
[rooftop_id] => 45
[rooftop_name] => Leith Volkswagen of Raleigh
[name] => Google+ Local
[review_site_id] => 31
[review_count] => 21
[review_average] => 4.80
)
[Yelp] => Array
(
[rooftop_id] => 45
[rooftop_name] => Leith Volkswagen of Raleigh
[name] => Yelp
[review_site_id] => 29
[review_count] => 3
[review_average] => 1.50
)
[Dealer Rater] => Array
(
[rooftop_id] => 45
[rooftop_name] => Leith Volkswagen of Raleigh
[name] => Dealer Rater
[review_site_id] => 10
[review_count] => 0
[review_average] => 0
)
)
[56] => Array
(
[Cars.com] => Array
(
[rooftop_id] => 56
[rooftop_name] => Wilde Jaguar Of Sarasota
[name] => Cars.com
[review_site_id] => 30
[review_count] => 34
[review_average] => 4.70
)
[Dealer Rater] => Array
(
[rooftop_id] => 56
[rooftop_name] => Wilde Jaguar Of Sarasota
[name] => Dealer Rater
[review_site_id] => 10
[review_count] => 271
[review_average] => 4.90
)
[Google+ Local] => Array
(
[rooftop_id] => 56
[rooftop_name] => Wilde Jaguar Of Sarasota
[name] => Google+ Local
[review_site_id] => 31
[review_count] => 31
[review_average] => 4.70
)
[Yelp] => Array
(
[rooftop_id] => 56
[rooftop_name] => Wilde Jaguar Of Sarasota
[name] => Yelp
[review_site_id] => 29
[review_count] => 1
[review_average] => 1.00
)
)
)
You're sorting the wrong value. Do this:
foreach ($data as $key=>$value) {
ksort($value);
}
$key is the index of the main array. $value is the array element you want to sort. It'd make more sense if you named your variables more logically, like this:
foreach ($data as $index=>$element) {
ksort($element);
}
Or this for short:
foreach ($data as $element) {
ksort($element);
}
Related
Below is My Array. I want to make all possible combinations of array whose sum is equal to some number(e.g 20). The super parent array is continent and it's child array is it's countries and countries child array is its respective cities. i want to get all combinations of countries whose cities sum of days_range equal to some number(e.g 20). This is just a single continent array. it may be possible that the array is for other continents are also available.
Array
(
[1] => Array
(
[10] => Array
(
[0] => Array
(
[id] => 18
[city_name] => Baku
[country_id] => 10
[country_name] => Azerbaijan
[days_range] => 5
[continent_id] => 1
)
[2] => Array
(
[id] => 43
[city_name] => Lahıc
[country_id] => 10
[country_name] => Azerbaijan
[days_range] => 5
[continent_id] => 1
)
)
[23] => Array
(
[0] => Array
(
[id] => 42
[city_name] => Vientiane
[country_id] => 23
[country_name] => Laos
[days_range] => 7
[continent_id] => 1
)
[1] => Array
(
[id] => 47
[city_name] => Vang Vieng
[country_id] => 23
[country_name] => Laos
[days_range] => 3
[continent_id] => 1
)
)
[20] => Array
(
[0] => Array
(
[id] => 37
[city_name] => Tamsui District
[country_id] => 20
[country_name] => Taiwan
[days_range] => 1
[continent_id] => 1
)
[1] => Array
(
[id] => 35
[city_name] => Taipei
[country_id] => 20
[country_name] => Taiwan
[days_range] => 3
[continent_id] => 1
)
)
[22] => Array
(
[2] => Array
(
[id] => 46
[city_name] => Maolin District
[country_id] => 20
[country_name] => Taiwan
[days_range] => 3
[continent_id] => 1
)
[3] => Array
(
[id] => 36
[city_name] => Tainan
[country_id] => 20
[country_name] => Taiwan
[days_range] => 3
[continent_id] => 1
)
)
)
)
Below is my expected Output.
Array(
[0]=Array(
[0]=>[10],
[1]=>[23],
)
[1]=Array(
[0]=>[10],
[1]=>[20],
[2]=>[22],
)
[2]=Array(
[0]=>[23],
[1]=>[20],
[2]=>[22],
)
)
I have an array but I want to generate other array from that one..
Array
(
[0] => Array
(
[supplier] => Billy
[total_bookings] => 5
[year] => 2016
[month] => 6
[user_id] => 4
[sales_revenue] => 1180
[net_revenue] => 1180
)
[1] => Array
(
[supplier] => XYZ1
[total_bookings] => 3
[year] => 2016
[month] => 6
[user_id] => 2
[sales_revenue] => 642
[net_revenue] => 642
)
[2] => Array
(
[supplier] => Billy
[total_bookings] => 1
[year] => 2016
[month] => 3
[user_id] => 4
[sales_revenue] => 30
[net_revenue] => 30
)
[3] => Array
(
[supplier] => Billy
[total_bookings] => 1
[year] => 2015
[month] => 10
[user_id] => 4
[sales_revenue] => 30
[net_revenue] => 30
)
)
to new array :
Array
(
[2016] => Array(
[6] => Array
(
[0] => Array(
[supplier] => Billy
[total_bookings] => 5
[user_id] => 4
[sales_revenue] => 1180
[net_revenue] => 1180
)
[1] => Array
(
[supplier] => XYZ1
[total_bookings] => 3
[user_id] => 2
[sales_revenue] => 642
[net_revenue] => 642
)
)
[3] => Array
(
[0] => Array
(
[supplier] => Billy
[total_bookings] => 1
[year] => 2016
[month] => 3
[user_id] => 4
[sales_revenue] => 30
[net_revenue] => 30
)
)
)
[2015] => Array(
[10] => Array
(
[supplier] => Billy
[total_bookings] => 1
[user_id] => 4
[sales_revenue] => 30
[net_revenue] => 30
)
)
)
The solution using array_fill_keys, array_column(available since PHP 5.5), array_walk and array_diff_key functions:
// supposing $arr is your initial array
$years = array_fill_keys(array_column($arr, 'year'), []);
array_walk($arr, function($v) use(&$years){
if (key_exists($v['year'], $years)) {
$years[$v['year']][$v['month']][] = array_diff_key($v, ['year'=>0, 'month'=>0]);
}
});
print_r($years);
Try this one and let me know:
$new_arr = array();
foreach($arr as $val){
$temp = array('supplier' => $val['supplier'], 'total_bookings' => $val['total_bookings'], 'user_id' => $val['user_id'], 'sales_revenue' => $val['sales_revenue'], 'net_revenue' => $val['net_revenue']);
array_push($new_arr[$val['year']][$val['month']], $temp);
}
print_r($new_arr);
Try this one:-
$res = [];
foreach($array as $record){
$year = $record['year'];
$month = $record['month'];
unset($record['year'],$record['month']);
$res[$year][$month][] = $record;
}
echo '<pre>'; print_r($res);
[traffic] => Array
(
[0] => Array
(
[id] => 1
[visitors] => 310
[pageviews] => 1333
[created_date] => 2016-03-09
)
[1] => Array
(
[id] => 2
[visitors] => 374
[pageviews] => 1010
[created_date] => 2016-03-10
)
[2] => Array
(
[id] => 3
[visitors] => 143
[pageviews] => 617
[created_date] => 2016-03-11
)
)
[source] => Array
(
[0] => Array
(
[created_date] => 2016-03-09
[scount] => 368
)
[1] => Array
(
[created_date] => 2016-03-10
[scount] => 550
)
[2] => Array
(
[created_date] => 2016-03-11
[scount] => 238
)
)
I have two multidimensional arrays, I want to combine both arrays into one with matching created_date value, the result should be like this,
Array
(
[0] => Array
(
[created_date] => 2016-03-09
[id] => 1
[visitors] => 310
[pageviews] => 1333
[scount] => 368
)
[1] => Array
(
[created_date] => 2016-03-10
[id] => 2
[visitors] => 374
[pageviews] => 1010
[scount] => 550
)
[2] => Array
(
[created_date] => 2016-03-11
[id] => 3
[visitors] => 143
[pageviews] => 617
[scount] => 238
)
)
$traffic = []; //...
$source = []; // ...
foreach($traffic as $key => $value)
{
if(isset($source[$key]))
{
$token = $source[$key];
foreach($token as $keyy => $valuee)
{
if(isset($traffic[$key][$keyy]))
{
// Collision handling, if any ...
$traffic[$key][$keyy] = $valuee;
}
else $traffic[$key][$keyy] = $valuee;
}
}
}
The following code should do the trick.
The solution:
# I split your array into 2 parts ($traffic = $your_array['traffic'])
$traffic = array(
array(
id => 1,
visitors => 310,
pageviews => 1333,
created_date => '2016-03-09'
),
array(
id => 2,
visitors => 374,
pageviews => 1010,
created_date => '2016-03-10'
),
array(
id => 3,
visitors => 143,
pageviews => 617,
created_date => '2016-03-11'
)
);
# I split your array into 2 parts ($source = $your_array['source'])
$source = array(
array (
created_date => '2016-03-09',
scount => 368
),
array (
created_date => '2016-03-10',
scount => 550
),
array (
created_date => '2016-03-11',
scount => 238
)
);
# copy the traffic array cause we want to merge the new data into it
$result = $traffic;
# loop over the traffic array
foreach ($traffic as $k => $t) {
# loop over the source
foreach ($source as $s) {
# try to find a match
if ($t['created_date'] === $s['created_date']) {
# add data to result
$result[$k]['scount'] = $s['scount'];
# we exit the inner foreach-loop here as there is only 1 match
break;
}
}
}
# print the result
echo '<pre>'; print_r($result); echo '</pre>';
The result:
Array
(
[0] => Array
(
[id] => 1
[visitors] => 310
[pageviews] => 1333
[created_date] => 2016-03-09
[scount] => 368
)
[1] => Array
(
[id] => 2
[visitors] => 374
[pageviews] => 1010
[created_date] => 2016-03-10
[scount] => 550
)
[2] => Array
(
[id] => 3
[visitors] => 143
[pageviews] => 617
[created_date] => 2016-03-11
[scount] => 238
)
)
you can test it here: http://www.writephponline.com
Here is your solution:-
$arr1 = $arr1['traffic']; // assign key traffic record to array1
$arr2 = $arr2['source']; // assign key source record to array2
$result = [];
foreach($arr1 as $key=>$value){
$result[$key] = $value;
// find created_date in second array
$keyOfSecondArr = array_search($value['created_date'], array_column($arr2, 'created_date'));
$result[$key]['scount'] = $arr2[$keyOfSecondArr]['scount'];
}
echo '<pre>'; print_r($result);
output:-
Array
(
[0] => Array
(
[id] => 1
[visitors] => 310
[pageviews] => 1333
[created_date] => 2016-03-09
[scount] => 368
)
[1] => Array
(
[id] => 2
[visitors] => 374
[pageviews] => 1010
[created_date] => 2016-03-10
[scount] => 550
)
[2] => Array
(
[id] => 3
[visitors] => 143
[pageviews] => 617
[created_date] => 2016-03-11
[scount] => 238
)
)
I am completely new to programming and was tasked with changing the structure of an array, I just can't get it to work.
This is the original array:
[0] => Array
(
[att_values_id] => 5
[att_value] => Sloping
[att_id] => 5
[att_category] => Frame
)
[1] => Array
(
[att_values_id] => 13
[att_value] => Time Trial
[att_id] => 5
[att_category] => Frame
)
[2] => Array
(
[att_values_id] => 21
[att_value] => Mountain
[att_id] => 5
[att_category] => Frame
)
[3] => Array
(
[att_values_id] => 15
[att_value] => Carbon
[att_id] => 3
[att_category] => Material
)
[4] => Array
(
[att_values_id] => 15
[att_value] => Titanium
[att_id] => 9
[att_category] => Frame
)
[5] => Array
(
[att_values_id] => 15
[att_value] => Aluminum
[att_id] => 17
[att_category] => Frame
)
[6] => Array
(
[att_values_id] => 7
[att_value] => Expensive
[att_id] => 12
[att_category] => Price
)
[7] => Array
(
[att_values_id] => 7
[att_value] => Moderate
[att_id] => 33
[att_category] => Price
)
[8] => Array
(
[att_values_id] => 7
[att_value] => Entry Level
[att_id] => 40
[att_category] => Price
)
And I have to change it to this:
Array
(
[Frame] => Array
(
[5] => Sloping
[13] => Mountain
[21] => Time Trial
)
[Material] => Array
(
[3] => Carbon
[9] => Titanium
[17] => Aluminum
)
[Price] => Array
(
[12] => Expensive
[33] => Moderate
[40] => Entry Level
)
)
I have tried using foreach to go through the array but I don't know how to build the new array.
this little pseudocode is the closest we can get before plainly doing your homework.
foreach (firstarray as key => value) {
newarray[value['att_category']][value['att_id']]=value['att_value'];
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP remove duplicate values from multidimensional array
i have an array like:
Array
(
[prom] => Array
(
[cab] => Array
(
[0] => Array
(
[code] => 01
[price1] => 1000
[price2] => 2000
[available] => 2
[max] => 2
[gca] => 2
)
[1] => Array
(
[code] => 04
[price1] => 870
[price2] => 2500
[available] => 3
[max] => 4
[gca] => 10
)
[2] => Array
(
[code] => 01
[price1] => 1000
[price2] => 2000
[available] => 2
[max] => 2
[gca] => 2
)
[3] => Array
(
[code] => 05
[price1] => 346
[price2] => 1022
[available] => 10
[max] => 2
[gca] => 20
)
)
[cab1] => Array........
)
[prom1] = Array....
)
What i have to do is to remove duplicates inside every [cab*] array..
so to have something like:
Array
(
[prom] => Array
(
[cab] => Array
(
[0] => Array
(
[code] => 01
[price1] => 1000
[price2] => 2000
[available] => 2
[max] => 2
[gca] => 2
)
[1] => Array
(
[code] => 04
[price1] => 870
[price2] => 2500
[available] => 3
[max] => 4
[gca] => 10
)
[2] => Array
(
[code] => 05
[price1] => 346
[price2] => 1022
[available] => 10
[max] => 2
[gca] => 20
)
)
[cab1] => Array........
)
[prom1] = Array....
)
In know that there is array_unique combined with array_map to remove duplicates.. but i know that it works only on 2D array.. what can i do? can someone help me pls? thanks!!!
Try it:-
function super_unique($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = super_unique($value);
}
}
return $result;
}
$input = super_unique($myarray);
echo "<pre>";
print_r($input);