Reputation: 2422
Suppose i have the following data.
Now i am trying to group all the values of each group in one array.
Suppose $temp_array contaib all of the results that i have. Now i am trying to group all the data based on the id-
So i am trying to do this ---
array(
values (
id -225
name1= hello
name2= 45
name3= burger
,
id -225
name1= bacon
name2= cheese))
so i have tried like so---
$count=count($temp_array); // $temp_array contains all results
foreach ($temp_array as $key => $var) {
for ($j = 0; $j < count($count); ++$j) {
$data = array();
$data['id'] = $var[$j]['id'];
$data['name'] = $var[$j]['name'];
}}
but the problem is it is only looping one time, means the [0]
value.
so i get the result like so ---
array(
values (
id -225
name1= hello
name2= 0
name3= 0
,
id -225
name1= bacon
name2= 0
...))
my results ---
Array
(
[0] => Array // group number 0
(
[0] => Array
([id] => 225
[name] => hello
)
[1] => Array
(
[id] => 225
name] => ham
)
[2] => Array
(
[id] => 225
)
[3] => Array
(
[id] => 225
name] => burger
)
[1] => Array // group number 1
(
[0] => Array
(
[id] => 45
name] => bacon
)
[1] => Array
(
[id] => 45
name] => cheese
)
can someone try to help me to fix this problem
Upvotes: 1
Views: 91
Reputation: 1289
Your array is incorrect. Your output is this:
Array
(
[0] => Array // group number 0
(
[0] => Array
([id] => 225
[name] => hello
)
[1] => Array
(
[id] => 225
name] => ham
)
[2] => Array
(
[id] => 225
)
[3] => Array
(
[id] => 225
name] => burger
) // you need to close this array [0] with another ),
[1] => Array // group number 1
(
[0] => Array
(
[id] => 45
name] => bacon
)
[1] => Array
(
[id] => 45
name] => cheese
i've edited the array and tested it with the code you've made and it worked fine. Build it like this:
$array = Array
(
'0' => Array // group number 0
(
'0' => Array
('id' => 225,
'name' => 'hello'
),
'1' => Array
(
'id' => 225,
'name' => 'ham'
),
'2' => Array
(
'id' => 225
),
'3' => Array
(
'id' => 225,
'name' => 'burger'
)),
'1' => Array // group number 1
(
'0' => Array
('id' => 222,
'name' => 'test'
),
'1' => Array
(
'id' => 225,
'name' => 'ham'
),
'2' => Array
(
'id' => 225
)));
and also what they have mentioned before
for ($j = 0; $j < $count; ++$j) {
$data = array();
$data['id'] = $var[$j]['id'];
$data['name'] = $var[$j]['name'];
print_r($data);
}}
Upvotes: -1
Reputation: 217
As its inside of an foreach you should concanate your output: http://php.net/manual/en/language.operators.string.php
$count=count($temp_array); // $temp_array contains all results
$data = '';
foreach ($temp_array as $key => $var) {
for ($j = 0; $j < count($count); ++$j) {
$data .= array();
$data['id'] .= $var[$j]['id'];
$data['name'] .= $var[$j]['name'];
}}
var_dump($data);
Upvotes: -1
Reputation: 23958
$count
is an integer.
If you count($count)
, it will count string length of $count
which is 1.
count()
function works differently for different data types.
If you pass any integer value to it, it will automatically type cast it to string and will count length.
For strings, it counts number of characters in it.
For Arrays, it counts number of elements in it.
In your case, you were getting string length (which is 1) instead of array elements count.
Change
for ($j = 0; $j < count($count); ++$j) {
To
for ($j = 0; $j < $count; ++$j) {
Upvotes: 1
Reputation: 996
$count 's value is an integer so you are apllying an extra count() function
replace the
for ($j = 0; $j < count($count); ++$j) {
with
for ($j = 0; $j < $count; ++$j) {
Also you can try like this:
$data = array();
foreach($tempArrray as $row) {
$data[$row['id']][] = $row;
}
Upvotes: 4