PHP/JSON array sorting by highest number - Riot API - php

I am currently playing with Riot Games API and using one of the wrappers developed by the community (https://github.com/kevinohashi/php-riot-api). My issue is, I am trying to sort the results using arsort
My code example:
<?php
include('php-riot-api.php');
$summoner_name = 'fallingmoon';
$summoner_id = 24381045;
$test = new riotapi('euw');
$r = $test->getLeague($summoner_id);
?>
<?php
$array = json_decode($r, true);
foreach($array AS $key => $newArray) {
$tempArray[$key] = $newArray['entries'][0]['leaguePoints'];
}
arsort($tempArray);
$finalArray = array();
foreach($tempArray AS $key => $value) {
$finalArray[] = $array[$key];
}
?>
My goal is sort the array by league points (Highest to lowest), but the output of the array once I print it is as followed, as you can see it hasn't sorted. I am probably missing something very minor but any help will be greatly appreciated.
Array
(
[0] => Array
(
[name] => Rengar's Demolishers
[tier] => GOLD
[queue] => RANKED_SOLO_5x5
[entries] => Array
(
[0] => Array
(
[playerOrTeamId] => 33372844
[playerOrTeamName] => L3tsPl4yLoL
[leagueName] => Rengar's Demolishers
[queueType] => RANKED_SOLO_5x5
[tier] => GOLD
[rank] => V
[leaguePoints] => 0
[wins] => 34
[isHotStreak] => 1
[isVeteran] =>
[isFreshBlood] =>
[isInactive] =>
[lastPlayed] => -1
)
[1] => Array
(
[playerOrTeamId] => 19397582
[playerOrTeamName] => Lunchi
[leagueName] => Rengar's Demolishers
[queueType] => RANKED_SOLO_5x5
[tier] => GOLD
[rank] => IV
[leaguePoints] => 10
[wins] => 7
[isHotStreak] =>
[isVeteran] =>
[isFreshBlood] =>
[isInactive] =>
[lastPlayed] => -1
)
[2] => Array
(
[playerOrTeamId] => 24613501
[playerOrTeamName] => RadiantBurst
[leagueName] => Rengar's Demolishers
[queueType] => RANKED_SOLO_5x5
[tier] => GOLD
[rank] => II
[leaguePoints] => 42
[wins] => 48
[isHotStreak] =>
[isVeteran] =>
[isFreshBlood] =>
[isInactive] =>
[lastPlayed] => -1
)
[3] => Array
(
[playerOrTeamId] => 19939979
[playerOrTeamName] => vinter
[leagueName] => Rengar's Demolishers
[queueType] => RANKED_SOLO_5x5
[tier] => GOLD
[rank] => I
[leaguePoints] => 38
[wins] => 57
[isHotStreak] =>
[isVeteran] =>
[isFreshBlood] =>
[isInactive] =>
[lastPlayed] => -1
)

The issue is that $array is an array with one array in it.
Presumably you want to sort the entries array in which case you can tweak your code:
foreach($array[0]['entries'] AS $key => $team) {
$tempArray[$key] = $team['leaguePoints'];
}
arsort($tempArray);
$finalArray = array();
foreach($tempArray AS $key => $value) {
$finalArray[] = $array[0]['entries'][$key];
}
Note that the above doesn't support multiple leagues.
However I find using usort to be more readable:
foreach($array as $key => $league){
usort($array[$key]['entries'], function($a,$b){
return $a['leaguePoints'] - $b['leaguePoints'];
});
}

Related

How to group subarray data using multiple column values and individually sum the remaining column values?

How can I group by Source and date then SUM Pageviews and revenue this array below
Array
(
[0] => Array
(
[Source] => Analytics
[Date] => 2017-10-31
[Source] => Google
[visits] => 6000
[Pageviews] => 12,214
[Revenue] => 25
)
[1] => Array
(
[Source] => Analytics
[Date] => 2017-10-31
[Source] => Google
[visits] => 600
[Pageviews] => 1015
[Revenue] => 10
)
[2] => Array
(
[Source] => Analytics
[Date] => 2017-10-31
[Source] => Facebook
[visits] => 600
[Pageviews] => 1144
[Revenue] => 40
)
[3] => Array
(
[Source] => Analytics
[Date] => 2017-10-30
[Source] => Google
[visits] => 600
[Pageviews] => 1144
[Revenue] => 10
)
[4] => Array
(
[Source] => Analytics
[Date] => 2017-10-30
[Source] => Facebook
[visits] => 1600
[Pageviews] => 11,445
[Revenue] => 5
)
[5] => Array
(
[Source] => Analytics
[Date] => 2017-10-30
[Source] => Facebook
[visits] => 700
[Pageviews] => 7,445
[Revenue] => 8
)
)
Expected Result
Array
(
[0] => Array
(
[Source] => Analytics
[Date] => 2017-10-31
[Source] => Google
[visits] => 6600
[Pageviews] => 13,229
[Revenue] => 35
)
[1] => Array
(
[Source] => Analytics
[Date] => 2017-10-31
[Source] => Facebook
[visits] => 600
[Pageviews] => 1144
[Revenue] => 40
)
[2] => Array
(
[Source] => Analytics
[Date] => 2017-10-30
[Source] => Google
[visits] => 600
[Pageviews] => 1144
[Revenue] => 10
)
[3] => Array
(
[Source] => Analytics
[Date] => 2017-10-30
[Source] => Facebook
[visits] => 2,300
[Pageviews] => 18,890
[Revenue] => 35
)
)
Here is the function you need. Cheers...
function combineAndSumUp ($myArray = []) {
$finalArray = Array ();
foreach ($myArray as $nkey => $nvalue) {
$has = false;
$fk = false;
// Remove comma from numbers
$nvalue['Pageviews'] = str_replace(",","",$nvalue["Pageviews"]);
foreach ($finalArray as $fkey => $fvalue) {
if ( ($fvalue['Date'] == $nvalue['Date']) && ($fvalue['Source'] == $nvalue['Source']) ) {
$has = true;
$fk = $fkey;
break;
}
}
if ( $has === false ) {
$finalArray[] = $nvalue;
} else {
$finalArray[$fk]['visits'] += $nvalue['visits'];
$finalArray[$fk]['Pageviews'] += $nvalue['Pageviews'];
$finalArray[$fk]['Revenue'] += $nvalue['Revenue'];
}
}
return $finalArray;
}
What #J.D.Pace meant by the comment:
How do you intend to differentiate between the two Source elements in each array?
is...
"Your incoming data is impossible, because you may not have two of the same key in the same level of an array. Your data shows "Source" => "Analytics" and "Source" => "Facebook" in the same subarray." ...This means that your sample input data is inaccurate.
Because we cannot know how it should be in your case AND because the Analytics value is completely worthless in respect to this task, I will provide a solution which omits these inaccurate subarray elements.
The other answers are working too hard and writing too much code. It is not necessary to write multiple/nested loops, nor to pre-sort the data. This task is very simply accomplished with a single loop by assigning "compound temporary keys" in your output array and using the output array as a "lookup array" while processing. isset() is a very fast way of determining if the current subarray contains a new group of data or if the group has been encountered before.
When finished you can optionally call array_values() to re-index the output (if needed). You may also like to re-iterate to reformat the Pageviews to include commas -- that is again at your discretion.
Code: (Demo)
$array = [
['Date' => '2017-10-31', 'Source' => 'Google', 'visits' => '6000', 'Pageviews' => '12,214', 'Revenue' => '25'],
['Date' => '2017-10-31', 'Source' => 'Google', 'visits' => '600', 'Pageviews' => '1015', 'Revenue' => '10'],
['Date' => '2017-10-31', 'Source' => 'Facebook', 'visits' => '600', 'Pageviews' => '1144', 'Revenue' => '40'],
['Date' => '2017-10-30', 'Source' => 'Google', 'visits' => '600', 'Pageviews' => '1144', 'Revenue' => '10'],
['Date' => '2017-10-30', 'Source' => 'Facebook', 'visits' => '1600', 'Pageviews' => '11,445', 'Revenue' => '5'],
['Date' => '2017-10-30', 'Source' => 'Facebook', 'visits' => '700', 'Pageviews' => '7,445', 'Revenue' => '8'],
];
foreach ($array as $subarray) {
$tempKey = $subarray['Source'].$subarray['Date']; // this is the compound value
$subarray['Pageviews'] = str_replace(',', '', $subarray['Pageviews']); // get rid of meddlesome comma
if (isset($result[$tempKey])) {
$result[$tempKey]['visits'] += $subarray['visits'];
$result[$tempKey]['Pageviews'] += $subarray['Pageviews'];
$result[$tempKey]['Revenue'] += $subarray['Revenue'];
} else {
$result[$tempKey] = $subarray;
}
}
var_export(array_values($result));
Output: (abridged)
array (
0 =>
array (
'Date' => '2017-10-31',
'Source' => 'Google',
'visits' => 6600,
'Pageviews' => 13229,
'Revenue' => 35,
),
...
)
Assuming there is only one Source index.
Try this:
array_multisort(array_column($arr, 'Date'), SORT_DESC,array_column($arr, 'Source'), SORT_DESC, $arr);
$new_arr = [];
$temp = ["Date"=>""];
$ctr = 0;
foreach($arr as $val) {
if($val["Date"] == $temp["Date"] && $val["Source"] == $temp["Source"]) {
$ctr2 = $ctr - 1;
$new_arr[$ctr2]["visits"] += $val["visits"];
$new_arr[$ctr2]["Pageviews"] += $val["Pageviews"];
$new_arr[$ctr2]["Revenue"] += $val["Revenue"];
} else {
$new_arr[$ctr++] = $val;
}
$temp = $val;
}
print_r($new_arr);

PHP multi dimensional array to unique array of some data but return multi dimensional with its related data

I have an array of farmer and their related crops and also crop related images. I want that array to farmer array with their related crops with image. I have to unique farmer and related crops can be different object in farmer array.
I use below query:
$q = $this->db->select("farmer_master.*, state_master.name as state_name,
district_master.name as district_name,
taluka_master.name as taluka_name, farmer_crop.crop_id
AS crops, farmer_crop.acre AS acres, crop_master.name as
crop_name, GROUP_CONCAT(farmer_crop_images.crop_image)
as crops_images")
->from("farmer_master")
->join("farmer_crop", "farmer_crop.farmer_id =
farmer_master.id","LEFT")
->join("crop_master", "crop_master.id = farmer_crop.crop_id","LEFT")
->join("state_master", "state_master.id =
farmer_master.state_id","LEFT")
->join("district_master", "district_master.id =
farmer_master.district_id","LEFT")
->join("taluka_master", "taluka_master.id =
farmer_master.taluka_id","LEFT")
->join("farmer_crop_images", "farmer_crop_images.farmer_crop_id =
farmer_crop.id","LEFT")
->where("farmer_master.sales_id", $sales_id)
->group_by("farmer_crop_images.farmer_crop_id")
->get();
and result is
$q->result_array();
I have an array like below:
Array
(
[0] => Array
(
[id] => 1
[farmer_name] => Mehul
[mobile] => 8401036474
[address] => Karanagar road
[village] => Nagarvel
[total_acre] => 100
[state_id] => 1
[district_id] => 10
[taluka_id] => 28
[sales_id] => 43
[created_at] => 2017-05-15 04:21:09
[state_name] => gujarat
[district_name] => bharuch
[taluka_name] => anklesvar
[crops] => 4
[acres] => 15
[crop_name] => green gram
[crops_images] => 1494836337726.jpg,1494739175265.jpg
)
[1] => Array
(
[id] => 1
[farmer_name] => Mehul
[mobile] => 8401036474
[address] => Karanagar road
[village] => Nagarvel
[total_acre] => 100
[state_id] => 1
[district_id] => 10
[taluka_id] => 28
[sales_id] => 43
[created_at] => 2017-05-15 04:21:09
[state_name] => gujarat
[district_name] => bharuch
[taluka_name] => anklesvar
[crops] => 3
[acres] => 70
[crop_name] => rice
[crops_images] => 1494836356691.jpg
)
)
And my desired result like below:
Array
(
[0] => Array
(
[id] => 1
[farmer_name] => Mehul
[mobile] => 8401036474
[address] => Karanagar road
[village] => Nagarvel
[total_acre] => 100
[state_id] => 1
[district_id] => 10
[taluka_id] => 28
[sales_id] => 43
[created_at] => 2017-05-15 04:21:09
[state_name] => gujarat
[district_name] => bharuch
[taluka_name] => anklesvar
[crops] => Array
(
[0] => Array
(
[crop_id] => 4
[acres] => 15
[crop_name] => green gram
[crops_images] => 1494836337726.jpg,1494739175265.jpg
)
[1] => Array
(
[crop_id] => 3
[acres] => 70
[crop_name] => rice
[crops_images] => 1494836356691.jpg
)
)
)
)
You have to first take get id of farmer with unique
$ids = array_unique(array_column($array, "id"));
// print_r($ids);exit; Array ( [0] => 1 )
$farmer_list_new = array();
for($i=0; $i<count($ids); $i++)
{
$first_time = true;
$farmer_list_new[$i] = array();
foreach( $farmer_list as $row )
{
if( $ids[$i] == $row['id'] )
{
if( $first_time )
{
$first_time = false;
$farmer_list_new[$i] = array(
'id' => $row['id'] ,
'farmer_name' => $row['farmer_name'] ,
'mobile' => $row['mobile'] ,
'address' => $row['address'] ,
'village' => $row['village'] ,
'total_acre' => $row['total_acre'] ,
'state_id' => $row['state_id'] ,
'district_id' => $row['district_id'] ,
'taluka_id' => $row['taluka_id'] ,
'sales_id' => $row['sales_id'] ,
'created_at' => $row['created_at'] ,
'state_name' => $row['state_name'] ,
'district_name' => $row['district_name'],
'taluka_name' => $row['taluka_name'] ,
'crops' => array()
);
}
$crop_images = explode(",", $row['crops_images']);
foreach($crop_images as $img){
$crop_images_url[] = $img;
}
$img_crops = implode(",", $crop_images_url);
$farmer_list_new[$i]['crops'][] = array(
'crop_id' => $row['crops'],
'acres' => $row['acres'],
'crop_name' => $row['crop_name'],
'crops_images' => $row['crops_images'],
);
}
}
}

Clean multidimensional array in php

I struggle to clean a multidimensional array. I find several Q&A:s on this topic but yet I can't get it to work.
The array $overPayments comes out (from a db call) as below.
Array (
[0] => Array (
[invoiceID] => 103080
[invoiceNumber] => 781
[faktBel] => 1500.00
[totalPayed] => 1500.00
[sumPayedOnThisJournal] => 1500.00
[totOPtoday] => 0.00
[totOPbeforeToday] => -1500.00
[totOPthisJournal] => 0.00 )
[1] => Array(
[invoiceID] => 103290
[invoiceNumber] => 7818
[faktBel] => 648.00
[totalPayed] => 893.00
[sumPayedOnThisJournal] => 893.00
[totOPtoday] => 245.00
[totOPbeforeToday] => -648.00
[totOPthisJournal] => 245.00 )
[2] => Array (
[invoiceID] => 103453
[invoiceNumber] => 202071
[faktBel] => 2250.00
[totalPayed] => 2317.00
[sumPayedOnThisJournal] => 2317.00
[totOPtoday] =>67.00
[totOPbeforeToday] => -2250.00
[totOPthisJournal] => 67.00 )
)
What I need to do is loop through the array called $overPayments containing about 200 sub arrays, and remove all "rows" (subarrays) that have $overPayment['totOPthisJournal'] <= 0. So that I end up with a either modified or new multidimensional array where the totOPthisJournal value is > 0.
I think array_filter is what you are after.
$filteredArray = array_filter($overPayments, function($value) {
return $value['totOPthisJournal'] > 0;
});
just try to unset the array index for which 'totOPthisJournal' is <=0
<?php
$array = Array ( '0' => Array ( 'invoiceID' => 103080, 'invoiceNumber' => 781, 'faktBel' => 1500.00,
'totalPayed' => 1500.00,'sumPayedOnThisJournal' => 1500.00, 'totOPtoday' => 0.00,
'totOPbeforeToday' => -1500.00, 'totOPthisJournal' => 0.00 ), '1' => Array( 'invoiceID' => 103290,
'invoiceNumber' => 7818, 'faktBel' => 648.00, 'totalPayed' => 893.00,
'sumPayedOnThisJournal' => 893.00,'totOPtoday' => 245.00, 'totOPbeforeToday' => -648.00,
'totOPthisJournal' => 245.00 ), '2' => Array ( 'invoiceID' => 103453,'invoiceNumber' => 202071,
'faktBel' => 2250.00, 'totalPayed' => 2317.00, 'sumPayedOnThisJournal' => 2317.00,
'totOPtoday' =>67.00, 'totOPbeforeToday' => -2250.00, 'totOPthisJournal' => 67.00));
foreach($array as $key=>$value){
if($array[$key]['totOPthisJournal'] <= 0){
unset($array[$key]);
}
}
print_r($array);
Put this array into a foreach loop:
foreach($overPayments as $key => $value) {
if($value['totOPthisJournal'] <= 0) {
$key = null;
}
}
This removes the overPayment where [totOPthisJournal] <= 0.
Hope this helps.

Create new array depending on key

My array is like that:
Array
(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
)
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
[2] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[3] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
I want to change that array like that depending on key['type']. If array key['type'] are same, I want to change array like that:
Array
(
[car] => Array(
[0]=>Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
),
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
),
[train]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[cruise]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
)
)
what I mean is that if key['type'] is car, I want to create car array or if the type is train I want to create train array or if the type is cruise I want to create cruise array. I don't know how to loop the array. Anyone please help me. Thanks a lot!
Here's a simple way to do it: loop over the data, and just append to the subarray matching the type value:
// starting data
$starting_array = array (
0 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 1,
'tran_name' => 'private',
'tran_image' => '1251961905A1.jpg',
'type' => 'car',
'troute_id' => 10
),
1 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 2,
'tran_name' => 'express',
'tran_image' => 'bus3.jpg',
'type' => 'car',
'troute_id' => 13
),
2 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 3,
'tran_name' => 'MyanmarTrain',
'tran_image' => 'Burma-Gorteikviaduct.jpg',
'type' => 'train',
'troute_id' => 16
),
3 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 4,
'tran_name' => 'Ayeyarwaddy Cruise',
'tran_image' => 'boat-ChutzpahToo1.jpg',
'type' => 'cruise',
'troute_id' => 22
)
);
// initialize the result array
$result = array();
// loop over the starting array
foreach($starting_array as $entry) {
// make sure the result array has a key matching this item's type
if(!array_key_exists($entry['type'], $result)) {
$result[ $entry['type'] ] = array();
}
// add this item to the result array
$result[ $entry['type'] ][] = $entry;
}
// this is just for testing, so you can verify the output matches your desired result
echo "<pre>";
var_dump($result);
echo "</pre>";
Try this:
<?php
$tempArr = Array
(
Array(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 1,
"tran_name" => "private",
"tran_image" => "1251961905A1.jpg",
"type" => "car",
"troute_id" => 10
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 2,
"tran_name" => "express",
"tran_image" => "bus3.jpg",
"type" => "car",
"troute_id" => 13
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 3,
"tran_name" => "MyanmarTrain",
"tran_image" => "Burma-Gorteikviaduct.jpg",
"type" => "train",
"troute_id" => 16
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 4,
"tran_name" => "Ayeyarwaddy Cruise",
"tran_image" => "boat-ChutzpahToo1.jpg",
"type" => "cruise",
"troute_id" => 22
)
);
$resultArr = array();
foreach($tempArr as $tempKey=>$temp)
{
if(!array_key_exists($temp['type'], $resultArr))
{
$resultArr[$temp['type']] = array();
}
$resultArr[$temp['type']][] = $temp;
}
echo '<pre>';
print_r($resultArr);
?>
This is working fine .....

Convert PHP "grouped" array to "segmented" array

Trying to convert a "grouped" multidimensional array to a "segmented" multidimensional array. Not sure how to explain it exactly so hopefully these print_r results explain it better.
Current Array
Array
(
[qty] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[item_id] => Array
(
[0] => RR332
[1] => FF586
[2] => QW865
)
[item_name] => Array
(
[0] => Widget 1
[1] => Widget 2
[2] => Widget 3
)
[price] => Array
(
[0] => 1.00
[1] => 1.50
[2] => 1.50
)
)
Wanted Array
Array
(
[0] => Array
(
[qty] => 1
[item_id] => RR332
[item_name] => Widget 1
[price] => 1.00
)
[1] => Array
(
[qty] => 1
[item_id] => FF586
[item_name] => Widget 2
[price] => 1.50
)
[2] => Array
(
[qty] => 1
[item_id] => QW865
[item_name] => Widget 3
[price] => 1.50
)
)
This seems like it should be a simple task but I haven't been able to crack it yet.
I would preferably like to loop through the Current Array to create the Wanted Array if possible.
Any help is greatly appreciated!
Thanks.
You mean this?:
$data = array(
'qty' => array(
0 => 1,
1 => 1,
2 => 1,
),
'item_id' => array(
0 => 'RR332',
1 => 'FF586',
2 => 'QW865',
),
'item_name' => array(
0 => 'Widget 1',
1 => 'Widget 2',
2 => 'Widget 3',
),
'price' => array(
0 => '1.00',
1 => '1.50',
2 => '1.50',
),
);
$result = array();
foreach($data as $key => $values)
{
foreach($values as $number => $value)
$result[$number][$key] = $value;
}
print_r($result);
Output:
Array
(
[0] => Array
(
[qty] => 1
[item_id] => RR332
[item_name] => Widget 1
[price] => 1.00
)
[1] => Array
(
[qty] => 1
[item_id] => FF586
[item_name] => Widget 2
[price] => 1.50
)
[2] => Array
(
[qty] => 1
[item_id] => QW865
[item_name] => Widget 3
[price] => 1.50
)
)
I think this will do the job for you, though I don't have a good data set to test against.
$wanted_array = array();
// Iterate over the outer array to get the text keys & inner arrays
foreach ($current_array as $txtkey=>$arr) {
// Iterate over each inner array to get the numeric key
foreach ($arr as $numkey=>$val) {
// Set a numeric key pointing to a new array
// onto the new outer array if it isn't already there
if (!isset($wanted_array[$numkey])) $wanted_array[$numkey] = array();
// Swap the keys of the outer and inner arrays.
$wanted_array[$numkey][$txtkey] = $val;
}
}
Update: Using test array from #hakre's answer, here it is in practice:
$current_array = array(
'qty' => array(
0 => 1,
1 => 1,
2 => 1
),
'item_id' => array(
0 => 'RR332',
1 => 'FF586',
2 => 'QW865'
),
'item_name' => array(
0 => 'Widget 1',
1 => 'Widget 2',
2 => 'Widget 3'
),
'price' => array(
0 => '1.00',
1 => '1.50',
2 => '1.50'
)
);
// Output:
Array
(
[0] => Array
(
[qty] => 1
[item_id] => RR332
[item_name] => Widget 1
[price] => 1.00
)
[1] => Array
(
[qty] => 1
[item_id] => FF586
[item_name] => Widget 2
[price] => 1.50
)
[2] => Array
(
[qty] => 1
[item_id] => QW865
[item_name] => Widget 3
[price] => 1.50
)
)
Like you said, just loop through it to build it.
$wanted = array();
foreach($current['item_id'] as $key => $value) {
$wanted[$key] = array(
'qty' = $current['qty'][$key],
'item_id' = $current['item_id'][$key],
'item_name' = $current['item_name'][$key],
'price' = $current['price'][$key]
);
}
print_r($wanted);
You can loop over your original array using the foreach loop, which gives you the $key, and $value for each item in your array. From there, construct your new array as you like.
$array2 = array();
foreach ($array1 as $key => $value)
{
$array2[] = array('key' => 'value');
}

Categories