Array separating

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ashrafy84
Forum Newbie
Posts: 9
Joined: Tue Dec 23, 2008 4:29 am

Array separating

Post by ashrafy84 »

i have retrieved the records from mysql database as an double dimensional array which is give below

[text]Array
(
[0] => Array
(
[0] => 11
[BudgetHead] => 11
[1] => 1
[Ident] => 1
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 11
[5] => 500
[Amount] => 500
[6] => csir/2010/101
[LetterNo] => csir/2010/101
[7] => 2010-09-27
[LetterDate] => 2010-09-27
[8] => Contingency
[BudgetName] => Contingency
)

[1] => Array
(
[0] => 11
[BudgetHead] => 11
[1] => 6
[Ident] => 6
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 11
[5] => 5000
[Amount] => 5000
[6] => hghg/2010
[LetterNo] => hghg/2010
[7] => 2010-09-28
[LetterDate] => 2010-09-28
[8] => Contingency
[BudgetName] => Contingency
)

[2] => Array
(
[0] => 11
[BudgetHead] => 11
[1] => 11
[Ident] => 11
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 11
[5] => 100000
[Amount] => 100000
[6] =>
[LetterNo] =>
[7] => 2010-10-10
[LetterDate] => 2010-10-10
[8] => Contingency
[BudgetName] => Contingency
)

[3] => Array
(
[0] => 15
[BudgetHead] => 15
[1] => 2
[Ident] => 2
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 15
[5] => 1000
[Amount] => 1000
[6] => csir/2010/101
[LetterNo] => csir/2010/101
[7] => 2010-09-27
[LetterDate] => 2010-09-27
[8] => Equipment(Major)
[BudgetName] => Equipment(Major)
)

[4] => Array
(
[0] => 15
[BudgetHead] => 15
[1] => 7
[Ident] => 7
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 15
[5] => 10000
[Amount] => 10000
[6] => hghg/2010
[LetterNo] => hghg/2010
[7] => 2010-09-28
[LetterDate] => 2010-09-28
[8] => Equipment(Major)
[BudgetName] => Equipment(Major)
)

[5] => Array
(
[0] => 35
[BudgetHead] => 35
[1] => 3
[Ident] => 3
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 35
[5] => 1500
[Amount] => 1500
[6] => csir/2010/101
[LetterNo] => csir/2010/101
[7] => 2010-09-27
[LetterDate] => 2010-09-27
[8] => Overhead Charges - Departments
[BudgetName] => Overhead Charges - Departments
)

[6] => Array
(
[0] => 35
[BudgetHead] => 35
[1] => 8
[Ident] => 8
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 35
[5] => 15000
[Amount] => 15000
[6] => hghg/2010
[LetterNo] => hghg/2010
[7] => 2010-09-28
[LetterDate] => 2010-09-28
[8] => Overhead Charges - Departments
[BudgetName] => Overhead Charges - Departments
)

[7] => Array
(
[0] => 36
[BudgetHead] => 36
[1] => 4
[Ident] => 4
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 36
[5] => 2000
[Amount] => 2000
[6] => csir/2010/101
[LetterNo] => csir/2010/101
[7] => 2010-09-27
[LetterDate] => 2010-09-27
[8] => Overhead Charges - University
[BudgetName] => Overhead Charges - University
)

[8] => Array
(
[0] => 36
[BudgetHead] => 36
[1] => 9
[Ident] => 9
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 36
[5] => 20000
[Amount] => 20000
[6] => hghg/2010
[LetterNo] => hghg/2010
[7] => 2010-09-28
[LetterDate] => 2010-09-28
[8] => Overhead Charges - University
[BudgetName] => Overhead Charges - University
)

[9] => Array
(
[0] => 18
[BudgetHead] => 18
[1] => 5
[Ident] => 5
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 18
[5] => 2500
[Amount] => 2500
[6] => csir/2010/101
[LetterNo] => csir/2010/101
[7] => 2010-09-27
[LetterDate] => 2010-09-27
[8] => Fellowship
[BudgetName] => Fellowship
)

[10] => Array
(
[0] => 18
[BudgetHead] => 18
[1] => 10
[Ident] => 10
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 18
[5] => 25000
[Amount] => 25000
[6] => hghg/2010
[LetterNo] => hghg/2010
[7] => 2010-09-28
[LetterDate] => 2010-09-28
[8] => Fellowship
[BudgetName] => Fellowship
)

)

This array should be formed like this as given below

