How to access attributes with dashes in the name? - php

So I run some code like this:
$quote = simplexml_load_string($xml);
$quote = $quote->Stock;
echo 'Name: ';
echo $quote->Name;
echo '<br>';
echo 'Sybmol: ';
echo $quote->Symbol;
echo '<br>';
echo 'Last Price: ';
echo $quote->Last;
echo '<br>';
echo 'Earnings To Price Ratio: ';
echo $quote->P-E;
echo '<br>';
I know that the second to last line ($quote->P-E) is incorrect - I don't think you can use dashes like that. But for some reason I can't figure out how to access that property. The weird thing is that's how it's written if I var_dump($quote) (It's towards the end):
object(SimpleXMLElement)#17 (16) { ["Symbol"]=> string(4) "AAPL" ["Last"]=> string(6) "271.87" ["Date"]=> string(9) "6/17/2010" ["Time"]=> string(6) "3:59pm" ["Change"]=> string(5) "+4.62" ["Open"]=> string(6) "270.72" ["High"]=> string(6) "272.90" ["Low"]=> string(6) "269.50" ["Volume"]=> string(8) "31195032" ["MktCap"]=> string(6) "247.4B" ["PreviousClose"]=> string(6) "267.25" ["PercentageChange"]=> string(6) "+1.73%" ["AnnRange"]=> string(15) "132.88 - 272.90" ["Earns"]=> string(6) "11.796" ["P-E"]=> string(5) "22.66" ["Name"]=> string(10) "Apple Inc." }
How should I be accessing this attribute/property?

$quote->{'P-E'};

Related

PHP ?Nested Array

I have an array returned from Google Maps, that looks like this:
array(4) {
["destination_addresses"]=> array(4) {
[0]=> string(19) "Walsall WS2 9PS, UK"
[1]=> string(19) "Walsall WS2 9PS, UK"
[2]=> string(19) "Walsall WS2 9PS, UK"
[3]=> string(26) "Wolverhampton WV10 0QP, UK"
}
["origin_addresses"]=> array(1) {
[0]=> string(18) "Stone ST15 0FL, UK"
}
["rows"]=> array(1) {
[0]=> array(1) {
["elements"]=> array(4) {
[0]=> array(3) {
["distance"]=> array(2) {
["text"]=> string(7) "41.9 km"
["value"]=> int(41947)
}
["duration"]=> array(2) {
["text"]=> string(7) "36 mins"
["value"]=> int(2134)
}
["status"]=> string(2) "OK"
}
[1]=> array(3) {
["distance"]=> array(2) {
["text"]=> string(7) "41.9 km"
["value"]=> int(41947)
}
["duration"]=> array(2) {
["text"]=> string(7) "36 mins"
["value"]=> int(2134)
}
["status"]=> string(2) "OK"
}
[2]=> array(3) {
["distance"]=> array(2) {
["text"]=> string(7) "41.9 km"
["value"]=> int(41947)
}
["duration"]=> array(2) {
["text"]=> string(7) "36 mins"
["value"]=> int(2134)
}
["status"]=> string(2) "OK"
}
[3]=> array(3) {
["distance"]=> array(2) {
["text"]=> string(7) "40.9 km"
["value"]=> int(40924)
}
["duration"]=> array(2) {
["text"]=> string(7) "41 mins"
["value"]=> int(2458)
}
["status"]=> string(2) "OK"
}
}
}
}
["status"]=> string(2) "OK"
}
I want to be able to run through the array, and print the destination address and the distance and time values.
This prints the destination addresses ok:
$length=count($properties);
for($a=0;$a<$length;$a++)
{
echo '</br>Index is ' . $a . ' | id is ' . $index[$a] . ' | destination is ' . $properties["destination_addresses"][$a] . ' | time is ';
}
But I can't figure out how to print the rest. I've been banging my head against the wall all night!
Thank you in anticipation.
To loop over all these journeys and print the distance and time for each potential journey, create a foreach loop and then use the $idx to address the subordinate parts of the muleiple arrays
foreach( $destination['destination_addresses'] as $idx => $to ) {
if ( $destination['rows'][0]['elements'][$idx]['status'] == 'OK' ) {
echo 'Going from ' . $destination['origin_addresses'][0];
echo ' To ' . $to;
echo ' Distance ' . $destination['rows'][0]['elements'][$idx]['distance']['text'];
echo ' Duration ' . $destination['rows'][0]['elements'][$idx]['duration']['text'];
} else {
//whatever you do if the status is not OK
}
}

issue with array and json decode

I'm a little confused I have the following;
array(4) { ["id"]=> string(1) "2" ["result"]=> array(5) { ["account"]=> string(34) "rf2DKsfbZuCa2WwQAg49cRXqYuRCTszUTp" ["assets"]=> array(1) { ["rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ"]=> array(1) { [0]=> array(2) { ["currency"]=> string(3) "FLX" ["value"]=> string(9) "999999999" } } } ["ledger_hash"]=> string(64) "E44084AAC1F1AE2C7147675BF8F763ED7620DC82E1B1A5BD6DC6F640652F1B63" ["ledger_index"]=> int(17773072) ["validated"]=> bool(true) } ["status"]=> string(7) "success" ["type"]=> string(8) "response" }
I am trying to echo the currency;
I have the following but it's not working.
$response = websocket_read($sp,$errstr);
$decode = json_decode($response, true);
echo'<br><br>';
echo $decode['result']['assets']['currency'];
echo'<br><br>';
echo "Server responed with: '" . $response ."'\n";
The line that is not working is echo $decode['result']['assets']['currency'];
Thankyou
Your rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ has the value as an array, either remove the square brackets from this part to treat it as value
"rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ":[{"currency":"FLX","value":"999999999"}]
to
"rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ":{"currency":"FLX","value":"999999999"}
or use index to get the value
echo $decode['result']['assets']['rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ'][0]['currency'];

output an Array with same values

I have a SQL Query result (array): "title", "content" and "name"
Here is my var_dump:
array(28) {
[0]=>
array(3) {
["title"]=>
string(10) "Basis Task"
["content"]=>
string(43) "https://www.wrike.com/open.htm?id=440908999"
["name"]=>
string(14) "Christian Wahl"
}
[1]=>
array(3) {
["title"]=>
string(10) "Basis Task"
["content"]=>
string(5) "MySQL"
["name"]=>
string(14) "Christian Wahl"
}
[2]=>
array(3) {
["title"]=>
string(4) "Test"
["content"]=>
string(3) "PHP"
["name"]=>
string(14) "Christian Wahl"
}
[3]=>
array(3) {
["title"]=>
string(4) "Test"
["content"]=>
string(3) "PHP"
["name"]=>
string(14) "Christian Wahl"
}
[4]=>
array(3) {
["title"]=>
string(10) "Basis Task"
["content"]=>
string(7) "content"
["name"]=>
string(9) "Elena Ott"
}
(I cut off the end of the array to make it a little clearer to see)
These are Tasks who are assigned to a User.
Now i want to output the "Name" (panel-heading)
and the "title" and "content" (panel-body).
It should look smth like this but for each given name:
how it should look like
I tried to find a solution on my own, but without success :(
I hope u can help me?
thx a lot
-Taddl
just loop on your data and render it
$data = [];
foreach ($databaseResult as $row) {
$data[$row['name']][] = $row;
}
foreach($data as $name => $stuff) {
echo $name . '<br>';
foreach($stuff as $row) {
echo $row["title"] . ':' . $row["content"] . '<BR>';
}
}
You can use foreach loop. As example:
foreach($yourarrayvariable as $data)
{
echo "<tr>";
echo "<td class='heading'>".$data['name']."</td>";
echo "<div class='content'>";
echo "<td>".$data['title']."</td>";
echo "<td>".$data['content']."</td>";
echo "</div>";
echo "</tr>";
}
And create your desired template look classes as per your need inside foreach.

Access multidimensional array in php?

array(10) {
["table_name"]=>
string(0) ""
["chart_type"]=>
string(5) "Table"
["chart_name"]=>
string(9) "Veterans "
["dashboard_name"]=>
string(7) "Default"
["data_option"]=>
string(0) ""
["limit_value"]=>
string(1) "0"
["view_status"]=>
bool(true)
["view_priority"]=>
string(0) ""
["chart_data"]=>
array(72) {
[0]=>
array(11) {
[0]=>
string(4) "Rank"
[1]=>
string(6) "Branch"
[2]=>
string(12) "Level 3 Name"
[3]=>
string(12) "Level 4 Name"
[4]=>
string(23) "Logged Premia Score MTD"
[5]=>
string(26) "Converted Premia Score MTD"
[6]=>
string(21) "Persistancy Score MTD"
[7]=>
string(23) "Logged Premia Score YTD"
[8]=>
string(26) "Converted Premia Score YTD"
[9]=>
string(21) "Persistancy Score YTD"
[10]=>
string(5) "Score"
}
[1]=>
array(11) {
[0]=>
float(1)
[1]=>
string(22) " GWALIOR DIRECTSegment"
[2]=>
string(29) "G00563 - GOVIND SINGH KUSHWAH"
[3]=>
string(21) "S03643 - Shishir Jain"
[4]=>
float(1)
[5]=>
float(1)
[6]=>
float(1)
[7]=>
float(0.9994)
[8]=>
float(1)
[9]=>
float(1)
[10]=>
float(9.89901)
}
[2]=>
array(11) {
[0]=>
float(2)
[1]=>
string(26) " KURUKSHETRA DIRECTSegment"
[2]=>
string(23) "A02311 - Amarjeet Singh"
[3]=>
string(21) "S00927 - Sachin Kumar"
[4]=>
float(1)
[5]=>
float(1)
[6]=>
float(1)
[7]=>
float(0.953)
[8]=>
float(1)
[9]=>
float(1)
[10]=>
float(9.82245)
}
}
["color_scheme_name"]=>
string(7) "Default"
}
How do i display the chart_data in php
$arr=json_decode($row[0],TRUE);
var_dump($arr);
foreach($arr as $row)
{
foreach($row['chart_data'] as $k)
{
echo $k['Rank'];
echo $k['Branch'];
}
}
EDIT:
for ($x=0; $x<=count
($arr['chart_data']); $x++) {
foreach($arr['chart_data'][$x] as $key=>$val)
{
echo $val;
}
}
You are having 2 foreach calls.
The first one passes all keys of the outer array, amongst which chart_data.
The second one looks at the children of the outer array, and looks for chart_data in each child. But is obviously not finding any.
Change your code to this:
$arr=json_decode($row[0],TRUE);
foreach($arr['chart_data'] as $k){
echo $k['Rank'];
echo $k['Branch'];
}
edit:
I see. (Thanks for indenting your array) Your problem is off course that $arr['rank']and $arr['branch'] don't exist. They are mere strings inside $k[0] and $k[1] and themselves don't contain any other data.
What would you have like to see outputted?
Maybe this is solution.
$arr = json_decode($row[0],TRUE);
foreach($row['chart_data'] as $chart_data)
{
foreach($chart_data[0] as $k)
{
echo $k['Rank'];
echo $k['Branch'];
}
}
Update :
Rank, Branch is values. Your array is bad format.
$arr = json_decode($row[0],TRUE);
foreach($row['chart_data'] as $chart_data)
{
foreach($chart_data[0] as $k)
{
echo $k[0];
echo $k[1];
}
}

Wordpress Parsing Array in PHP

How can I parse this in PHP:
a:8:{s:3:"key";s:19:"field_501743d4baa78";s:5:"label";s:8:"Category";s:4:"name";s:8:"category";s:4:"type";s:8:"checkbox";s:12:"instructions";s:0:"";s:8:"required";s:1:"1";s:7:"choices";a:17:{s:4:"Arts";s:4:"Arts";s:8:"Business";s:8:"Business";s:14:"Communications";s:14:"Communications";s:16:"Criminal Justice";s:16:"Criminal Justice";s:13:"Culinary Arts";s:13:"Culinary Arts";s:9:"Education";s:9:"Education";s:11:"Engineering";s:11:"Engineering";s:11:"Health Care";s:11:"Health Care";s:22:"Information Technology";s:22:"Information Technology";s:13:"International";s:13:"International";s:5:"Legal";s:5:"Legal";s:17:"Political Science";s:17:"Political Science";s:10:"Psychology";s:10:"Psychology";s:8:"Religion";s:8:"Religion";s:7:"Science";s:7:"Science";s:9:"Technical";s:9:"Technical";s:10:"Veterinary";s:10:"Veterinary";}s:8:"order_no";s:1:"3";}
to get a list of all the "categorys" such as Arts, Business, Communications etc etc
Thanks for your help
UPDATE
After running unserialize this is what i get:
array(8) { ["key"]=> string(19) "field_501743d4baa78" ["label"]=> string(8) "Category" ["name"]=> string(8) "category" ["type"]=> string(8) "checkbox" ["instructions"]=> string(0) "" ["required"]=> string(1) "1" ["choices"]=> array(17) { ["Arts"]=> string(4) "Arts" ["Business"]=> string(8) "Business" ["Communications"]=> string(14) "Communications" ["Criminal Justice"]=> string(16) "Criminal Justice" ["Culinary Arts"]=> string(13) "Culinary Arts" ["Education"]=> string(9) "Education" ["Engineering"]=> string(11) "Engineering" ["Health Care"]=> string(11) "Health Care" ["Information Technology"]=> string(22) "Information Technology" ["International"]=> string(13) "International" ["Legal"]=> string(5) "Legal" ["Political Science"]=> string(17) "Political Science" ["Psychology"]=> string(10) "Psychology" ["Religion"]=> string(8) "Religion" ["Science"]=> string(7) "Science" ["Technical"]=> string(9) "Technical" ["Veterinary"]=> string(10) "Veterinary" } ["order_no"]=> string(1) "3" }
however i'm not sure how to loop through and just get the category names - sorry - i'm new to PHP - probably just doing something stupid - thanks for your help
That's just a serialized array. Just unserialize it and getting the values is easy:
$array = unserialize(a:8:{s:3:"key";s:19:"field_501743d4baa78";s:5:"label";s:8:"Category";s:4:"name";s:8:"category";s:4:"type";s:8:"checkbox";s:12:"instructions";s:0:"";s:8:"required";s:1:"1";s:7:"choices";a:17:{s:4:"Arts";s:4:"Arts";s:8:"Business";s:8:"Business";s:14:"Communications";s:14:"Communications";s:16:"Criminal Justice";s:16:"Criminal Justice";s:13:"Culinary Arts";s:13:"Culinary Arts";s:9:"Education";s:9:"Education";s:11:"Engineering";s:11:"Engineering";s:11:"Health Care";s:11:"Health Care";s:22:"Information Technology";s:22:"Information Technology";s:13:"International";s:13:"International";s:5:"Legal";s:5:"Legal";s:17:"Political Science";s:17:"Political Science";s:10:"Psychology";s:10:"Psychology";s:8:"Religion";s:8:"Religion";s:7:"Science";s:7:"Science";s:9:"Technical";s:9:"Technical";s:10:"Veterinary";s:10:"Veterinary";}s:8:"order_no";s:1:"3";});
var_dump($array);
$sample_arr = unserialize($array); //unserialize here
$sample_arr = $sample_arr['choices']; //get the array
then you have the category list saved to $sample_arr.
foreach($sample_arr as $temp) {
//do stuff with each element here
}
Examples include outputting this as a html list
echo '<ul>';
foreach($sample_arr as $temp) {
echo '<li>' . $temp . '</li>';
}
echo '</ul>';

Categories