I have the following 2D-Array:
Array (
[ID] => Array
(
[0] => 150
[1] => 250
)
[Group] => Array
(
[0] => 120
[1] => 120
)
[Name] => Array
(
[0] => Name 1
[1] => Name 2
)
[Price] => Array
(
[0] => 9.99
[1] => 11.99
)
[Date] => Array
(
[0] => 12.04.2013
[1] => 11.04.2013
)
[SortIndex] => Array
(
[0] => 20
[1] => 10
)
)
and I want to sort them after the SortIndex-Array ascending. But the different Values from the Arrays are related to the others. So the association can't be changed.
How I could sort them?
Is there is any reason not to convert array to more suitable form?
Array (
[0] => Array
(
[ID] => 150
[Group] => 120
)
[1] => Array
(
[ID] => 250
[GROUP] => 120
)
....
Related
I have following array which has ID as index, and some count as value. Ultimate goal is to get total of unique IDs in another array.
Array
(
[0] => Array
(
[0] => Team Object
(
[id] => 1
[countStat] => 25
)
)
[1] => Array
(
[0] => Team Object
(
[id] => 2
[countStat] => 24
)
)
[2] => Array
(
[0] => Team Object
(
[id] => 1
[countStat] => 23
)
)
[3] => Array
(
[0] => Team Object
(
[id] => 3
[countStat] => 23
)
)
[4] => Array
(
[0] => Team Object
(
[id] => 5
[countStat] => 21
)
)
[5] => Array
(
[0] => Team Object
(
[id] => 3
[countStat] => 21
)
)
[6] => Array
(
[0] => Team Object
(
[id] => 5
[countStat] => 20
)
)
[7] => Array
(
[0] => Team Object
(
[id] => 2
[countStat] => 20
)
)
)
I want result like below.
Array
(
[0] => Array
(
[0] => Team Object
(
[id] => 1
[countStat] => 48
)
)
[1] => Array
(
[0] => Team Object
(
[id] => 2
[countStat] => 44
)
)
[3] => Array
(
[0] => Team Object
(
[id] => 3
[countStat] => 44
)
)
[4] => Array
(
[0] => Team Object
(
[id] => 5
[countStat] => 41
)
)
)
I has spend few hours working on it but couldn't get solution. Can someone please help ?
Thank you.
Looks like you want to sum team object's countStats for like ids.
I've created a similar data structure, looped through it to form a temporary array that uses the id as a key, with associated amounts (to later sum - mine is amount, yours is countStats).
Then sum those and recreate desired output.
<?php
class Team
{
public $id;
public $amount;
public function __construct($id, $amount)
{
$this->id = $id;
$this->amount = $amount;
}
}
$input =
[
[new Team(1, 3)],
[new Team(1, 4)],
[new Team(2, 5)],
[new Team(2, 7)]
];
foreach($input as $subarray)
$amounts[$subarray[0]->id][]=$subarray[0]->amount;
print_r($amounts);
foreach($amounts as $k => $v)
$result[] = [new Team($k, array_sum($v))];
print_r($result);
Output:
Array
(
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 5
[1] => 7
)
)
Array
(
[0] => Array
(
[0] => Team Object
(
[id] => 1
[amount] => 7
)
)
[1] => Array
(
[0] => Team Object
(
[id] => 2
[amount] => 12
)
)
)
This is the array which i get from post
Array
(
[data] => Array
(
[Invoice] => Array
(
[itemNo] => Array
(
[0] => rtgrg
[1] => 4t4t
[2] => ththt
[3] => thth
)
[itemName] => Array
(
[0] => rtgrt
[1] => 4t5t5
[2] => hthtyh
[3] => gnghnn
)
[itemDiscription] => Array
(
[0] => 5tr5t
[1] => t45t4
[2] => tyhthtyh
[3] => gnghnh
)
[price] => Array
(
[0] => 2
[1] => 10
[2] => 9
[3] => 12
)
[itemQuantity] => Array
(
[0] => 2
[1] => 12
[2] => 9
[3] => 9
)
[itemDiscount] => Array
(
[0] => 11
[1] => 0.14
[2] => 0.13
[3] => 0.1
)
[itemTotal] => Array
(
[0] => 333333
[1] => 34535
[2] => 55555555555555
[3] => 666666666
)
[itemStartDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] => 2016-06-17
)
[itemEndDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[itemCustomcol] => Array
(
[0] => 4t4t4
[1] => t5trgr
[2] => htht
[3] => gngh
)
[itemCustomcol2] => Array
(
[0] => t4t4t
[1] => rtgtr
[2] => thth
[3] => gng
)
[itemttax2] => Array
(
[0] => tax1
)
[itemttax3] => Array
(
[0] => tax1
)
[itemttax4] => Array
(
[0] => tax1
)
[itemtCsTax] => Array
(
[0] => gngn
)
[itemtCsTaxPer] => Array
(
[0] => 0.1
)
[itemtDiscount] => Array
(
[0] => 0.18
)
[itemtInvReceived] => Array
(
[0] => gngn
)
[itemtInvToReturn] => Array
(
[0] => gngng
)
[itemInvDue] => Array
(
[0] => nghnghng
)
[itemInvComment] => Array
(
[0] => hngnhg
)
)
)
There is a separate table for dynamic input like itemName, itemDiscription to itemCustomcol2 and another table for itemttax2 to itemInvComment .
I tried foreach loop for multiple element array but this doent work for me as it returns a string. one of them is shown below
$capture_field_vals ="";
foreach($_POST["data"]["Invoice"]["itemNo"] as $key => $text_field){
$capture_field_vals .= $text_field .", ";
}
echo $capture_field_vals;
What i want is collect all element[0] and then insert it in a row mysql and then another element another row and so on.
Is there any reason you can't use the following format?
Array
(
[data] => Array
(
[Invoice] => Array
(
[0] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),
[1] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),...
with this method you can loop through retrieving all details of each item in the invoice?
If I have misunderstood your problem, I apologise
I am generating menu from the database, it is working perfectly but only problem is, my parent node is ROOT which I have to remove from the array.
Array ( [0] => Array ( [label] => ROOT [url] => Array ( [0] => category/view [id] => 1 )
[items] => Array (
[0] => Array ( [label] => DESIGNERS [url] => Array ( [0] => category/view [id] => 2 ) )
[1] => Array ( [label] => WOMEN [url] => Array ( [0] => category/view [id] => 3 ) [items] => Array ( [0] => Array ( [label] => CURRENT SALES [url] => Array ( [0] => category/view [id] => 8 ) [items] => Array ( [0] => Array ( [label] => SUIT SELECTIONS [url] => Array ( [0] => category/view [id] => 10 ) ) ) ) [1] => Array ( [label] => ENDING SOON [url] => Array ( [0] => category/view [id] => 9 ) ) ) )
[2] => Array ( [label] => MEN [url] => Array ( [0] => category/view [id] => 4 ) )
[3] => Array ( [label] => MAKE IT YOUR OWN [url] => Array ( [0] => category/view [id] => 5 ) )
[4] => Array ( [label] => CLEARANCE [url] => Array ( [0] => category/view [id] => 6 ) )
[5] => Array ( [label] => OUT OF THE BOX [url] => Array ( [0] => category/view [id] => 7 ) ) ) ) )
I want the array to start with [items] => Array(), how do I remove the parent node.
The desired solution must be:
Array (
[0] => Array ( [label] => DESIGNERS [url] => Array ( [0] => category/view [id] => 2 ) )
[1] => Array ( [label] => WOMEN [url] => Array ( [0] => category/view [id] => 3 ) [items] => Array ( [0] => Array ( [label] => CURRENT SALES [url] => Array ( [0] => category/view [id] => 8 ) [items] => Array ( [0] => Array ( [label] => SUIT SELECTIONS [url] => Array ( [0] => category/view [id] => 10 ) ) ) ) [1] => Array ( [label] => ENDING SOON [url] => Array ( [0] => category/view [id] => 9 ) ) ) )
[2] => Array ( [label] => MEN [url] => Array ( [0] => category/view [id] => 4 ) )
[3] => Array ( [label] => MAKE IT YOUR OWN [url] => Array ( [0] => category/view [id] => 5 ) )
[4] => Array ( [label] => CLEARANCE [url] => Array ( [0] => category/view [id] => 6 ) )
[5] => Array ( [label] => OUT OF THE BOX [url] => Array ( [0] => category/view [id] => 7 ) ) ) ) )
I am not sure I understood well, is that what you want?
$desired_array=$old_array[0]['items']
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've been looking for the follow solution to create the array structure I need.
For a a list with updates for a specific user who can be following different things, I need to get first the followed items and the data compare to this item. my output for this is as follow.
array how it is right now
Array
(
[0] => Array
(
[website_id] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
[1] => Array
(
[id] => 1
[follower_id] => 2
)
[2] => Array
(
[id] => 1
[follower_id] => 3
)
[3] => Array
(
[id] => 1
[follower_id] => 4
)
[4] => Array
(
[id] => 1
[follower_id] => 5
)
)
)
[1] => Array
(
[website_id] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
)
)
)
I want to create the above array like tho on below
array like it has to be
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
[1] => Array
(
[id] => 1
[follower_id] => 2
)
[2] => Array
(
[id] => 1
[follower_id] => 3
)
[3] => Array
(
[id] => 1
[follower_id] => 4
)
[4] => Array
(
[id] => 1
[follower_id] => 5
)
[5] => Array
(
[id] => 1
[follower_id] => 1
)
)
Thanks in advice.
With kind regards,
Nicky
Seems to me that array_merge() should be sufficient:
$newArray = array_merge($myArray[0]['website_id'], $myArray[1]['website_id']);