PHP Adjacency Data to Nested Array - php

I am having array as follows:
Array
(
[0] => Array
(
[question_id] => 1
[question_title] => Q1
[child_question_id] => NULL
)
[1] => Array
(
[question_id] => 2
[question_title] => Q2
[child_question_id] => NULL
)
[2] => Array
(
[question_id] => 3
[question_title] => Q3
[child_question_id] => 4
)
[3] => Array
(
[question_id] => 3
[question_title] => Q3
[child_question_id] => 5
)
[4] => Array
(
[question_id] => 4
[question_title] => Q4
[child_question_id] => NULL
)
[5] => Array
(
[question_id] => 5
[question_title] => Q5
[child_question_id] => NULL
)
[6] => Array
(
[question_id] => 6
[question_title] => Q6
[child_question_id] => NULL
)
[7] => Array
(
[question_id] => 7
[question_title] => Q7
[child_question_id] => 6
)
)
And using PHP I am expecting result as:
Array
(
[0] => Array
(
[question_id] => 1
[question_title] => Q1
[child_question_id] => NULL
)
[1] => Array
(
[question_id] => 2
[question_title] => Q2
[child_question_id] => NULL
)
[2] => Array
(
[question_id] => 3
[question_title] => Q3
[children] => array(
[0] => Array
(
[question_id] => 4
[question_title] => Q4
)
[1] => Array
(
[question_id] => 5
[question_title] => Q5
)
)
)
[3] => Array
(
[question_id] => 7
[question_title] => Q7
[children] => array(
[0] => Array
(
[question_id] => 6
[question_title] => Q6
)
)
)
)
I tried to build final array using following logic:
Traversing array to collect child ids and child data
Traversing array again and match items child id with child array
formed earlier, if found then create children array
Could you please help me out to create better solution?

Did you mean something like that?
$i = 0;
$new = array();
$unsetter = array();
foreach($array as $val)
{
$new[$i] = array();
foreach($val as $key => $data)
{
if(($key == 'child_question_id') && (!is_null($data)))
{
$new[$i]['children'] = array();
$new[$i]['children']['question_id'] = $data;
$new[$i]['children']['question_title'] = $array[$data]['question_title'];
$unsetter[] = $data;
} else {
$new[$i][$key] = $data;
}
}
$i++;
}
foreach($unsetter as $uns)
{
unset($new[$uns]);
}
$new = array_values($new);
var_dump($new);
This will output
array (size=5)
0 =>
array (size=3)
'question_id' => int 1
'question_title' => string 'Q1' (length=2)
'child_question_id' => null
1 =>
array (size=3)
'question_id' => int 2
'question_title' => string 'Q2' (length=2)
'child_question_id' => null
2 =>
array (size=3)
'question_id' => int 3
'question_title' => string 'Q3' (length=2)
'children' =>
array (size=2)
'question_id' => int 4
'question_title' => string 'Q4' (length=2)
3 =>
array (size=3)
'question_id' => int 3
'question_title' => string 'Q3' (length=2)
'children' =>
array (size=2)
'question_id' => int 5
'question_title' => string 'Q5' (length=2)
4 =>
array (size=3)
'question_id' => int 7
'question_title' => string 'Q7' (length=2)
'children' =>
array (size=2)
'question_id' => int 6
'question_title' => string 'Q6' (length=2)
Live Demo

Related

Merging two array with same values in php

