I'm newbie to PHP and trying to get a string from mysql query result.
I'm run this code:
$results = $query->rows;
echo '<pre>';
print_r($results);
echo '</pre>';
And get this result:
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => Array
(
[location] => someaddress
)
)
How can I get 'someaddress' to string?
foreach($results as $key=>$val){
echo $val['location'] . "<br/>";
}
no need to convert to string.simply iterate to the loop and store all location to some array if more are there.
Try this
foreach($results as $key => $value)
{
echo $value['location'] . "<br/>";
}
Related
I have a php array value but i don't know how to print it. there is my php code
$json = array();
if($sql->num_rows > 0){
while($result = $sql->fetch_assoc()) {
$json[] = $result;
}
}
And here is my array value
Array
(
[0] => Array
(
[locationid] => 1
[locationname] => Anantapur
[locationvalue] => 1.1
)
[1] => Array
(
[locationid] => 2
[locationname] => Guntakal
[locationvalue] => 1.2
)
[2] => Array
(
[locationid] => 4
[locationname] => Guntur
[locationvalue] => 1.3
)
)
I'm Trying like
echo $json['locationname'];
But It's Not working please help me
You need to loop through your array first. Try foreach
foreach($json as $j){
echo $j['locationname'];
}
You have not need to make a new array after get the result. you can print your value within the loop
if($sql->num_rows > 0){
while($result = $sql->fetch_assoc()) {
echo $result['locationaname'];
}
}
The array you created is associative array. For loo through these type of arrays requires foreach. Visit http://php.net/manual/en/control-structures.foreach.php
foreach($json AS $key =>$value){
echo $value['locationname'];
}
You can directly print the value of location, instead of creating a new array, unless you need it further.
if($sql->num_rows > 0){
while($result = $sql->fetch_assoc()) {
echo $result['locationname'];
}
}
if you want to have $json, which is an associative array, you need to get into it, looping through it.
foreach($json as $key => $value){
echo $value['locationname'];
}
If you want json string try this:
echo json_encode($json);
after your loop.
i am trying to print multidimensional array in php below is my array and array variable is gplus
Array ( [0] => Array ( [name] => krishna sitaram [email] => kasyapa25#gmail.com )
[1] => Array ( [name] => ravi kumar [email] => ravikumar#gmail.com )
i tried bellow
foreach($gplus as $gvalue){
echo $gvalue."<br />";
}
but not working.
the result should be
name email
krishna sitaram kasyapa25#gmail.com
ravi kumar ravikumar#gmail.com
Thank you all i got the the correct solution is
foreach($gplus as $array){
echo $array['name']."<br />";
echo $array['email']."<br />";
}
If you're just debugging:
print_r($your_associative_array);
If you want to print it:
foreach($gplus as $array){
foreach($array as $key=>$value){
echo "Key: $key / Value: $value<br />";
}
}
foreach ($gplus as $k => $gvalue) {
echo $gvalue["name"]." ".$gvalue["email"]."<br />";
}
I know a little late but you can do also this, if you like to use the var_dump function for debug information:
ob_start();
var_dump($array);
$array_content_string = ob_get_contents();
ob_end_clean();
you can use this $array_content_string as normal string
first your might know that you have a assoc array not a simple array , to display your assoc array you have to write :
foreach ( $arrayRow as $key => $value)
{
echo $value;
}
I have a json array, looks like:
Array (
[0] => Array
(
[k1] => aaa
[k2] => aaa
[kTypes] => Array
(
[ktype1] => Array
(
[desc] => asd
)
[ktype2] => Array
(
[desc] => asd
)
)
)
And I try to get the desc values inside ktypes, tried this:
$items = $myArray;
// echo "<pre>";
// print_r($items);
// echo "</pre>";
echo '<table>';
echo '<tr><th>k1</th><th>k2</th><th>ktype1</th><th>ktype2</th></tr>';
foreach($items as $item)
{
echo "<tr><td>$item[k1]</td><td>$item[k2]</td><td>$item[kTypes][kType1][desc]</td><td>$item[kTypes][kType2][desc]</td>";
}
echo '</table>';
which works fine for the first both columns, but not the ktype ones. There the:
echo is "Array[kType2][desc]"
So I tried a nested loop, but this didn't work, too.
can anyone help me on the right track?
To interpolate multidimensional array accesses in a string, you have to use the "complex" format with curly braces.
echo "<tr><td>$item[k1]</td><td>$item[k2]</td><td>{$item['kTypes']['kType1']['desc']}</td><td>{$item['kTypes']['kType2']['desc']}</td>";
Try this foreach for your specific piece:
echo "<tr>";
foreach ($items as $field => $value) {
if($field =='ktype'} {
foreach($items['ktype'] as $ktype) {
foreach($ktype as $k) {
echo "<td>{$k}</td>";
}
}
} else {
echo "<td>{$value}</td>";
}
}
echo "</tr>";
However, I would do a recursive piece so it can go as deep into the array as possible.
Below is the dump that I got from mongo. I need to fetch the opening artist name.
Array
(
[_id] => MongoId Object
(
[$id] => 51c9b63b6f7cb5f8229f27b7
)
[s20] => Array
(
[opening] => Array
(
[artist] => Array
(
[name] => Jay Z
)
[music] => Array
(
[name] => 99 problems
)
)
)
So, I tried:
foreach($mongo_dump as $key=>$value){
echo "<pre>KEY: " . print_r($key["s20"]["opening"]["artist"]["name"]) . "</pre>"; // line # 16
echo "<pre>VALUE: " . print_r($value) . "</pre>";
echo "\n\n";
}
However, I did not get the artist name. I received the following PHP warning:
PHP Warning: Illegal string offset 's20' in /var/www/Code/analytics/fetch_top_5_opening_artists.php on line 16
As Blaine mentions, $key isn't an array. The way that you are traversing the dump is incorrect. $key becomes a string in the context of the foreach loop. Try doing something like this:
if ($key == "s20") {
echo "<pre>KEY: " . print_r($value["opening"]["artist"]["name"]) . "</pre>";
}
the value itself is array() so your forloop is not going work unless you setupup nested. Here is example of neted for loop.
foreach($mongo_dump as $key )
{
{
foreach($key as $subkey)
{
echo $subkey
echo "\n\n";
}
}
I'm trying to print array. All code working fine.But at last I'm getting `ArrayArray'. Can any one solve this problem. many many thanks
here is my array
Array
(
[Post1] => Array
(
[id] => 1
[title] => hi
)
[Post2] => Array
(
[0] => Array
(
[id] => 1
)
)
[Post3] => Array
(
[0] => Array
(
[id] => 1
)
)
)
Here is my PHP Code
foreach($post as $key => $value) {
foreach($value as $print => $key) {
echo "<br>".$key;
}
}
here is output
ID
Array
Array
Try this:
foreach($post as $key => $value) {
foreach($value as $print => $key) {
if (is_array($key)){
foreach($key as $print2 => $key2) {
echo "<br>".$key2;
}
}else{
echo "<br>".$key;
}
}
}
The to string method of an array is to return "Array".
It sounds like you want to view the array for debugging purposes. var_dump() is your friend :)
you are trying to print an array, resulting in Array.
If you want to print an array use print_r
I think the trouble for you is that you have $key in the outer loop and $key in the inner loop so its really confusing which $key you are talking about for starters.
You just want the stuff printed out to debug?
echo "<pre>" . print_r( $post , true ) . "</pre>\n";