This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
I have an array variable $array like this
$array = Array
(
[results] => stdClass Object
(
[successc] => stdClass Object
(
[926] => stdClass Object
(
[transaction_id] => xx
[transaction_code] => xx
[status] => xx
[amount] => 5
)
)
[success] => Array
(
[0] => Successful transaction
)
)
)
I want to access the transaction_id element. The 926 is a variable value. It could very well be 927 or 928. It comes from another object $cc. Would it be correct to access the transaction_id using the following code?
$x = $cc->id;
$transaction_id = $array['results']->successc->{$x}->transaction_id;
Your approach is not bad, but the code structure looks more like an array.
To convert to a full array, you can encode to json en decode to array.
$array = json_decode(json_encode($array), true);
When TRUE, returned objects will be converted into associative arrays.
So you can access each level of $array as array element.
Related
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
I have php array like
Array
(
[0] => stdClass Object
( [type] => MILESTONE
[creator] => xyz
[tpid] => abc
[docname] => STS
[items] => stdClass Object
(
[MILESTONE_CODE] => ARPOD
[MILESTONE_TYPE] => ACT
[MILESTONE_DESCRIPTION] => Arrival at Port of Discharge
[TRIGGER_EVENT] => Y [ENVIRONMENT] => T
)
)
[1] => stdClass Object
(
[type] => MILESTONE
[creator] => xyz
[tpid] => abc
[docname] => STS
[items] => stdClass Object
(
[MILESTONE_CODE] => BKD
[MILESTONE_TYPE] => EST
[MILESTONE_DESCRIPTION] => Booking created
[TRIGGER_EVENT] => N
[ENVIRONMENT] => P
)
)
)
There are 2 php files written
File 1: In this file I am decoding my json data and got above array
$data = json_decode(file_get_contents("php://input"));
// print_r($data);exit;
foreach ($data as $value){
$TP->createTPConfig($data); // Here I am calling method for each index of array
}
File 2: In this file I have written method to insert data into oracle database.
function createTPConfig($dataToBeInsert){
// print_r($dataToBeInsert);exit; // till here I am getting above array
// written insert query here
}
Now in File 2, I want to access array values of
[type], [creator], [tpid], [docname]
and again
I want to foreach(repeat) loop for [items]
and want to access keys and values of
[MILESTONE_CODE], [MILESTONE_TYPE], [MILESTONE_DESCRIPTION] and [TRIGGER_EVENT]
and insert both keys and values into database
Can anyone help ... your help would be appreciated!
Just change
$data = json_decode(file_get_contents("php://input"));
to
$data = json_decode(file_get_contents("php://input"), true);
This will give you a full-fledged array. Obligatory man link: json decode
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I'm building a php webapp and I want to get a list of id's from an array named $array.
stdClass Object
(
[users] => Array
(
[0] => stdClass Object
(
[id] => 90379747
[id_str] => 90379747
)
[1] => stdClass Object
(
[id] => 30207605
[id_str] => 30207605
)
)
)
However when I try to get the value from the id key it somehow is not working.
echo "<h1>Result</h1>";
foreach ($array as $obj)
{
echo $obj->id;
}
What am I doing wrong here. Other examples on stackoverflow seem to suggest this should work.
You iterate an object, not an array. Try this way:
foreach ($array->users as $obj)
{
echo $obj->id;
}
This question already has answers here:
php stdClass to array
(11 answers)
Closed 6 years ago.
stdClass Object
(
[CountyId] => 3
[Name] => Alba
[Abbreviation] => AB
)
stdClass Object
(
[CountyId] => 4
[Name] => Arad
[Abbreviation] => AR
)
stdClass Object
(
[CountyId] => 5
[Name] => Arges
[Abbreviation] => AG
)
I want to convert this collection of stdClass Object into an array that contains only the CountyId, such as
[CountyId[0] => 3, CountyId[1] => 4, CountyId[2] => 5,...].
Anyone can help me ?
Try this:
$array = (array)$stdClassObject; // type casting
Update:
// convert your object into array using type casting
$array = (array) $stdClassObject;
// use array_column for specific index
$CountyIdArr = array_column($array, 'CountyId');
// note that, you can not use same index name for all values, you need to use as
$CountyIdArr['CountryID'] = $CountyIdArr;
echo "<pre>";
print_r($CountyIdArr);
This question already has answers here:
How can I access an array/object?
(6 answers)
Accessing Arrays inside Arrays In PHP
(4 answers)
Closed 7 years ago.
I am working with response from Youtube Data API and I don't know how to read each values in the array.
Array
(
[0] => youtube#channelListResponse
[1] => "X"
[2] => Array (
[totalResults] => 1
[resultsPerPage] => 1
)
[3] => Array (
[0] => Array
(
[kind] => youtube#channel
[etag] => "XX"
[id] => XXX
)
)
)
When I try to access [id] with echo $array[3]['id'] it returns Notice: Undefined offset: 3
AS Max said you can access single value in array in array by
$arrayName[index]['key']
Example You can access 'kind' with
$arrayName[3]['kind']
Since, inner level array is key value paired you need to specify key instead of index.
If you want to access all value, you can do it with foreach loop and check type of each value use'gettype()' method. if type is array then use same function recursively.
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
I have a call to a webservice which is returning json data. I use:
$response_json = json_decode ( $response );
If I print_r($response) I get this:
stdClass Object
(
[meta] => stdClass Object
(
[no_of_pages] => 3
[current_page] => 1
[max_items_per_page] => 250
[no_of_items] => 740
)
[data] => Array
(
[0] => stdClass Object
(
[orderid] => 322191645
[customer] => stdClass Object
(
[city] => FELIXSTOWE
I am trying to loop through the orders:
foreach($response_json as $orders) {
echo $orders.['data'].[0].['orderid'];
}
but I keep getting:
Catchable fatal error: Object of class stdClass could not be converted to string. I have also tried many other ways, but I just can't seem to access the data in a loop. Thanks in advance.
You can json_decode as associative array.
$response = json_decode ($response, true);
foreach($response as $orders) {
echo $orders[0]['orderid'];
}
Don't use dots between brackets.
Skip "data" in your array.
Example:
foreach($response_json as $orders) {
echo $orders[0]['orderid'];
}