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);
Related
This question already has answers here:
how get each single column data from php multidimensional array?
(2 answers)
Closed 2 years ago.
I have the following data structure:
$campaigns =
Array
(
[0] => Array
(
[subject] => cca-cpg
)
[1] => Array
(
[subject] => cleanup-cpg
)
[2] => Array
(
[subject] => gas-cpg
)
[3] => Array
(
[subject] => pollinators-cpg
)
)
what I would like to end up with is:
$campaigns = ['cca-cpg','clean_up-cpg','gas-cpg','pollinators-cpg'];
this will work:
$newCampaigns=[];
for($i=0;$i<count($campaigns);$i++){
array_push($newCampaigns,$campaigns[$i]['subject'];
}
but I was wondering if there's a better way to do this. The data is coming directly from a mysql database
There's array_column() function for you:
$newCampaigns = array_column($campaigns, 'subject');
Source: https://www.php.net/manual/en/function.array-column.php
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 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.
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:
Reset PHP Array Index
(4 answers)
Closed 8 years ago.
Array
(
[2] => stdClass Object
(
[name] => test-song-poll-03
[description] => test-song-poll-03
[created_at] => 2014-05-02T23:19:06Z
[count] => stdClass Object
(
[approved] => 26638
[pending] => 0
[rejected] => 36923
[total] => 63561
)
[tpm] => 47
[approved_tpm] => 9
[pct] => 2
)
)
I have a function that uses array_filter and it returns what you see above. It will only return one object within the array. I do not know what the array index will be, but I know there will only be one item in the array. Is there an array function that strips down the array and just returns the content of it, since I don't need an array with just one item in it.
You should use:
$x = array_values($yourArrayName);
echo $x[0];
You can also use:
echo current($yourArrayName);