I have two arrays.
Array 1
Array
(
[1] => 111,
[id1] => 1,
[2] => 11231,
[id2] => 2,
[3] => 12311,
[id3] => 3,
[4] => 11981,
[id4] => 4,
[5] => 11761,
[id5] => 5,
[6] => 11561,
[id6] => 6
)
Array 2
Array
(
[1] => 2,
[id1] => 1,
[2] => 2,
[id2] => 2,
[3] => 3,
[id3] => 3,
[4] => 4,
[id4] => 4,
[5] => 4,
[id5] => 5,
[6] => 6,
[id6] => 6
)
Id key is user id in both arrays
And numerical key in second array is manager id
I want to merge these two array in below format.
Merge array
Array
(
[2] => Array
(
[0] => 111
[1] => 1
),
Array
(
[0] => 11231
[1] => 2
),
[3] => Array
(
[0] => 12311
[1] => 3
),
[4] => Array
(
[0] => 11981
[1] => 4
),
Array
(
[0] => 11761
[1] => 5
),
[6] => Array
(
[0] => 11561
[1] => 6
)
)
Array inside array is the value of first array.
2,3,4,6 key is values from second array.
Those users who have same manager id will be merge in single array.
Array Walk Apply a user supplied function to every member of an array
array_walk($array2, function($value,$key) use($array1,&$result){
if(is_integer($key))
$result[$value][]=[$array1[$key],$array1['id'.$key]];
});
output
array (size=4)
2 =>
array (size=2)
0 =>
array (size=2)
0 => int 111
1 => int 1
1 =>
array (size=2)
0 => int 11231
1 => int 2
3 =>
array (size=1)
0 =>
array (size=2)
0 => int 12311
1 => int 3
4 =>
array (size=2)
0 =>
array (size=2)
0 => int 11981
1 => int 4
1 =>
array (size=2)
0 => int 11761
1 => int 5
6 =>
array (size=1)
0 =>
array (size=2)
0 => int 11561
1 => int 6

Organizing multi-dimensional array of inventory

