Printing an stdClass object - php

I have:
$value = $wpdb->get_row("SELECT custom_message FROM `wp_wpsc_cart_contents` WHERE purchaseid='" . $purchase_log['id'] . "'");
if I do:
print_r($value);
I get:
stdClass Object
(
[custom_message] => |Castor Seed Oil $4.45|
)
So I tried to get that value doing:
foreach($value as $index => $result) {
echo $result["custom_message"];
}
I also tried:
foreach($value as $index => $result) {
echo $result->custom_message;
}
but that prints nothing, any idea what I'm doing wrong here?

The loop does nothing, you are iterating an object with a single property that you already know the name of. Just do this:
echo $value->custom_message;

No need for the for loop. Just do
echo $value->custom_message;

Related

How do I echo Nested Array Element?

I don't know what I'm doing wrong, I can't get my array item to echo out. I keep getting this error:
Illegal string offset 'CreatedDatetime'.
My XML looks like this via JSON print_r:
Array
(
[ABOUT_VERSIONS] => Array
(
[ABOUT_VERSION] => Array
(
[CreatedDatetime] => 2019-10-22T22:29:47.7617229Z
[DataVersionIdentifier] => 201703
)
)
)
My code is:
foreach ($newArr["ABOUT_VERSIONS"]["ABOUT_VERSION"] as $item){
echo $item["CreatedDatetime"]."<br>";
}
This seems so simple but I'm having a block. I can't echo out the CreatedDatetime key.
$item is not an array, it iterates over the two values in $newArr['ABOUT_VERSION'] i.e. 2019-10-22T22:29:47.7617229Z and 201703. To display the CreatedDatetime, either access it directly:
echo $newArr["ABOUT_VERSIONS"]["ABOUT_VERSION"]["CreatedDatetime"] . "<br>";
or do a key comparison in the loop:
foreach ($newArr["ABOUT_VERSIONS"]["ABOUT_VERSION"] as $key => $value){
if ($key == "CreatedDatetime") echo $value . "<br>";
}
In both cases the output is 2019-10-22T22:29:47.7617229Z.
Demo on 3v4l.org

php get the value of attribute name from array

I have an array stored in my database. So when I try : print_r($arrayname),
It showed the result like this:
Array
(
[0] => Color,Processor
[attribute_name] => Color,Processor
)
I want to get the values of [attribute_name] => Color,Processor .
So far I made the foreach loop like this :
foreach ($arraylist as $name) {
echo $name['attribute_name];
}
But it showing the result like cc. So can someone kindly tell me how to get the values from database?
Actually you don't need to use foreach
You can access it like this.
echo $arraylist["attribute_name"];
Please try this
foreach ($arraylist as $key => $name) {
if($key == 'attribute_name')
echo $name;
}

how to echo multidimension array using php

I have the following array, I need to display the names on a form.
How can I do this via foreach() loop?
What I am trying is:
Array
(
[0] => Array
(
[to_user_name] => John
)
[1] => Array
(
[to_user_name] => Mike
)
)
foreach( $myArray as $subArray ) {
echo $subArray["to_user_name"];
}
It's not clear how you want to use those values in your form, but just echo the values wherever you'd need them, e.g.,
foreach( $myArray as $subArray ) {
echo "<input type=\"text\" name=\"user_name\" value=\"" . $subArray["to_user_name"] . "\">";
}
I was treating it like a single array and then i realized it is a multidimensino array and the following worked, i hope this helps someone else too
foreach ($messages->getMessage('recipient_names') as $section => $items ){
foreach ($items as $key => $value){
echo "$value, ";
}
}
to see the content you can use
print_r($your_array);
For developing purpose you need to use for/foreach loop
foreach($your_array as $array_temp)
{
foreach($array_temp as $item)
{
echo $item;
}
}

getting information out of an array with key/value pairs

I have the following snippet of code that is creating the following array...
while ($stmt->fetch()) {
foreach($row as $key => $val) {
$x[$key] = $val;
}
$results[] = $x;
}
Results in the follow array:
Array ( [0] => Array ( [cap_login] => master [cap_pword] => B-a411dc195b1f04e638565e5479b1880956011badb73361ca ) )
Basically I want to extract the cap_login and cap_pword values for testing. For some reason I can't get it!
I've tried this kind of thing:
echo $results[$cap_login];
but I get the error
Undefined variable: cap_login
Can someone put me right here?
Thanks.
cap_login is in an array within $results so you would have to do $results[0]['cap_login']
You would have to do the following:
echo $x[0]['cap_login'] . '<br />';
echo $x[0]['cap_pword'];
The reson $results[$cap_login] wont work is because there isn't a variable called $cap_login, there is a string called cap login. In addition, there isn't a key in $results called $cap_login. There is a value in $results called 'cap_login'

Strange Array Behavior in PHP

I am trying to build an array and I am looping through the values of an XML document, I have everything pulling out great using xpath, here's my code:
function parseAccountIds($xml) {
$arr = array();
foreach($xml->entry as $k => $v) {
$acctName = $v->title;
$prop = $v->xpath('dxp:property');
foreach($prop as $k1 => $v1) {
if($v1->attributes()->name == "ga:accountId")
$acctId = (string) $v1->attributes()->value;
else if($v1->attributes()->name == "ga:profileId")
$profileId = (string) $v1->attributes()->value;
}
echo "profile id ".$profileId;
echo "<BR>";
echo "acctName ".$acctName;
echo "<BR>";
$subArray = array($acctName => $profileId);
print_r($subArray);
$arr[] = array($acctId => $subArray);
}
print_r($arr);
return json_encode($arr);
}
The most important bit is where I print_r subArray. I can see acctName and profileId print, but then subArray is empty. For Example:
profile id 45580
acctName accountName1
Array
(
)
profile id 4300
acctName accountName2
Array
(
)
profile id 4338
acctName accountName3
Array
(
)
How are these values not being inserted? I've been looking at the code for a while now, and I'm a bit confused.
Any suggestions would really help,
Thanks!
try this:
$subArray[$acctName] = $profileId;
instead of
$subArray = array($acctName => $profileId);
$v->title is actually a SimpleXMLObject still!
I forgot to cast it as a string, when I tried to make it the index in the array, it freaked out, geez I spent a whole hour on this!
Thanks for your suggestions guys :P

Categories