Echo a specific value on a very nested array - php

I have a function that generate an array (I did not made that function, comes from an API) and then saves it to a variable($customerList). I need to display just one value of an entire nested array; this array contains all this code (shown with print_r($customerList);) :
Array
(
[0] => Customer Object
(
[status:protected] =>
[creation_date:protected] => 2016-01-14T12:07:07-06:00
[balance:protected] =>
[clabe:protected] => 321654qweasd
[derivedResources:protected] => Array
(
[cards] => 123123123123
//a lot of code
How can I display just the value on "[cards]" using echo?
I have tried
//the for loop {
echo $customerList['0']['derivedResources:protected']['cards'];
And
//the for loop {
echo $customerList['0']['derivedResources']['cards'];
But it just display nothing. I haven't seen an array like this, so maybe is just a newbie mistake.
I think that "protected" thing has something to do with; but if I can display it, I can read/access it, right?
If any of you need the entire array (it's very long) I can put it here.
Thanks.

I haven't seen an array like this
It is an array of objects. Learn something about OOP.
This should work:
echo $customerList[0]->derivedResources['cards'];

Based on the print_r:
Array
(
[0] => Customer Object
(
[status:protected] =>
[creation_date:protected] => 2016-01-14T12:07:07-06:00
[balance:protected] =>
[clabe:protected] => 321654qweasd
[derivedResources:protected] => Array
(
[cards] => 123123123123
//a lot of code
We can simulate:
<?php
$CustomerObject = new \stdClass;
$derivedResources = new \stdClass();
$CustomerObject->derivedResources = ["cards" => 123123123123];
$customerList = [$CustomerObject];
// print_r($customerList);
echo $customerList[0]->derivedResources['cards'];
Which, uncommenting the print_r results in:
Array
(
[0] => stdClass Object
(
[derivedResources] => Array
(
[cards] => 123123123123
)
)
)
123123123123

Related

PHP array_merge_recursive with one array

I am struggling with a data structure in PHP. I'm trying to use array_merge_recursive to condense an array by like keys and then grab all values instead of having them overwritten. This is why I chose array_merge_recursive instead of array_merge.
My array is something similar to:
Array
(
[0] => Array
(
[App] => APP1
[Type] => DB
)
[1] => Array
(
[App] => APP1
[Type] => WEBSITE
)
[2] => Array
(
[App] => APP2
[Type] => IOS
)
)
I was expecting array_merge_recursive to combine like keys and then group the other elements into arrays however this is not the behavior I am seeing.
I am hoping to get an array like the following:
Array
(
[0] => Array
(
[App] => APP1
[Type] => Array
(
[0] => DB
[1] => WEBSITE
)
)
[1] => Array
(
[App] => APP2
[Type] => IOS
)
)
array_merge_recursive() does not do what you think it does. You are looking for a function that restructures an array based on specific rules that are helpful to you and as such there isn't a builtin php function for that. I.e. How would PHP know that you wanted to new array to be structured by APP rather TYPE. Assuming your array is always that simple, the easiest version of the function you want looks something like this:
function sortByApp($array){
$result = array();
foreach($array as $row){
if(!isset( $result[ $row['APP'] ] ) {
$result[ $row['APP'] ] = array(
'APP' => $row['APP'],
'TYPE' => array( $row['TYPE'] )
);
}else{
$result[ $row['APP'] ]['TYPE'] = array_merge( $result[ $row['APP'] ]['TYPE'], array($row['TYPE']) );
}
}
$result = array_values($result); //All this does is remove the keys from the top array, it isn't really necessary but will make the output more closely match what you posted.
return $result
}
Note, in this solution, the value of the TYPE key in every APP will always be an array. This makes handling the data later easier in my opinion since you don't have to worry about checking for a string vs an array.

Looping through JSON output from Hubspot deals API

I am trying to use the Hubspot API (http://developers.hubspot.com/docs/overview) to loop through all deals and find only those which are current and then do something with those deals.
No matter what I try to do I cannot get my head around how I access the data I need - below is an example of the output.
In the API there are lots of items like dealstage below and the value field under these is what I need to access - for example in this case the deal is closedlost. Another example would be amount which would also have an entry in value so I can then see the deal value.
I want to loop through all deals and for each deal get the dealstage, amount, last update, owner and so on. Each of these are contained in an array of the same layout as [dealstage] below with a value
I have gotten to where I can print the dealstage value for each deal but it doesn't really help - is there a better way of doing this?
foreach ($list['deals'] as $line) {
foreach ($line['properties'] as $row => $value) {
if ($row=="dealstage") {
$stage=$value['value'];
print $stage."<br>";
}
}
}
Example array:
Array
(
[deals] => Array
(
[0] => Array
(
[portalId] => 12345
[dealId] => 67890
[isDeleted] =>
[associations] => Array
(
[associatedVids] => Array
(
[0] => 4051
)
[associatedCompanyIds] => Array
(
[0] => 23456
)
[associatedDealIds] => Array
(
)
)
[properties] => Array
(
[dealstage] => Array
(
[value] => closedlost
)
[createdate] => Array
(
[value] => 1471334633784
)
[amount] => Array
(
[value] => 1000
)
Would something like this be what you are looking for. Loop through the array picking out the items you are interested in and place them in a nice simple array for you to use later when building your email.
$for_email = array();
foreach ($list['deals'] as $line) {
$t = array();
if (isset($line['properties']['dealstage']['value'])) {
$t['dealstage'] = $line['properties']['dealstage']['value'];
}
if (isset($line['properties']['amount']['value'])) {
$t['amount'] = $line['properties']['amount']['value'];
}
if (isset($line['properties']['createdate']['value'])) {
$t['createdate'] = $line['properties']['createdate']['value'];
}
// any other data you want to capture
// put this data in the new array
$for_email[] = $t;
}
// check what the new array looks like
print_r($for_email);

Filtering array of stdClass into single array

I want to be able to take an array of stdClass returned from PDO fetchAll and turn this into a single array.
So i get an array back that looks like this:
// print_r($getCategories); returns..
Array
(
[0] => stdClass Object
(
[field1] => 'foo',
[field2] => 'foo2'
)
[1] => stdClass Object
(
[field1] => 'bar'
[field2] => 'bar2'
)
)
I loop through this and put a list of one field into a combo box
// <select> and <option> around this..
foreach($getCategories as $category) {
echo $category->field1;
}
Now i know i could create an array by using the following code
foreach($getCategories as $category){
if($_POST['category'] == $category->field1){
$chosenCategory = array(
'field1' => $category->field1,
'field2' => $category->field2
);
}
}
I just want to know if there is a better way to do this where I don't have to build an array, instead just filter what is returned in $getCategories using user input?
I am fairly new to OOP and programming in general so an explanation as well as an answer would be much appreciated!
Thanks

Add data to multidimensional arrays php in a while loop

I have a problem adding data to a multidimensional array in a while loop.
My code lookes like this
while ($dataOmråde=mysql_fetch_array($område))
{
if(!in_array($dataOmråde['STED'], $aSted))
{
$aSted[] = $dataOmråde['STED'];
$aOmråde[$dataOmråde['BY']]['pladsnr'] = array($dataOmråde['PLADSNR']);
}
else
{
$aOmråde[$dataOmråde['BY']]['pladsnr'] = array($dataOmråde['PLADSNR']);
}
}
But this keeps overwrithing my data so I get a result like this.
Array ( [Annaberg] => Array ( [pladsnr] => Array ( [0] => O_DAC_ALP_001 )
Bu what I want is to append data to the pladsnr array, so the result should look like this.
Array ( [Annaberg] => Array ( [pladsnr] => Array ( [0] => O_DAC_ALP_001, [1] => new pladsnr, [2] => new pladsnr second )
I have tried array_push but cant get i to work. Hopes someone can help:-)
Regards, Andreas
As you did with $aSted, do:
$aOmråde[$dataOmråde['BY']]['pladsnr'][] = $dataOmråde['PLADSNR'];

How to access stdclass object after a specific key value pair?

I have a stdclass object as shown below:
stdClass Object
(
[text] => Parent
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Laurence W. Lane Jr.
[url] => http://www.freebase.com/view/m/0c02911
)
)
)
I iterate over multiple such objects, some of which have
stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
I was wondering how I would access the "values" object if it comes after a "text" that has "Parent" as its value?
there are serveral ways to turn it to array:
First Solution:
$value = get_object_vars($object);
Second Solution:
$value = (array) $object;
Third Solution
$value = json_decode(json_encode($object), true);
to get value of converted array
echo $value['values']['0']['id'];
The alternate way to access objects var without convert the object, try
$object->values->{'0'}->id
Expanding (or rather minimalizing) upon answer by Somwang Souksavatd, I like accessing Object values like this:
echo get_object_vars($object)['values']['0']['id'];
I had the same issue, still not so sure why but I was able to get it working using this workaround:
$k2 ="1";
$elements = json_decode('{"id":"1","name":"User1"}');
//$elements['id'] == $k2; //****Not Working
$tmp = (object)$elements;
$tmp = $tmp ->id; //****Working
//$tmp =$elements['id'] ; //****Not Working
return $tmp == $k2;
I have to say that sometimes accessing the element as array works and some times not,(On PHP7 it worked for me but on PHP5.6 it didn't).
$elements can be Array to but I chose to demonstrate with json string.
I hope this helps somehow !!!
$Obj=stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
$Values= $result->values;
$Item = $Values[0];
$id=$Item->id;
$text = $Item->text;
$url=$Item->url;
I'm doing the same thing and all I did was this;
<?php
$stdObject = json_decode($stdClassObject);
print $stdObject->values[0]->id;
this can help you accessing subarrays in php using codeigniter framework
foreach ($cassule['tarefa'][0] as $tarefa => $novo_puto_ultimos_30_dias) {
echo $novo_puto_ultimos_30_dias;
What you are looking for is the Object['values'][0]: 'values' is the keymap just like 'text', and [0] is the index inside that array you wish to access. so if you would like to get the id deep in the nest, you'd have to do something like
Object['values'][0]['id']
or
Object['values'][0]->id
which should give you /m/0c02911. But I have no idea how you are doing your loop, so you will have to adjust it to your needs and place proper variables where they need to go in that code in your loop. Not exactly sure which language you are working with.

Categories