I need help organizing my inventory array. The structure is one big inventory array, with array of items inside. Per item array consists of the following:
item, item_group, item_no and array sold. sold consists of inner arrays with dates and quantity. Now, I'm having trouble organizing it for my needed output. I'll give you guys sample of input and output. So please do check and it's very much appreciated.
Sample part of my $inventory array
Array
(
[0] => Array
(
[item] => NK
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 11
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 1
[date] => 2017-10-29
)
)
[item_no] => 1
)
[1] => Array
(
[item] => FL
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 7
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 2
)
[2] => Array
(
[item] => AD
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 5
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 3
[date] => 2017-10-29
)
)
[item_no] => 3
)
[3] => Array
(
[item] => CV
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 4
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 4
)
[4] => Array
(
[item] => NB
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 12
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 4
[date] => 2017-10-29
)
)
[item_no] => 5
)
[5] => Array
(
[item] => SP
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 4
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 6
)
[6] => Array
(
[item] => WB
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 5
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 7
)
[7] => Array
(
[item] => wny
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 4
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 8
)
[8] => Array
(
[item] => bs
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 15
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 9
)
[9] => Array
(
[item] => st
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 16
[date] => 2017-10-29
)
)
[item_no] => 10
)
[10] => Array
(
[item] => ayhtdws
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 11
)
[11] => Array
(
[item] => sif
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 3
[date] => 2017-10-29
)
)
[item_no] => 12
)
[12] => Array
(
[item] => bb
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 13
)
)
From there, what I want to display is like this. Grouped by date ascending. And each item => quantity sold
Array
(
[0] => Array
(
[date] => 2017-10-28
[NK] => 11
[FL] => 7
[AD] => 5
[CV] => 4
[NB] => 12
[SP] => 4
[WB] => 5
[wny] => 4
[bs] => 15
)
[1] => Array
(
[date] => 2017-10-29
[NK] => 1
[FL] => 2
[AD] => 3
[CV] => 6
[NB] => 4
[SP] => 6
[WB] => 2
[wny] => 6
[bs] => 2
[st] => 16
[ayhtdws] => 2
[sif] => 3
[bb] => 6
)
)
I've spent almost 3 days figuring this out and up to this writing, I was only able to make it this far
$result = array();
$dates = array();
foreach ($inventory as $key => $item) {
foreach ($item['sold'] as $k => $v) {
array_push($dates, $v['date']);
}
}
$dates = array_unique($dates);
foreach($dates as $key => $value) {
array_push($result, array('date' => $value));
}
foreach ($dates as $rkey => $rvalue) {
foreach ($inventory as $key => $item) {
foreach ($item['sold'] as $k => $v) {
if ($v['date'] = $result[$key]['date']) {
array_push($result[$key][$item['item']] = $v['quantity']);
}
}
}
}
return $result;
Which of course gives me this sad result
Array
(
[0] => Array
(
[date] => 2017-10-28
[NK] => 1
)
[1] => Array
(
[date] => 2017-10-29
[FL] => 2
)
)
And to make things worse, we have this rule about cyclomatic complexities that we should only have at most 3 loop/conditions and up to 3 nesting levels per loop/conditions. And the whole organizing should not have any user created functions.
Even if not following the rules, I wasn't able to figure it out for days. Sorry if problem is long. Please help :(
Update: var_export($inventory) output
array (
0 =>
array (
'item' => 'NK',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '11',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '1',
'date' => '2017-10-29',
),
),
'item_no' => '1',
),
1 =>
array (
'item' => 'FL',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '7',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '2',
),
2 =>
array (
'item' => 'AD',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '5',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '3',
'date' => '2017-10-29',
),
),
'item_no' => '3',
),
3 =>
array (
'item' => 'CV',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '4',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '4',
),
4 =>
array (
'item' => 'NB',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '12',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '4',
'date' => '2017-10-29',
),
),
'item_no' => '5',
),
5 =>
array (
'item' => 'SP',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '4',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '6',
),
6 =>
array (
'item' => 'WB',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '5',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '7',
),
7 =>
array (
'item' => 'wny',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '4',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '8',
),
8 =>
array (
'item' => 'bs',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '15',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '9',
),
9 =>
array (
'item' => 'st',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '16',
'date' => '2017-10-29',
),
),
'item_no' => '10',
),
10 =>
array (
'item' => 'ayhtdws',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '11',
),
11 =>
array (
'item' => 'sif',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '3',
'date' => '2017-10-29',
),
),
'item_no' => '12',
),
12 =>
array (
'item' => 'bb',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '13',
),
)
I don't know if this is more or less the same as Erwin's code, but I wrote this code 13 hours ago and had to wait for the array.
Edit; I have tested Erwin's code and it seems we have close to matching code.
He makes one loop more to get the date in there but it's more or less the same.
I loop the array and the subarray sold.
I make the new array key the date example:
Echo $new['2017-10-28']['nk']; // 11
And if the date key is not set already I create it.
Once the loop is done I use array_values to remove the date keys making the array look like:
Echo $new[0]['nk']; // 11
The code:
$new =[];
Foreach($inventory as $sub){
Foreach($sub["sold"] as $sold){
If (!isset($new[$sold["date"]]["date"])) $new[$sold["date"]]["date"] = $sold["date"];
$new[$sold["date"]][$sub["item"]] = $sold["quantity"];
}
}
$new = array_values($new);
Var_dump($new);
https://3v4l.org/mGJSX
I've run through some tests and workarounds and come up with this solution. I've managed to do it by using ksort(), array_values()and array_key_exists(). I used the date first as an array key to contain all items sold on that date. Then, move it inside the array after gathering all items, then renumbered the array. Further explanation is given through code comments
// result array
$result = array();
// Loop for each item on inventory
foreach ($inventory as $inv) {
// Loop for each sold array
foreach ($inv['sold'] as $item) {
// date sold
$date = $item['date'];
// Check if date already exist in result array as key
// If not, Create new array with key equal to date and push to result array
if (!array_key_exists($date, $result)) {
$result[$date] = array();
}
// Add array of item => quantity to corresponding date array inside result array
$result[$date][$inv['item']] = $item['quantity'];
}
}
// From here you already have each item => quantity sold inside each date array
// e.g. $result = array( '2017-10-28' => array('NK' => '11', 'FL' => '7', ...) );
// Sort keys which are dates ascending
ksort($result);
// Loop to results and set
foreach ($result as $key => $value) {
$result[$key]['date'] = $key;
}
// Renumber Keys
$result = array_values($result);
// Print result
echo '<pre>';
print_r($result);
Output
Array
(
[0] => Array
(
[NK] => 11
[FL] => 7
[AD] => 5
[CV] => 4
[NB] => 12
[SP] => 4
[WB] => 5
[wny] => 4
[bs] => 15
[date] => 2017-10-28
)
[1] => Array
(
[NK] => 1
[FL] => 2
[AD] => 3
[CV] => 6
[NB] => 4
[SP] => 6
[WB] => 2
[wny] => 6
[bs] => 2
[st] => 16
[ayhtdws] => 2
[sif] => 3
[bb] => 6
[date] => 2017-10-29
)
)

Update a multidimensional array