Array
(
[0] => Array
(
[0] => Array
(
[0] => 11
[BudgetHead] => Contingency
[1] => 1
[Ident] => 1
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 11
[5] => 500
[Amount] => 500
[6] => csir/2010/101
[LetterNo] => csir/2010/101
[7] => 2010-09-27
[LetterDate] => 2010-09-27
[8] => Contingency
)
[1] => Array
(
[0] => 11
[BudgetHead] => Contingency
[1] => 6
[Ident] => 6
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 11
[5] => 5000
[Amount] => 5000
[6] => hghg/2010
[LetterNo] => hghg/2010
[7] => 2010-09-28
[LetterDate] => 2010-09-28
[8] => Contingency
)

[2] => Array
(
[0] => 11
[BudgetHead] => Contingency
[1] => 11
[Ident] => 11
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 11
[5] => 100000
[Amount] => 100000
[6] =>
[LetterNo] =>
[7] => 2010-10-10
[LetterDate] => 2010-10-10
[8] => Contingency
)
)
[1] => Array
(
[0] => Array
(
[0] => 15
[BudgetHead] => Equipment(Major)
[1] => 2
[Ident] => 2
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 15
[5] => 1000
[Amount] => 1000
[6] => csir/2010/101
[LetterNo] => csir/2010/101
[7] => 2010-09-27
[LetterDate] => 2010-09-27
[8] => Equipment(Major)
)
[1] => Array
(
[0] => 15
[BudgetHead] => Equipment(Major)
[1] => 7
[Ident] => 7
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 15
[5] => 10000
[Amount] => 10000
[6] => hghg/2010
[LetterNo] => hghg/2010
[7] => 2010-09-28
[LetterDate] => 2010-09-28
[8] => Equipment(Major)
)
)

[2] => Array
(
[0] => Array
(
[0] => 35
[BudgetHead] => Overhead Charges - Departments
[1] => 3
[Ident] => 3
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 35
[5] => 1500
[Amount] => 1500
[6] => csir/2010/101
[LetterNo] => csir/2010/101
[7] => 2010-09-27
[LetterDate] => 2010-09-27
[8] => Overhead Charges - Departments
)
[1] => Array
(
[0] => 35
[BudgetHead] => Overhead Charges - Departments
[1] => 8
[Ident] => 8
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 35
[5] => 15000
[Amount] => 15000
[6] => hghg/2010
[LetterNo] => hghg/2010
[7] => 2010-09-28
[LetterDate] => 2010-09-28
[8] => Overhead Charges - Departments
)
)
[3] => Array
(
[0] =>Array
(
[0] => 36
[BudgetHead] => Overhead Charges - University
[1] => 4
[Ident] => 4
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 36
[5] => 2000
[Amount] => 2000
[6] => csir/2010/101
[LetterNo] => csir/2010/101
[7] => 2010-09-27
[LetterDate] => 2010-09-27
[8] => Overhead Charges – University
)
[1] => Array
(
[0] => 36
[BudgetHead] => Overhead Charges - University
[1] => 9
[Ident] => 9
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 36
[5] => 20000
[Amount] => 20000
[6] => hghg/2010
[LetterNo] => hghg/2010
[7] => 2010-09-28
[LetterDate] => 2010-09-28
[8] => Overhead Charges - University
)
)

[4] => Array
(
[0] =>Array
(
[0] => 18
[BudgetHead] => Fellowship
[1] => 5
[Ident] => 5
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 18
[5] => 2500
[Amount] => 2500
[6] => csir/2010/101
[LetterNo] => csir/2010/101
[7] => 2010-09-27
[LetterDate] => 2010-09-27
[8] => Fellowship
)
[10] => Array
(
[0] => 18
[BudgetHead] => Fellowship
[1] => 10
[Ident] => 10
[2] => 11021001
[ProjectCode] => 11021001
[3] =>
[Department] =>
[4] => 18
[5] => 25000
[Amount] => 25000
[6] => hghg/2010
[LetterNo] => hghg/2010
[7] => 2010-09-28
[LetterDate] => 2010-09-28
[8] => Fellowship
)
)

)[/text]

The concept of this is. i have compared the budgetHead which has same value and it should be formed as single array. I have tried my best but i didnt get any solution. Please any one help me. I need its soon.if any code is available means send it ashraf.y84@gmail.com
Last edited by Benjamin on Fri Oct 15, 2010 10:42 am, edited 1 time in total.
Reason: Added [syntax=text] tags.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Array separating

Post by Jonah Bron »

Create an array called $budgetHead, and loop through each element in the original array. Add each budgethead to the $budgetHead array, and then run array_unique() on the array.

When that's finished, you should have an array ($budgetHead) of unique values. Now, create a new array called $newFormat. Loop through the $budgetHead array, and loop through the original array inside of that. Search for the current budgetHead. If it's correct, add that element to $newFormat. Here's some pseudo code to get you going:

[syntax]
budgetHead;
originalFormat = // the original array
newFormat;

foreach (originalFormat as original) {
budgetHead[] = original['BudgetHead'];
}
budgetHead = array_unique(budgetHead);
// we now have an array of unique budgetheads.

foreach (budgetHead as budget) {
temp;
foreach (originalFormat as key => original) {
if (original['BudgetHead'] == budget) {
temp[] = original;
unset(originalFormat[key]);
}
}
newFormat[] = temp;
}

// newFormat now holds the array![/syntax]

Note: This is pseudo code. This is not actual working PHP, but the functions used in it are real functions. It's purpose is to show you how the logic works.

Also, please put all code/large amounts of text into

Code: Select all

 or [syntax] tags.  All PHP related questions belong in [url=http://forums.devnetwork.net/viewforum.php?f=1]PHP - Code[/url].
ashrafy84
Forum Newbie
Posts: 9
Joined: Tue Dec 23, 2008 4:29 am

Re: Array separating

Post by ashrafy84 »

Hi Jonah Bron, thanks for your help. i worked on it and i got the output successfully what i have expected. once again thanks for your help johan
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Array separating

Post by Benjamin »

:arrow: Moved to PHP - Code
Post Reply