I am trying to work with this script and get it to echo the results for a variable called bio. The code below does work and when I run var_dump($result); I do get the array from the test table that shows the bio variable data for that record. Oddly, I just can't get that variable to echo using the code below. What am I missing here?
<?php
include "ASEngine/AS.php";
include "templates/header.php";
$userId = ASSession::get("user_id");
?>
Testing the bio variable return:
<?php
$result = $db->select("SELECT * FROM test WHERE user_id = :id", array( 'id' => $userId ));
echo $result['bio'];
?>
You are not accessing the array properly, should be
echo $result[0]['bio'];
because your dump shows an array of array array(1) { [0]=> array(3) {
The array you have given in the comments for vardump will look like this:
array(
array(
"user_id" => 2,
"interests"=>"",
"bio" => "This is my bio"
)
);
so you are trying to echo a key which is non existent in the first dimension of the array. Try the following:
echo $result[0]['bio'];
Related
I get data from a .xlsx file, which I also put into an array(named $tableArray, I used PHPExcel for that). now i don't know how I can output even a single entry in my array.
I tried
$tableArray[1];
getting all the entries works with
var_dump($tableArray);
my code:
<?php
$excelReader = PHPExcel_IOFactory::createReaderForFile($fileName);
$excelReader->setReadDataOnly();
$excelObj = $excelReader->load($fileName);
$excelObj->getActiveSheet()->toArray();
$worksheetNames = $excelObj->getSheetNames($fileName);
$tableArray = array();
foreach($worksheetNames as $key => $sheetName){
$excelObj->setActiveSheetIndexByName($sheetName);
$tableArray[$sheetName] = $excelObj->getActiveSheet()->toArray();
}
var_dump($tableArray);
?>
Here's a few tips to help get you started.
If you want to see your array in a cleaner format on your browser, surround the var_dump() in a <pre> tag like so.
<?php
$myArray = [
"orange" => "foo",
"bar" => [
"test" => "more"
]
];
?>
<pre>
<?php var_dump($myArray); ?>
</pre>
This will help you understand your array structure better as it will be easier to read.
Accessing values in arrays is as easy as referencing the key. For example in the array above, if we want the value of orange, just echo $myArray["orange"];
If the value is another array, such as the key bar, and we want the value of test, we can just reference both keys in order echo $myArray["bar"]["test"];
You can use foreach() to loop through your array and perform action on each key => value pair.
<?php
$myArray = [
"orange" => "foo",
"bar" => [
"test" => "more"
]
];
foreach($myArray as $key => $value) {
var_dump($key);
var_dump($value);
}
If you're looking for more detailed information I'd recommend going through some of the courses on Code Academy. It will help you not only with this question, but others involving basic functionality of PHP.
Hope this helps!
I am very new in PHP and I was checking whether all my controlers are in so how can I echo this? what I tried resulting nothing
$controllers = array_intersect($json_api->get_controllers(), $active_controllers );
$return = array(
'json_api_version' => $version,
'controllers' => array_values($controllers)
);
echo $return['controllers']['controllers'];
use print_r function:
print_r($return['controllers']);
when you want read city you do:
$arr_controllers = $return['controllers'];
$key_2 = $arr_controllers[2];
where 2 is the key
If you want to print the values of an array with a
understandable format you should use print_r() instead of echo like so:
print_r($return['controllers']);
You can also use var_dump() to get some extra information about the fields, like the type and lenght.
If what you need is to asign a certain index of the array to a value just do something like this:
$variable = $return['controllers'][indexOfField]; // indexOfField=2 for city field
echo $variable;
For further information about print_r() check the official manual.
I have an array that is filled with different sayings and am trying to output a random one of the sayings. My program prints out the random saying, but sometimes it prints out the variable name that is assigned to the saying instead of the actual saying and I am not sure why.
$foo=Array('saying1', 'saying2', 'saying3');
$foo['saying1'] = "Hello.";
$foo['saying2'] = "World.";
$foo['saying3'] = "Goodbye.";
echo $foo[array_rand($foo)];
So for example it will print World as it should, but other times it will print saying2. Not sure what I am doing wrong.
Drop the values at the start. Change the first line to just:
$foo = array();
What you did was put values 'saying1' and such in the array. You don't want those values in there. You can also drop the index values with:
$foo[] = 'Hello.';
$foo[] = 'World.';
That simplifies your work.
You declared your array in the wrong way on the first line.
If you want to use your array as an associative Array:
$foo=Array('saying1' => array (), 'saying2' => array(), 'saying3' => array());
Or you can go for the not associative style given by Kainaw.
Edit: Calling this on the not associative array:
echo("<pre>"); print_r($foo); echo("</pre>");
Has as output:
Array
(
[0] => saying1
[1] => saying2
[2] => saying3
[saying1] => Hello.
[saying2] => World.
[saying3] => Goodbye.
)
Building on what #Answers_Seeker has said, to get your code to work the way you expect it, you'd have to re-declare and initialise your array using one of the methods below:
$foo=array('saying1'=>'Hello.', 'saying2'=>'World.', 'saying3'=>'Goodbye.');
OR this:
$foo=array();
$foo['saying1'] = "Hello.";
$foo['saying2'] = "World.";
$foo['saying3'] = "Goodbye.";
Then, to print the contents randomly:
echo $foo[array_rand($foo)];
I am trying to fix a bit of code and display an image that is set with a custom metabox. I have found the saved data in wp_postmeta and it looks like the data is saved as a string but I can see an obvious key value pair.
When I use the following code...
$imgVar = get_post_meta($post->ID, 'attachments', true);
$testing4 = $imgVar;
var_dump($testing4);
...I get the following output...
string(101) "{"my_item":[{"id":"653","fields":{"title":"mytitle","caption":"test this out"}}]}"
... this looks like it is telling me that the output is a string with 101 characters but I see key values and an array.
what I would like to have output is, or what it seems it should be...
array[0](
"my_item" => array(
"id" => "653",
"fields" => array(
"title" =>"mytitle",
"caption" => "test this out"
),
)
),
can someone explain what is being output for this newb :), and if it is possible to turn what is being output into a regular array. Or if I can access the key value "id => 653" without switching the output.
Thanks.
The output-string is probably serialized (easier for Wordpress to store data more efficient).
Try:
<?php maybe_unserialize( $original ) ?>
If you want to know more about this look at: http://codex.wordpress.org/Function_Reference/maybe_unserialize
$var = json_decode($testing4);
Format output with <pre> tags
echo '<pre>' . var_dump($testing4) . '</pre>';
echo "{$line['text_1']}";
the above echo works fine ,however when it comes to 2d array, in my sublime, only {$line['text_2']} this part work fine. output error both sublime and browser
echo "$array_2d[{$line['text_1']}][{$line['text_2']}]";
any idea?
update
echo "$array_2d[$line['text_1']][$line['text_2']]";
using xampp, error Parse error: syntax error, unexpected '[', expecting ']' in C:\xampp\htdocs
and I'm just outputting a value from the mysql_fetch_assoc. I can do it in another way by echo '' however I'm trying to make my code easier for future editting and code copy paste
and yes I'm doing things like
echo "The price is $array_2d[$line['text_1']][$line['text_2']]"
with lots of html code in the double quote.
Why are you trying to output the array?
if it is for debugging purposes, you can just use the native php functions print_r() or var_dump()
You should be able to say
echo "item is {$array_2d[$line['text1']][$line['text2']]}";
to get to a subelement.
Of course, this is only really useful when it's not the only thing in the string. If you're only echoing the one value, you don't need the quotes, and things get simpler.
echo $array_2d[$line['text1']][$line['text2']];
this should work :
echo $array_2d[$line['text_1']][$line['text_2']];
When echoing variables, you don't have to use the quotes:
echo $array_2d[$line['text_1']][$line['text_2']];
If you do need to output something with that string, the concatentation operator can help you:
echo "Array: " . echo $array_2d[$line['text_1']][$line['text_2']];
You can use print_r() to echo the array.
e.g.:
print_r($array);
Output will be:
Array ( [test] => 1 [test2] => 2 [multi] => Array ( [multi] => 1 [multi2] => 2 ) )
Also you can use this to make it more readable in a HTML context:
echo '<pre>';
print_r($array);
echo '</pre>';
Output will be:
Array
(
[test] => 1
[test2] => 2
[multi] => Array
(
[multi] => 1
[multi2] => 2
)
)
You can use print_r() or var_dump() to echo an array.
The print_r() displays information about a variable in a way that's readable by humans whereas the var_dump() function displays structured information about variables/expressions including its type and value.
$array = 'YOUR ARRAY';
echo "<pre>";
print_r($array);
echo "</pre>";
or
$array = 'YOUR ARRAY';
var_dump($array);
Example variations
I'm wondering why you would try using the $line array as a key to access data in $array_2d.
Anyway, try this:
echo($line['text_1'].'<br>');
this:
echo($array_2d['text_1']['text_2'].'<br>');
and finally this (based on your "the $line array provides the keys for the $array_2d" array example)
$key_a = $line['text_1'];
$key_b = $line['text_2'];
echo($array_2d[$key_a][$key_b].'<br>');
Which can also be written shorter like this:
echo($array_2d[$line['text_1']][$line['text_2']].'<br>');
Verifying/Dumping the array contents
To verify if your arrays hold the data you expect, do not use print_r. Do use var_dump instead as it will return more information you can use to check on any issues you think you might be having.
Example:
echo('<pre>');
var_dump($array_2d);
echo('</pre>');
Differences between var_dump and print_r
The var_dump function displays structured information of a variable (or expression), including its type and value. Arrays are explored recursively with values indented to show structure. var_dump also shows which array values and object properties are references.
print_r on the other hand displays information about a variable in a readable way and array values will be presented in a format that shows keys and elements. But you'll miss out on the details var_dump provides.
Example:
$array = array('test', 1, array('two', 'more'));
output of print_r:
Array
(
[0] => test
[1] => 1
[2] => Array
(
[0] => two
[1] => more
)
)
output of var_dump:
array(3) {
[0]=> string(4) "test"
[1]=> int(1)
[2]=> array(2)
{
[0]=> string(3) "two"
[1]=> string(4) "more"
}
}