I have something like this. I want to access "name" attribute. I can access it by
foreach($items as $item)
{echo $item->name}
I have tried
echo $items[0]['name'] but it doesn't work.
Array
(
[0] => stdClass Object
(
[term_id] => 7
[name] => Video/Animacija
[slug] => videoanimacija
[term_group] => 0
[term_taxonomy_id] => 7
[taxonomy] => portfolio_technologies
[description] =>
[parent] => 0
[count] => 17 [filter] => raw
)
)
You need to use the object syntax on name there as well.
echo $items[0]->name;
should work. Remember, you have the following structures
an array (access via [])
an object (access via ->)
$item[0] is a std object, and you cannot access to its value using brackets: [], you should use arrow: ->
Like this :
$item[0]->name
You have an array of objects, not an array of arrays. You need to do this:
$variable = $items[0]->name;
You don't have an array inside an array, you have an object inside an array. You need to access it like:
echo $items[0]->name;
Related
Please tell me how to output the imtId value, the array is not complete, I will not output further, but the meaning should be clear.Thank you in advance
Array
(
[id] => mavrin-wildberries-1635334576193516728
[jsonrpc] => 2.0
[result] => stdClass Object
(
[cards] => Array
(
[0] => stdClass Object
(
[id] => d3c33a3f-f5b3-5647-8e7a-ad50d27d4417
[imtId] => 30306963
[userId] => 0
[supplierId] => e9b901b9-b663-5648-97b8-6313d0e245ba
[imtSupplierId] => 0
I tried:
echo ['result']['cards'][0]['nmId']
A few things:
You need to echo an actual variable, not just a series of indexes.
The item inside "result" is an object, not an array
So is the item within the "0" index.
There is no such index as "nmld" - you said you wanted "imtId" instead, so I don't know why you didn't use that?
Therefore, if this data is contained in a variable called $arr then something like
echo $arr["result"]->cards[0]->imtId;
I have my code in PHP which is returning this Array of data:
GoCardlessPro\Core\ListResponse Object
(
[records] => Array
(
[0] => GoCardlessPro\Resources\Mandate Object
(
[model_name:protected] => Mandate
[created_at:protected] => 2017-04-01T16:49:09.642Z
[id:protected] => ID001
[links:protected] => stdClass Object
(
[customer_bank_account] => CB001
[creditor] => CR001
[customer] => CU001
)
[metadata:protected] => stdClass Object
(
)
[next_possible_charge_date:protected] => 2017-04-06
[payments_require_approval:protected] =>
[reference:protected] => RE001
[scheme:protected] => bacs
[status:protected] => active
[data:GoCardlessPro\Resources\BaseResource:private] => stdClass Object
(
[id] => 123
[created_at] => 2017-04-01T16:49:09.642Z
[reference] => RE001
[status] => active
[scheme] => bacs
[next_possible_charge_date] => 2017-04-06
[payments_require_approval] =>
[metadata] => stdClass Object
(
)
[links] => stdClass Object
(
[customer_bank_account] => 001
[creditor] => CR001
[customer] => CU001
)
)
[api_response] =>
)
)
)
I want to be able to read the ID of the first item in therecords array.
This data is contained inside a variable called $GC_Mandate;
I have tried these:
echo $GC_Mandate->records->{0}->id;
echo $GC_Mandate->records->0->id;
echo $GC_Mandate->records->[0]->id;
$GC_Mandate = $GC_Mandate->records;
echo $GC_Mandate->{0}->id;
But none will return the data
To get the first record, the syntax you need is $GC_Mandate->records[ 0 ].
However, that object is a GoCardlessPro\Resources\Mandate object and its member id is protected1, so we'd need to know the interface of GoCardlessPro\Resources\Mandate (its public methods1), to know if we can somehow retrieve the value of id.
My guess would be getId(), so the full syntax would become
$GC_Mandate->records[ 0 ]->getId()
But, that's just a guess. You'd have to look into the documentation/class definition of GoCardlessPro\Resources\Mandate, to be sure if you can retrieve id.
Turns out (provided I'm linking to the correct github repository) you can do:
$GC_Mandate->records[ 0 ]->id
since GoCardlessPro\Resources\Mandate extends GoCardlessPro\Resources\BaseResource, which exposes the protected members through GoCardlessPro\Resources\BaseResource::__get()2.
1. visibility in PHP
2. magic methods in PHP
I can't comment so I guess I'll have to post.
You should try to print_r($GC_Mandate); and see what it gives out and then go from there.
Try $GC_Mandate->records[0]->__get('id')
it will return all data ..for perticulat data put this in foreach loop
print_r($GC_Mandate['records']);
I have an array $presettings
print_r($presettings); outputs:
Array (
[0] => stdClass Object (
[uuid] => xxx-1ef8-aac6-xxx-xxx
[name] => etime
[owner] => eder112T Resident
[online] => 1
[channel] => 63b525ae-xxx-3555-1c74-xxx
[owner_uuid] => a371751c-eb77-xxx-899c-xxx
[simname] => Plainfield
[slurl] => xxx://xxx/xx/xx/243/24/xx/?title=xx
[design] => 2
[msg_oftheday] => two
[machine_name] => one
[autopay] =>
[autolog_leave] =>
[autolog_offline] =>
[allow_activation] =>
)
)
and now i want to get a special key:
echo "test output : "$presettings['machine_name']." testend";
outputs "" (nothing).
my method look like this
function preloadSettingsFromMYSQL($ownername,$prim_uuid)
{
$result = $this->instance->get_rows("SELECT * FROM etime_rims where owner='".$ownername."' AND uuid='".$prim_uuid."'");
return $result;
}
$result is an object array, also tried it with $presettings->machine_name, did not work too.
where is the error?
thank you.
If you look closely in the print_r result, you can see that there is a 0 there, meaning that those values aren't directly in $presettings, but actually in the first element of $presettings.
Just try:
$presettings[0]->machine_name
You object is multi dimensional so just add a level
$presettings[0]->machine_name;
As side note you have an object here and not array do don't try to access alues with scopes.
In your array, you have stdClass Object at index 0. You're looking to access the object's variables, which is a slightly different syntax than arrays:
echo $presettings[0]->machine_name;
I get the following returned result from a print_r of a variable returned from a $metadataPlugin->metadata->listAction($filter, $pager) function. I am trying to echo out the [xml] piece but can't figure out how to do it.
KalturaMetadataListResponse Object ( [objects] => Array ( [0] => KalturaMetadata Object ( [id] => 1 [partnerId] => 8 [metadataProfileId] => 2 [metadataProfileVersion] => 1 [metadataObjectType] => 1 [objectId] => 0 [version] => 1 [createdAt] => 1353093894 [updatedAt] => 1353093894 [status] => 1 [xml] => 1353049200 ) ) [totalCount] => 1 )
I have tried
echo $result['objects'][0]->xml;
and
echo $result[0]->xml;
with no success.
It looks like it should be
$result->objects[0]->xml
Just remember when looking at print_r or var_dump results that any time you see Object (...) that you will need to use -> to access the property listed for that object.
So in this case, you have a base Object set as $result, which you then need to get the objects property from. That property contains an array. At index 0 of the array, is another Object, which is the Object where your xml property resides.
I have a SimpleXMLElement and would like to check if a particular element has a non-blank value before looping through it utilizing a foreach loop. Here is my SimpleXMLObject
Say this is contained in $myXMLElement
SimpleXMLElement Object ( [f] => Array ( [0] => Marcus [1] => Smith [2] => Brown University [3] => 1243123200000 [4] => Masters [5] => TestValue [6] => TestValue2 [7] => 4 [8] => SimpleXMLElement Object ( [#attributes] => Array ( [id] => 16 ) ) [9] => 0 ) [update_id] => 1325795135203 )
Within the array is a value of
[3] => 1243123200000
This is what I would like to check this parameter and determine that it is not NULL. If I wanted to grab it as a String what would be the way to do this.
I was hoping for something like (string)$myXMLElement[3] but that does not appear to be correct syntax for what I am trying.
You can use array notation, but since the array is part of the node f you have to access it like this:
$val = $xmlObj->f[3];
if (empty($val)) {
// its empty
}
On an unrelated note, since the 8th element in that array is itself a SimpleXML object, you would access its values like this:
$val = $xmlObj->f[8]->update_id;
In these cases, its just a mix of object access and array access notation.
To get the attributes from the 8th element, you can also use array notation:
$id = $xmlObj->f[8]['id']; // get "id" attribute
The page on Basic SimpleXML Usage has some very helpful examples showing how to access different elements from a SimpleXML object. Example #5 show how to access attributes.