Add values of two different php arrays
up vote
0
down vote
favorite
My question is about how to add the values from two different arrays, that each have 20 records.
$array1
Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 220
[type] => 0
)
[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 70
[type] => 0
)
$array2
Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 280
[type] => 0
)
[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 30
[type] => 0
)
I would like to get a third array like that (adding the values of $array1 and $array2):
Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 500
[type] => 0
)
[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 100
[type] => 0
)
How do I do this? All operations I've tried only added the values one after another (new array with 40 rows), not adding them.
EDIT SOLUTION
$array3 = array();
for ($i = 0; $i < count($array2); $i++) {
array_push($array3, $array1[$i]->num_rows + $array2[$i]->num_rows);
}
php arrays
add a comment |
up vote
0
down vote
favorite
My question is about how to add the values from two different arrays, that each have 20 records.
$array1
Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 220
[type] => 0
)
[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 70
[type] => 0
)
$array2
Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 280
[type] => 0
)
[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 30
[type] => 0
)
I would like to get a third array like that (adding the values of $array1 and $array2):
Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 500
[type] => 0
)
[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 100
[type] => 0
)
How do I do this? All operations I've tried only added the values one after another (new array with 40 rows), not adding them.
EDIT SOLUTION
$array3 = array();
for ($i = 0; $i < count($array2); $i++) {
array_push($array3, $array1[$i]->num_rows + $array2[$i]->num_rows);
}
php arrays
Looks like your trying to add the count of rows in two SQL queries.
– Nigel Ren
Nov 10 at 18:54
70 + 30 = 100 not 1000
– Madhur Bhaiya
Nov 10 at 18:54
4
Better to do this in MySQL than PHP
– symlink
Nov 10 at 18:57
1
This is XY problem. Somewhere else something is incorrect.
– u_mulder
Nov 10 at 19:03
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
My question is about how to add the values from two different arrays, that each have 20 records.
$array1
Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 220
[type] => 0
)
[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 70
[type] => 0
)
$array2
Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 280
[type] => 0
)
[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 30
[type] => 0
)
I would like to get a third array like that (adding the values of $array1 and $array2):
Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 500
[type] => 0
)
[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 100
[type] => 0
)
How do I do this? All operations I've tried only added the values one after another (new array with 40 rows), not adding them.
EDIT SOLUTION
$array3 = array();
for ($i = 0; $i < count($array2); $i++) {
array_push($array3, $array1[$i]->num_rows + $array2[$i]->num_rows);
}
php arrays
My question is about how to add the values from two different arrays, that each have 20 records.
$array1
Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 220
[type] => 0
)
[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 70
[type] => 0
)
$array2
Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 280
[type] => 0
)
[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 30
[type] => 0
)
I would like to get a third array like that (adding the values of $array1 and $array2):
Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 500
[type] => 0
)
[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 100
[type] => 0
)
How do I do this? All operations I've tried only added the values one after another (new array with 40 rows), not adding them.
EDIT SOLUTION
$array3 = array();
for ($i = 0; $i < count($array2); $i++) {
array_push($array3, $array1[$i]->num_rows + $array2[$i]->num_rows);
}
php arrays
php arrays
edited Nov 10 at 19:45
asked Nov 10 at 18:53
Lama222
82
82
Looks like your trying to add the count of rows in two SQL queries.
– Nigel Ren
Nov 10 at 18:54
70 + 30 = 100 not 1000
– Madhur Bhaiya
Nov 10 at 18:54
4
Better to do this in MySQL than PHP
– symlink
Nov 10 at 18:57
1
This is XY problem. Somewhere else something is incorrect.
– u_mulder
Nov 10 at 19:03
add a comment |
Looks like your trying to add the count of rows in two SQL queries.
– Nigel Ren
Nov 10 at 18:54
70 + 30 = 100 not 1000
– Madhur Bhaiya
Nov 10 at 18:54
4
Better to do this in MySQL than PHP
– symlink
Nov 10 at 18:57
1
This is XY problem. Somewhere else something is incorrect.
– u_mulder
Nov 10 at 19:03
Looks like your trying to add the count of rows in two SQL queries.
– Nigel Ren
Nov 10 at 18:54
Looks like your trying to add the count of rows in two SQL queries.
– Nigel Ren
Nov 10 at 18:54
70 + 30 = 100 not 1000
– Madhur Bhaiya
Nov 10 at 18:54
70 + 30 = 100 not 1000
– Madhur Bhaiya
Nov 10 at 18:54
4
4
Better to do this in MySQL than PHP
– symlink
Nov 10 at 18:57
Better to do this in MySQL than PHP
– symlink
Nov 10 at 18:57
1
1
This is XY problem. Somewhere else something is incorrect.
– u_mulder
Nov 10 at 19:03
This is XY problem. Somewhere else something is incorrect.
– u_mulder
Nov 10 at 19:03
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
One possible way to do that could be using a for loop and use the index of the arrays to add the num_rows of $array1
to $array2
:
for ($i = 0; $i < count($array1); $i++) {
$array2[$i]->num_rows += $array1[$i]->num_rows;
}
this way the values stayed the same
– Lama222
Nov 10 at 19:38
@Lama222 Did you run aprint_r($array2);
For example lik 3v4l.org/J8LJd
– The fourth bird
Nov 10 at 19:39
1
Thanks, got it working with your solution
– Lama222
Nov 10 at 19:44
add a comment |
up vote
0
down vote
if you want to add every single value of an array($array1) to another array($array3) you need to take a foreach like this :
foreach($array1 as $array) {
$array3 = $array;
}
add a comment |
up vote
0
down vote
Try
$resultArray = array_merge($array1,$array2);
this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
– Lama222
Nov 10 at 19:37
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Stephen C
Nov 11 at 7:02
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
One possible way to do that could be using a for loop and use the index of the arrays to add the num_rows of $array1
to $array2
:
for ($i = 0; $i < count($array1); $i++) {
$array2[$i]->num_rows += $array1[$i]->num_rows;
}
this way the values stayed the same
– Lama222
Nov 10 at 19:38
@Lama222 Did you run aprint_r($array2);
For example lik 3v4l.org/J8LJd
– The fourth bird
Nov 10 at 19:39
1
Thanks, got it working with your solution
– Lama222
Nov 10 at 19:44
add a comment |
up vote
0
down vote
accepted
One possible way to do that could be using a for loop and use the index of the arrays to add the num_rows of $array1
to $array2
:
for ($i = 0; $i < count($array1); $i++) {
$array2[$i]->num_rows += $array1[$i]->num_rows;
}
this way the values stayed the same
– Lama222
Nov 10 at 19:38
@Lama222 Did you run aprint_r($array2);
For example lik 3v4l.org/J8LJd
– The fourth bird
Nov 10 at 19:39
1
Thanks, got it working with your solution
– Lama222
Nov 10 at 19:44
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
One possible way to do that could be using a for loop and use the index of the arrays to add the num_rows of $array1
to $array2
:
for ($i = 0; $i < count($array1); $i++) {
$array2[$i]->num_rows += $array1[$i]->num_rows;
}
One possible way to do that could be using a for loop and use the index of the arrays to add the num_rows of $array1
to $array2
:
for ($i = 0; $i < count($array1); $i++) {
$array2[$i]->num_rows += $array1[$i]->num_rows;
}
answered Nov 10 at 19:16
The fourth bird
18.2k71323
18.2k71323
this way the values stayed the same
– Lama222
Nov 10 at 19:38
@Lama222 Did you run aprint_r($array2);
For example lik 3v4l.org/J8LJd
– The fourth bird
Nov 10 at 19:39
1
Thanks, got it working with your solution
– Lama222
Nov 10 at 19:44
add a comment |
this way the values stayed the same
– Lama222
Nov 10 at 19:38
@Lama222 Did you run aprint_r($array2);
For example lik 3v4l.org/J8LJd
– The fourth bird
Nov 10 at 19:39
1
Thanks, got it working with your solution
– Lama222
Nov 10 at 19:44
this way the values stayed the same
– Lama222
Nov 10 at 19:38
this way the values stayed the same
– Lama222
Nov 10 at 19:38
@Lama222 Did you run a
print_r($array2);
For example lik 3v4l.org/J8LJd– The fourth bird
Nov 10 at 19:39
@Lama222 Did you run a
print_r($array2);
For example lik 3v4l.org/J8LJd– The fourth bird
Nov 10 at 19:39
1
1
Thanks, got it working with your solution
– Lama222
Nov 10 at 19:44
Thanks, got it working with your solution
– Lama222
Nov 10 at 19:44
add a comment |
up vote
0
down vote
if you want to add every single value of an array($array1) to another array($array3) you need to take a foreach like this :
foreach($array1 as $array) {
$array3 = $array;
}
add a comment |
up vote
0
down vote
if you want to add every single value of an array($array1) to another array($array3) you need to take a foreach like this :
foreach($array1 as $array) {
$array3 = $array;
}
add a comment |
up vote
0
down vote
up vote
0
down vote
if you want to add every single value of an array($array1) to another array($array3) you need to take a foreach like this :
foreach($array1 as $array) {
$array3 = $array;
}
if you want to add every single value of an array($array1) to another array($array3) you need to take a foreach like this :
foreach($array1 as $array) {
$array3 = $array;
}
answered Nov 10 at 18:59
k-kaufmann
1
1
add a comment |
add a comment |
up vote
0
down vote
Try
$resultArray = array_merge($array1,$array2);
this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
– Lama222
Nov 10 at 19:37
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Stephen C
Nov 11 at 7:02
add a comment |
up vote
0
down vote
Try
$resultArray = array_merge($array1,$array2);
this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
– Lama222
Nov 10 at 19:37
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Stephen C
Nov 11 at 7:02
add a comment |
up vote
0
down vote
up vote
0
down vote
Try
$resultArray = array_merge($array1,$array2);
Try
$resultArray = array_merge($array1,$array2);
answered Nov 10 at 19:19
doron
1
1
this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
– Lama222
Nov 10 at 19:37
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Stephen C
Nov 11 at 7:02
add a comment |
this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
– Lama222
Nov 10 at 19:37
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Stephen C
Nov 11 at 7:02
this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
– Lama222
Nov 10 at 19:37
this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
– Lama222
Nov 10 at 19:37
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Stephen C
Nov 11 at 7:02
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– Stephen C
Nov 11 at 7:02
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242336%2fadd-values-of-two-different-php-arrays%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Looks like your trying to add the count of rows in two SQL queries.
– Nigel Ren
Nov 10 at 18:54
70 + 30 = 100 not 1000
– Madhur Bhaiya
Nov 10 at 18:54
4
Better to do this in MySQL than PHP
– symlink
Nov 10 at 18:57
1
This is XY problem. Somewhere else something is incorrect.
– u_mulder
Nov 10 at 19:03