I'm trying to update my array but it doesn't work as expected!
I got this array:
Array
(
[0] => Array
(
[0] => property_unit Object
(
[unit] => one bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 3
[size] => 54
[percentage] => 0
[search] => 1
)
)
)
[1] => property_unit Object
(
[unit] => two bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 74
[percentage] => 0
[search] => 1
)
)
)
[2] => property_unit Object
(
[unit] => three bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 90
[percentage] => 0
[search] => 1
)
)
)
[3] => property_unit Object
(
[unit] => studio
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 2
[size] => 22.35
[percentage] => 0
[search] => 1
)
)
)
)
[1] => Array
(
[0] => property_unit Object
(
[unit] => one bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 3
[size] => 54
[percentage] => 0
[search] => 1
)
)
)
[1] => property_unit Object
(
[unit] => two bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 74
[percentage] => 0
[search] => 1
)
)
)
[2] => property_unit Object
(
[unit] => studio
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 2
[size] => 22.35
[percentage] => 0
[search] => 1
)
)
)
[3] => property_unit Object
(
[unit] => three bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 90
[percentage] => 0
[search] => 1
)
)
)
)
[2] => Array
(
[0] => property_unit Object
(
[unit] => one bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 3
[size] => 54
[percentage] => 0
[search] => 1
)
)
)
[1] => property_unit Object
(
[unit] => three bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 90
[percentage] => 0
[search] => 1
)
)
)
[2] => property_unit Object
(
[unit] => two bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 74
[percentage] => 0
[search] => 1
)
)
)
[3] => property_unit Object
(
[unit] => studio
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 2
[size] => 22.35
[percentage] => 0
[search] => 1
)
)
)
)
)
and using var_dump()
array (size=24)
0 =>
array (size=4)
0 =>
object(property_unit)[1]
public 'unit' => string 'one bedroom' (length=11)
public 'details' =>
array (size=1)
...
1 =>
object(property_unit)[3]
public 'unit' => string 'two bedroom' (length=11)
public 'details' =>
array (size=1)
...
2 =>
object(property_unit)[5]
public 'unit' => string 'three bedroom' (length=13)
public 'details' =>
array (size=1)
...
3 =>
object(property_unit)[7]
public 'unit' => string 'studio' (length=6)
public 'details' =>
array (size=1)
...
1 =>
array (size=4)
0 =>
object(property_unit)[1]
public 'unit' => string 'one bedroom' (length=11)
public 'details' =>
array (size=1)
...
1 =>
object(property_unit)[3]
public 'unit' => string 'two bedroom' (length=11)
public 'details' =>
array (size=1)
...
2 =>
object(property_unit)[7]
public 'unit' => string 'studio' (length=6)
public 'details' =>
array (size=1)
...
3 =>
object(property_unit)[5]
public 'unit' => string 'three bedroom' (length=13)
public 'details' =>
array (size=1)
...
2 =>
array (size=4)
0 =>
object(property_unit)[1]
public 'unit' => string 'one bedroom' (length=11)
public 'details' =>
array (size=1)
...
1 =>
object(property_unit)[5]
public 'unit' => string 'three bedroom' (length=13)
public 'details' =>
array (size=1)
...
2 =>
object(property_unit)[3]
public 'unit' => string 'two bedroom' (length=11)
public 'details' =>
array (size=1)
...
3 =>
object(property_unit)[7]
public 'unit' => string 'studio' (length=6)
public 'details' =>
array (size=1)
...
3 =>
I got this array with this:
$array = [new property_unit('one bedroom', [new propertydetails(1, 54, 0, 1)]),
new property_unit('two bedroom', [new propertydetails(1, 74, 0, 1)]),
new property_unit('three bedroom', [new propertydetails(1, 90, 0, 1)]),
new property_unit('studio', [new propertydetails(2, 22.35, 0, 1)])];
Then I perform a computation on it to get all the possible array.
function computePermutations($array) {
$result = [];
$recurse = function($array, $start_i = 0) use (&$result, &$recurse) {
if ($start_i === count($array)-1) {
array_push($result, $array);
}
for ($i = $start_i; $i < count($array); $i++) {
//Swap array value at $i and $start_i
$t = $array[$i];
$array[$i] = $array[$start_i];
$array[$start_i] = $t;
//Recurse
$recurse($array, $start_i + 1);
//Restore old order
$t = $array[$i];
$array[$i] = $array[$start_i];
$array[$start_i] = $t;
}
};
$recurse($array);
return $result;
}
//I assign to my array
$array = computePermutations($array);
I thing something maybe goes wrong here ?
but when I'm trying to update my array like :
$array[0][0]->details[0]->priority = 3;
It update all the first priority field, (see the array before) this is after the update.
Meaning:
$array[0][0]->details[0]->priority
$array[1][0]->details[0]->priority
$array[2][0]->details[0]->priority
etc... becomes 3
Any idea where this can come from, is there any other way to update a multidimensional array? Am I doing something wrong here?

