How to make object from array in PHP [duplicate] - php

This question already has answers here:
How to convert an array to object in PHP?
(35 answers)
Converting array to objects in PHP [duplicate]
(1 answer)
How to convert an array into an object using stdClass() [duplicate]
(8 answers)
Closed 4 years ago.
How to assign array elements as object properties in PHP
$arr_zone_area = [];
foreach ($area as $key_area=>$row_area)
{
foreach ($nominalRoll as $key => $value)
{
if ($row_area->ADMIN_ID == $value->AREA_ID)
{
$arr_zone_area[$key_area]['ZONE_NAME'] = $row_area->ZONE_NAME;
$arr_zone_area[$key_area]['AREA_NAME'] = $row_area->AREA_NAME;
}
}
}
Output:
(
[0] => Array
(
[ZONE_NAME] => Dhaka
[ADMIN_ID] => Admin Dhaka
)
)
I want the output like this
(
[0] => stdClass Object
(
[ZONE_NAME] => Dhaka
[ADMIN_ID] => Admin Dhaka
)
)
Can anyone help me?
Thanks in advance

Just cast the values to an object when you assign them in the loop:
$arr_zone_area[$key_area] = (object)['ZONE_NAME' => $row_area->ZONE_NAME,
'AREA_NAME' => $row_area->AREA_NAME
];

you can use JSON encode and then decode it back, you will get the object. Something like this.
$obj_zone_area = json_decode(json_encode($arr_zone_area ));
now $obj_zone_area is a object.

Related

how to simplify an array of arrays with single object attribute [duplicate]

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

Access Variable Member [duplicate]

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.

How to get a value from an array key within an stdClass Object [duplicate]

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;
}

Converting a stdClass intro an array [duplicate]

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);

Remove all keys from an associative array in order to get a simple no key all-value array [duplicate]

This question already has answers here:
getting array values without foreach loop [duplicate]
(2 answers)
Closed 8 years ago.
Here is the sort of array I have for instance:
Array (
[0] => Array ( [id] => 21 )
[1] => Array ( [id] => 24 )
)
and I simply want to have
Array(21,24)
How can I do it ?
Try this
foreach($array as &$element){ $element = $element['id']; }

Categories