How to push array in PHP [closed]
I want to put data from Query into array, but this code is not working. can you explain the right way ? thanks
$sql = "SELECT * FROM populasi WHERE blablabla";
$result = $con->query($sql);
$tampung = array();
foreach ($result as $m) {
$tampung = array_push($tampung, "new google.maps.LatLng($m[x], $m[y]), ");
}
echo $tampung;
php arrays
closed as off-topic by Alon Eitan, RiggsFolly, Michael Dodd, jww, Pearly Spencer Nov 14 '18 at 16:53
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Alon Eitan, RiggsFolly, Michael Dodd, jww, Pearly Spencer
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I want to put data from Query into array, but this code is not working. can you explain the right way ? thanks
$sql = "SELECT * FROM populasi WHERE blablabla";
$result = $con->query($sql);
$tampung = array();
foreach ($result as $m) {
$tampung = array_push($tampung, "new google.maps.LatLng($m[x], $m[y]), ");
}
echo $tampung;
php arrays
closed as off-topic by Alon Eitan, RiggsFolly, Michael Dodd, jww, Pearly Spencer Nov 14 '18 at 16:53
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Alon Eitan, RiggsFolly, Michael Dodd, jww, Pearly Spencer
If this question can be reworded to fit the rules in the help center, please edit the question.
Read about explode() (Although it looks wrong overall I must say)
– Alon Eitan
Nov 14 '18 at 10:26
3
array_push()returns an integer so by doing$tampung = array_push( ... )you're reassigning$tampungto an(int)... ditch the$tampung =bit.
– CD001
Nov 14 '18 at 10:28
1
OR a simple$tampung = "new google.maps.LatLng($m[x], $m[y]), "
– RiggsFolly
Nov 14 '18 at 10:29
1
As manual suggests it is quicker if you are only adding one item to the array to use$x = $y;syntax overarray_push()as you then dont need to call a function
– RiggsFolly
Nov 14 '18 at 10:31
Best way to push some element into your array is$tampung = "new google.maps.LatLng($m[x], $m[y]),". This method is faster then usingarray_push(). To print array, please useprint_r()orvar_dump(). If you want to print pretty formated array and you didn't installedXDebuguseecho <pre>; print_r($yourArrayHere);. This small construction will prettify array output and will help you to read it.
– alexey-novikov
Nov 14 '18 at 10:39
add a comment |
I want to put data from Query into array, but this code is not working. can you explain the right way ? thanks
$sql = "SELECT * FROM populasi WHERE blablabla";
$result = $con->query($sql);
$tampung = array();
foreach ($result as $m) {
$tampung = array_push($tampung, "new google.maps.LatLng($m[x], $m[y]), ");
}
echo $tampung;
php arrays
I want to put data from Query into array, but this code is not working. can you explain the right way ? thanks
$sql = "SELECT * FROM populasi WHERE blablabla";
$result = $con->query($sql);
$tampung = array();
foreach ($result as $m) {
$tampung = array_push($tampung, "new google.maps.LatLng($m[x], $m[y]), ");
}
echo $tampung;
php arrays
php arrays
asked Nov 14 '18 at 10:24
Ehsar D. M.Ehsar D. M.
61
61
closed as off-topic by Alon Eitan, RiggsFolly, Michael Dodd, jww, Pearly Spencer Nov 14 '18 at 16:53
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Alon Eitan, RiggsFolly, Michael Dodd, jww, Pearly Spencer
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Alon Eitan, RiggsFolly, Michael Dodd, jww, Pearly Spencer Nov 14 '18 at 16:53
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Alon Eitan, RiggsFolly, Michael Dodd, jww, Pearly Spencer
If this question can be reworded to fit the rules in the help center, please edit the question.
Read about explode() (Although it looks wrong overall I must say)
– Alon Eitan
Nov 14 '18 at 10:26
3
array_push()returns an integer so by doing$tampung = array_push( ... )you're reassigning$tampungto an(int)... ditch the$tampung =bit.
– CD001
Nov 14 '18 at 10:28
1
OR a simple$tampung = "new google.maps.LatLng($m[x], $m[y]), "
– RiggsFolly
Nov 14 '18 at 10:29
1
As manual suggests it is quicker if you are only adding one item to the array to use$x = $y;syntax overarray_push()as you then dont need to call a function
– RiggsFolly
Nov 14 '18 at 10:31
Best way to push some element into your array is$tampung = "new google.maps.LatLng($m[x], $m[y]),". This method is faster then usingarray_push(). To print array, please useprint_r()orvar_dump(). If you want to print pretty formated array and you didn't installedXDebuguseecho <pre>; print_r($yourArrayHere);. This small construction will prettify array output and will help you to read it.
– alexey-novikov
Nov 14 '18 at 10:39
add a comment |
Read about explode() (Although it looks wrong overall I must say)
– Alon Eitan
Nov 14 '18 at 10:26
3
array_push()returns an integer so by doing$tampung = array_push( ... )you're reassigning$tampungto an(int)... ditch the$tampung =bit.
– CD001
Nov 14 '18 at 10:28
1
OR a simple$tampung = "new google.maps.LatLng($m[x], $m[y]), "
– RiggsFolly
Nov 14 '18 at 10:29
1
As manual suggests it is quicker if you are only adding one item to the array to use$x = $y;syntax overarray_push()as you then dont need to call a function
– RiggsFolly
Nov 14 '18 at 10:31
Best way to push some element into your array is$tampung = "new google.maps.LatLng($m[x], $m[y]),". This method is faster then usingarray_push(). To print array, please useprint_r()orvar_dump(). If you want to print pretty formated array and you didn't installedXDebuguseecho <pre>; print_r($yourArrayHere);. This small construction will prettify array output and will help you to read it.
– alexey-novikov
Nov 14 '18 at 10:39
Read about explode() (Although it looks wrong overall I must say)
– Alon Eitan
Nov 14 '18 at 10:26
Read about explode() (Although it looks wrong overall I must say)
– Alon Eitan
Nov 14 '18 at 10:26
3
3
array_push() returns an integer so by doing $tampung = array_push( ... ) you're reassigning $tampung to an (int) ... ditch the $tampung = bit.– CD001
Nov 14 '18 at 10:28
array_push() returns an integer so by doing $tampung = array_push( ... ) you're reassigning $tampung to an (int) ... ditch the $tampung = bit.– CD001
Nov 14 '18 at 10:28
1
1
OR a simple
$tampung = "new google.maps.LatLng($m[x], $m[y]), "– RiggsFolly
Nov 14 '18 at 10:29
OR a simple
$tampung = "new google.maps.LatLng($m[x], $m[y]), "– RiggsFolly
Nov 14 '18 at 10:29
1
1
As manual suggests it is quicker if you are only adding one item to the array to use
$x = $y; syntax over array_push() as you then dont need to call a function– RiggsFolly
Nov 14 '18 at 10:31
As manual suggests it is quicker if you are only adding one item to the array to use
$x = $y; syntax over array_push() as you then dont need to call a function– RiggsFolly
Nov 14 '18 at 10:31
Best way to push some element into your array is
$tampung = "new google.maps.LatLng($m[x], $m[y]),". This method is faster then using array_push(). To print array, please use print_r() or var_dump(). If you want to print pretty formated array and you didn't installed XDebug use echo <pre>; print_r($yourArrayHere);. This small construction will prettify array output and will help you to read it.– alexey-novikov
Nov 14 '18 at 10:39
Best way to push some element into your array is
$tampung = "new google.maps.LatLng($m[x], $m[y]),". This method is faster then using array_push(). To print array, please use print_r() or var_dump(). If you want to print pretty formated array and you didn't installed XDebug use echo <pre>; print_r($yourArrayHere);. This small construction will prettify array output and will help you to read it.– alexey-novikov
Nov 14 '18 at 10:39
add a comment |
2 Answers
2
active
oldest
votes
you can do it like this
foreach ($result as $m) {
$tampung = 'new google.maps.LatLng('.$m[x].', '.$m[y].')';
}
var_dump($tampung);
You can not echo array, use var_dump($arr) or print_r($arr) instead
We dont normally answer TYPO's we just make a simple comment and close the question as a typo.
– RiggsFolly
Nov 14 '18 at 10:32
add a comment |
You are just executing the query, you have not fetched it.
Fetch it to an array first :
$data=$result->fetch_assoc();
than you can use array_push function
Assuming this ismysqli- the question never specifies what$conis so it could be a custom database wrapper where thequery()method returns an iterable recordset ... unlikely but it could be.
– CD001
Nov 14 '18 at 10:34
To make a complete answer you really should also mention the other error i.e. the incorrect use ofarray_push()
– RiggsFolly
Nov 14 '18 at 10:35
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
you can do it like this
foreach ($result as $m) {
$tampung = 'new google.maps.LatLng('.$m[x].', '.$m[y].')';
}
var_dump($tampung);
You can not echo array, use var_dump($arr) or print_r($arr) instead
We dont normally answer TYPO's we just make a simple comment and close the question as a typo.
– RiggsFolly
Nov 14 '18 at 10:32
add a comment |
you can do it like this
foreach ($result as $m) {
$tampung = 'new google.maps.LatLng('.$m[x].', '.$m[y].')';
}
var_dump($tampung);
You can not echo array, use var_dump($arr) or print_r($arr) instead
We dont normally answer TYPO's we just make a simple comment and close the question as a typo.
– RiggsFolly
Nov 14 '18 at 10:32
add a comment |
you can do it like this
foreach ($result as $m) {
$tampung = 'new google.maps.LatLng('.$m[x].', '.$m[y].')';
}
var_dump($tampung);
You can not echo array, use var_dump($arr) or print_r($arr) instead
you can do it like this
foreach ($result as $m) {
$tampung = 'new google.maps.LatLng('.$m[x].', '.$m[y].')';
}
var_dump($tampung);
You can not echo array, use var_dump($arr) or print_r($arr) instead
answered Nov 14 '18 at 10:31
Oleksandr PobutaOleksandr Pobuta
32429
32429
We dont normally answer TYPO's we just make a simple comment and close the question as a typo.
– RiggsFolly
Nov 14 '18 at 10:32
add a comment |
We dont normally answer TYPO's we just make a simple comment and close the question as a typo.
– RiggsFolly
Nov 14 '18 at 10:32
We dont normally answer TYPO's we just make a simple comment and close the question as a typo.
– RiggsFolly
Nov 14 '18 at 10:32
We dont normally answer TYPO's we just make a simple comment and close the question as a typo.
– RiggsFolly
Nov 14 '18 at 10:32
add a comment |
You are just executing the query, you have not fetched it.
Fetch it to an array first :
$data=$result->fetch_assoc();
than you can use array_push function
Assuming this ismysqli- the question never specifies what$conis so it could be a custom database wrapper where thequery()method returns an iterable recordset ... unlikely but it could be.
– CD001
Nov 14 '18 at 10:34
To make a complete answer you really should also mention the other error i.e. the incorrect use ofarray_push()
– RiggsFolly
Nov 14 '18 at 10:35
add a comment |
You are just executing the query, you have not fetched it.
Fetch it to an array first :
$data=$result->fetch_assoc();
than you can use array_push function
Assuming this ismysqli- the question never specifies what$conis so it could be a custom database wrapper where thequery()method returns an iterable recordset ... unlikely but it could be.
– CD001
Nov 14 '18 at 10:34
To make a complete answer you really should also mention the other error i.e. the incorrect use ofarray_push()
– RiggsFolly
Nov 14 '18 at 10:35
add a comment |
You are just executing the query, you have not fetched it.
Fetch it to an array first :
$data=$result->fetch_assoc();
than you can use array_push function
You are just executing the query, you have not fetched it.
Fetch it to an array first :
$data=$result->fetch_assoc();
than you can use array_push function
edited Nov 14 '18 at 10:33
RiggsFolly
70.7k1864111
70.7k1864111
answered Nov 14 '18 at 10:32
xhuljoxhuljo
524
524
Assuming this ismysqli- the question never specifies what$conis so it could be a custom database wrapper where thequery()method returns an iterable recordset ... unlikely but it could be.
– CD001
Nov 14 '18 at 10:34
To make a complete answer you really should also mention the other error i.e. the incorrect use ofarray_push()
– RiggsFolly
Nov 14 '18 at 10:35
add a comment |
Assuming this ismysqli- the question never specifies what$conis so it could be a custom database wrapper where thequery()method returns an iterable recordset ... unlikely but it could be.
– CD001
Nov 14 '18 at 10:34
To make a complete answer you really should also mention the other error i.e. the incorrect use ofarray_push()
– RiggsFolly
Nov 14 '18 at 10:35
Assuming this is
mysqli - the question never specifies what $con is so it could be a custom database wrapper where the query() method returns an iterable recordset ... unlikely but it could be.– CD001
Nov 14 '18 at 10:34
Assuming this is
mysqli - the question never specifies what $con is so it could be a custom database wrapper where the query() method returns an iterable recordset ... unlikely but it could be.– CD001
Nov 14 '18 at 10:34
To make a complete answer you really should also mention the other error i.e. the incorrect use of
array_push()– RiggsFolly
Nov 14 '18 at 10:35
To make a complete answer you really should also mention the other error i.e. the incorrect use of
array_push()– RiggsFolly
Nov 14 '18 at 10:35
add a comment |
Read about explode() (Although it looks wrong overall I must say)
– Alon Eitan
Nov 14 '18 at 10:26
3
array_push()returns an integer so by doing$tampung = array_push( ... )you're reassigning$tampungto an(int)... ditch the$tampung =bit.– CD001
Nov 14 '18 at 10:28
1
OR a simple
$tampung = "new google.maps.LatLng($m[x], $m[y]), "– RiggsFolly
Nov 14 '18 at 10:29
1
As manual suggests it is quicker if you are only adding one item to the array to use
$x = $y;syntax overarray_push()as you then dont need to call a function– RiggsFolly
Nov 14 '18 at 10:31
Best way to push some element into your array is
$tampung = "new google.maps.LatLng($m[x], $m[y]),". This method is faster then usingarray_push(). To print array, please useprint_r()orvar_dump(). If you want to print pretty formated array and you didn't installedXDebuguseecho <pre>; print_r($yourArrayHere);. This small construction will prettify array output and will help you to read it.– alexey-novikov
Nov 14 '18 at 10:39