Combining array of array with another array in PHP using array_combine

I'm playing around with array functions to get a better understanding.
Below I have three arrays:
$cardNumber = array(2, 4, 5, 8, 9);
$playerName = array('Julian', 'Brad', 'Chloe', 'Laura', 'Paul');
$playerWins = array(4, 5, 1, 2, 6);
I am trying to achieve the following array structure:
array (size=5)
2 =>
array (size=2)
0 => string 'Julian' (length=6)
1 => int 4
4 =>
array (size=2)
0 => string 'Brad' (length=4)
1 => int 5
5 =>
array (size=2)
0 => string 'Chloe' (length=5)
1 => int 1
8 =>
array (size=2)
0 => string 'Laura' (length=5)
1 => int 2
9 =>
array (size=2)
0 => string 'Paul' (length=4)
1 => int 6
I decided to combine $playerName and $playerWins with array_combine(), and then combine the resulting array with $cardNumber, which does not yield the output I expect. Am I understanding array_combine() incorrectly?
You can use array_map with null as a callback and the two data arrays to give you the array structure that you want. Then combine that with the array for the keys using array_combine:
$result = array_combine($cardNumber, array_map(null, $playerName, $playerWins));
For illustration:
$result = array_map(null, $playerName, $playerWins);
Yields:
Array
(
[0] => Array
(
[0] => Julian
[1] => 4
)
[1] => Array
(
[0] => Brad
[1] => 5
)
[2] => Array
(
[0] => Chloe
[1] => 1
)
[3] => Array
(
[0] => Laura
[1] => 2
)
[4] => Array
(
[0] => Paul
[1] => 6
)
)
Then combine to get the keys:
$result = array_combine($cardNumber, $result);
Yields:
Array
(
[2] => Array
(
[0] => Julian
[1] => 4
)
[4] => Array
(
[0] => Brad
[1] => 5
)
[5] => Array
(
[0] => Chloe
[1] => 1
)
[8] => Array
(
[0] => Laura
[1] => 2
)
[9] => Array
(
[0] => Paul
[1] => 6
)
)

How to construct sub arrays from a main array

I would like to construct sub n number of arrays from a multi dimensional array depends on the data. For example: I have a main array as
Array
(
[0] => Array
(
[id] => 1
[status] => -1
)
[1] => Array
(
[id] => 2
[status] => 1
)
[2] => Array
(
[id] => 3
[status] => 2
)
[3] => Array
(
[id] => 4
[status] => 2
)
[4] => Array
(
[id] => 5
[status] => 2
)
)
I would like to get 3 arrays from this array depends on the no and type of status value
like
array{
[0]=array(
[status]=-1
[count]=1
)
[1]=array(
[status]=1
[count]=1
)
[2]=array(
[status]=2
[count]=3
)
}
Thanks in advance,
Sunil Kumar P
You mean like this?:
$array = array(
array('id' => 1, 'status' => -1),
array('id' => 2, 'status' => 1),
array('id' => 3, 'status' => 2),
array('id' => 4, 'status' => 2),
array('id' => 5, 'status' => 2)
);
$statuses = array();
foreach($array as $one){
if(!isset($statuses[$one['status']])){ $statuses[$one['status']] = 0; }
$statuses[$one['status']]++;
}
$newArray = array();
foreach($statuses as $key => $val){
$newArray[] = array('status' => $key, 'count' => $val);
}
print_r($newArray);
/*Array
(
[0] => Array
(
[status] => -1
[count] => 1
)
[1] => Array
(
[status] => 1
[count] => 1
)
[2] => Array
(
[status] => 2
[count] => 3
)
)*/

Categories