Add data to multidimensional arrays php in a while loop - php

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'];

Related

Array values to single array using foreach loop in PHP

I am working with php and arrays, I have multiple arrays like following
Array
(
[0] => Array
(
[wallet_address] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[1] => Array
(
[wallet_address] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[2] => Array
(
[wallet_address] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
and so on....
And i want to make them in single array with comma like following way
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
How can i do this ?Here is my current code but not working,showing me same result(0,1,2 keys),Where i am wrong ?
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[]=$arr;
}
echo "<pre>";print_R($set);
The original array is an Assoc array and therefore the wallet_address needs to be addressed specifically in a loop. Or you could use the array_column() builtin function to achieve the same thing.
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[] = $arr['wallet_address'];
}
echo "<pre>";print_r($set);
Or
$new = array_column($GetUserFollower, 'wallet_address');
print_r($new);
RESULT
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
Your comments are making me think you want an array without a key, which is impossible. If you do this with the example you show in your comments
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
print_r($set);
You will see
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)

php get values from array json output

When I use the code below:
print_r($jsoni);
$badge_url = "http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid=841370%3Fkey&steamids=76561198108211948&fbclid=IwAR0B4wUlosbqFElHBJw-AkLwb3mGsv42xKdtrEAarDmD97Ur3AprrkW4tCk";
$jsoni = json_decode(file_get_contents($badge_url), true);
I get the following as a result:
Array
(
[achievementpercentages] => Array
(
[achievements] => Array
(
[0] => Array
(
[name] => GAME_GREEN_LIGHT
[percent] => 70.9000015259
)
[1] => Array
(
[name] => CAREER_EARN_BADGE
[percent] => 62.2999992371
)
How can I get it so that it only shows the name and the percentage?
print_r($jsoni['achievementpercentages']['achievements'])
You could loop through the decoded data to create an associative array with the achievement names as keys and the percentages as values:
$achievements = [];
foreach($jsoni['achievementpercentages']['achievements'] as $achievement)
$achievements[$achievement['name']] = $achievement['percent'];
Which outputs:
Array
(
[GAME_GREEN_LIGHT] => 70.9000015259,
[CAREER_EARN_BADGE] => 62.2999992371
)
So im now using the following code.
<?php
$achievements = [];
foreach($jsoni['achievementpercentages']['achievements'] as $achievement)
$achievements[$achievement['name']] = $achievement['percent'];
print_r($achievements);
?>
wich results in
Array ( [GAME_GREEN_LIGHT] => 70.9000015259 [CAREER_EARN_BADGE] => 62.2999992371
how do i get it to show a list from top to bottom instead of in a cluster of text?
Thanks for the help!

PHP Build An Array By Looping Back On Self Until All Records Are Found

I have a function in PHP (using Symfony) where I am building an array by passing a value into the array first, and then I am looping through every record but changing the key/value each time until I run out of values...
For example:
I pass this value into my getFieldKeys() function listed below:
array(
'fieldKey'=>'123'
);
Inside of this function, I first add this value to the fieldKey array and then see if there are any records that match my query...
If there are records then loop through each of those records and send the fieldKey the value back to the same function and add the value to the array...
This all works but my array look aweful. Need help cleaning up this code, not sure what to do...
public function getFieldKeys($array){
$em = $this->getEntityManager();
$fieldKeys[] = $array['fieldKey']; // add original value to fieldKey array...
// loop over any results and and pass key back into this same function and then do a look up on that key and repeat the process until finished...
$keys = $em->getRepository('AppBundle:FieldKeys')->findBy([
'fieldKey' => $array['fieldKey'],
]);
foreach($keys as $key) {
$fieldKeys[] = $this->getFieldKeys([
'fieldKey'=>$key->getFieldKey(),
]);
}
return $fieldKeys;
}
My final array looks like this - yikes!
Array
(
[0] => ccrs_date
[1] => Array
(
[0] => prelim_title_report_date
[1] => Array
(
[0] => additional_escrow_deposit_date
[1] => Array
(
[0] => earnest_money_date
[1] => Array
(
[0] => acceptance_date
[1] => Array
(
[0] => contract_date
[1] => Array
(
[0] =>
)
)
)
)
)
)
)
What I am hoping for is something more like this...
Array
(
[0] => ccrs_date
[1] => prelim_title_report_date
[2] => additional_escrow_deposit_date
[3] => earnest_money_date
[4] => acceptance_date
[5] => contract_date
[6] => prelim_title_report_date
)
Thanks!
You have written a recursive function to achieve your keys array - good idea. However your function returns an array and you are adding the resulting array recursivley to the current array which causes an array nesting one down per level.
This might be what you need as your second key is always an array but you are just looking for the key:
public function getFieldKeys($array){
$em = $this->getEntityManager();
$fieldKeys[] = $array['fieldKey']; // add original value to fieldKey array...
// loop over any results and and pass key back into this same function and then do a look up on that key and repeat the process until finished...
$keys = $em->getRepository('AppBundle:FieldKeys')->findBy([
'fieldKey' => $array['fieldKey'],
]);
foreach($keys as $key) {
$tmp = $this->getFieldKeys([
'fieldKey'=>$key->getFieldKey(),
]);
$fieldKeys[] = reset($tmp);
}
return $fieldKeys;
}

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

Echo a specific value on a very nested array

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

Categories