Access property in stdClass Object [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
stdClass Object
(
[ModuleAccountInfo] => Array
(
[0] => stdClass Object
(
[ServerName] => EAST
[HostingModule] => ActiveDirectory
[ActiveDirectorySiteName] => EAST
[AccountIdentity] => OU=ndla,OU=Hosting,DC=east,DC=domain,DC=local
[Groups] => 2
[Users] => 15
)
[1] => stdClass Object
(
[ServerName] => EAST.net
[HostingModule] => hange
[DiskQuota] => 375000
[DiskQuotaAdditional] => 0
[DateQuotaExceeded] => 0001-01-01T00:00:00
[DiskSpace] => 58567
[MailboxesDiskSpace] => 59973051
[PublicFoldersDiskSpace] => 0
[MessageArchivingDiskSpace] => 0
[Contacts] => 8
[Mailboxes] => 15
How do I access the ServerName properties?
This object is held in a $modules variables. The above is the print_r of $modules.

Because the ModuleAccountInfo property is an array, you'll either need to use a specific index
echo $modules->ModuleAccountInfo[0]->ServerName;
or loop
foreach ($modules->ModuleAccountInfo as $moduleAccountInfo) {
echo $moduleAccountInfo->ServerName;
}

Related

PHP Multi-dimensional array rearranging [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have the following array that i want to re-arrange
Array
(
[0] => stdClass Object
(
[feeds_id] => 1338
[flag] => 0
)
[1] => stdClass Object
(
[feeds_id] => 1339
[flag] => 0
)
[2] => stdClass Object
(
[feeds_id] => 1339
[flag] => 1
)
)
I want to arrange it to look like this
[1338] => Array (
[0] => 0
)
[1339] => Array (
[0] => 0
[1] => 1
)
This code should work:
$newArray=array();
foreach($items as $item){
if(!is_array($newArray[$item->feeds_id])){
$newArray[$item->feeds_id]=array();
}
array_push($newArray[$item->feeds_id],$item->flag);
}
You should first create an empty array where the new data will be stored. Then, inside the foreach, you should use array_push, BUT if the sub-array in where you want to put the data is not an array, you should declare it first (that's why the "if" before the array_push)

Removed nested array php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have an array which is
Array
(
[0] => Array
(
[city_id] => 16
[city_name] => San Diego
[state_abbreviation] => CA
[city_lat] => 32.715328
[city_long] => -117.157257
[city_image] => sandiego_ca.jpg
)
)
i just want to show
Array
(
[city_id] => 16
[city_name] => San Diego
[state_abbreviation] => CA
[city_lat] => 32.715328
[city_long] => -117.157257
[city_image] => sandiego_ca.jpg
)
Basically removing the [0] nest so i could parse it better
not php array can do like this . just use a tmp variable.
$arr = $this_arr[0];
That's it .

foreach php nested array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
How foreach this array to < a href="path-to-jpg-file">name< /a>
Array
(
[0] => Array
(
[0] => 01-siena-rosso-new.jpg
[1] => Siena Rosso
)
[1] => Array
(
[0] => 02-siena-noce-new.jpg
[1] => Siena Noce
)
[2] => Array
(
[0] => 03-zloty-dab.jpg
[1] => Złoty Dąb
)
)
Try loop like this:-
foreach($array as $value) {
echo ''.$value[1].'';
}
Working Demo
Expanding on Roopendras, you just need preg_replace:
foreach($array as $value) {
echo ''.$value[1].'';
}

How to compare arrays and objects to find a match PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My first question is, how do I search values of my array inside the objects array [words] and return object if match is found?
How do I search for a better match? In the example below, second object is a better match with 2 words in common, rather than first with only 1 match.
Array
(
[0] => blue
[1] => green
[2] => love
[3] => sandro
)
stdClass Object
(
[1] => stdClass Object
(
[words] => Array
(
[0] => green
[1] => blue
)
[html] => html+img+link+code
)
[2] => stdClass Object
(
[words] => Array
(
[0] => love
[1] => sex
[2] => blue
)
[html] => html+img+link+code
)
)
Code I tried:
foreach ($ads_arr as $ad) {
print_r(array_intersect($ad->words,$words_arr));
}
You can use a forloop for your case, but you should consider defining real php class (not stdClass ) and implement some methods to help you.
foreach($main_std as $id => $sub_std){
$count_match[$id] = 0;
// now, check for each objects
// you can use an other loop with in_array, array_intersect
// or any other way
foreach($the_array as $word_search)
{
// for each word you're looking for, add +1
if (in_array($word_search, $sub_std->words))
$count_match[$id] ++;
}
}
// here, $count_match is an array you can sort by best match or whatever you want
Try out array_intersect() :
$output = array_intersect($array1, $array2);
print_r($output);

fetch data inmultidimensional array in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've got a problem.
I have got this array:
Array (
[0] => Array (
[id] => 1
[opt] => reg_limit
[value] => 0
)
[1] => Array (
[id] => 3
[opt] => pages_offline
[value] => []
)
[2] => Array (
[id] => 4
[opt] => devolp
[value] => TRUE
)
)
I want to check if the [opt] devolp has the [value] TRUE in the third array. How can I do?
$aArray = Array (
[0] => Array (
[id] => 1
[opt] => reg_limit
[value] => 0
)
[1] => Array (
[id] => 3
[opt] => pages_offline
[value] => []
)
[2] => Array (
[id] => 4
[opt] => devolp
[value] => TRUE
)
)
foreach($aArray AS $aInnerArray){
if($aInnerArray['opt'] == 'devolp' && $aInnerArray['value'] == TRUE){
//YOUR USE CASE
}
}
if ($array[2]['value']) echo 'true';
Since the OP's question is quite vague on the details wether or not he knows which array key he needs to check,
Here is a simple example that you can use if you know the array key that you need to check in.
$bool = $yourMultiDeminsionalArray[2]['value'];
if ($bool) {
//Do some awesome PHP shizzle here
}
Provided you will work with a big array in the future and need some flexibility, this foreach will work for you:
foreach($array as $a) {
if(array_key_exists("opt", $a) && $a['opt'] == "devolp") {
if(array_key_exists("value", $a) && $a['value'] == TRUE) {
echo "Found it!";
//Do whatever you need to do here....
}
}
}

Categories