Find value in Multidimensional array by array of keys - php

I am working on a script that lets the user load a remote XML file and lets them choose an element. I then need to be able to retrieve the value of that element a later date. The XML is updated regularly and I want to display the updates value each time.
So far I convert the XML into a multidimensional array, display the elements and their values to the user, and when they choose an element I save the keys of the multidimensional array.
So for example if we have the following array:
Array
(
[responsecode] => 0
[message] =>
[items] => Array
(
[0] => Array
(
[title] => Example1
[content] => This is the first message
[date] => 00/00/00
)
[1] => Array
(
[title] => Example2
[content] => This is the second message
[date] => 00/00/00
)
)
)
If the user chooses the first title element I save the path as follows:
$path = "itmes>0>title";
I then explode the string to get the separate keys:
$keys = explode(">", $path);
Array
(
[0] => items
[1] => 0
[2] => title
)
If I wanted to read the value manually I would use:
array['items']['0']['title']
But how would I build that query when I have an array of they keys?

Just write a loop:
function extract_value(array $array, array $keys) {
foreach($keys as $key) {
if (!isset($array[$key])) return null;
$array = $array[$key];
}
return $array;
}
You would use this as in
$result = extract_value($data, $keys);
The idea is that you have a variable that "points to" an element in the array, and you update it by branching with each key value. When there are no more keys the pointer points to your result.

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

Sort array based on key value

I'm using a form where the pair of inputs can be added per automatic. One store value in select and the other as input
After submit I receive the values in array and I need to associate them together. So all key values [0] belongs together and all [1] and so on.
Array
(
[issue] => Array
(
[0] => 2
[1] => 3
)
[qty] => Array
(
[0] => 1
[1] => 2
)
)
How can I do this using PHP?
Just use a simple foreach loop.
$combined = array();
foreach ($_POST["issue"] as $k=>$v) {
$combined[$k] = array($_POST["issue"][$k], $_POST["qty"][$k]);
}
print_r($combined);

Multiple foreach loops and accessing values outside

I'm trying to access multiple values in multiple foreach loops outside of the loops:
foreach(array1 as arr1) {
$var1 = arr2['value1']; //$array is associative array with mutliple keys value1
}
Then I have another
foreach(array2 as arr2) {
$var2 = arr2['value']; //$array2 is another associative array with multiple keys value
}
All of this is within another big foreach loop and now I want to create an array within the big foreach with $var1 and $var2 being used. This array I'm going to be creating is going to be an associative array as well. Any ideas how I can do this?
Array 1:
Array
(
[0] => Array
(
[id] => 1
[id_name] => 251452
[name] => bob
)
[1] => Array
(
[id] => 2
[id_name] => 251453
[name] => bob
)
)
Array 2:
Array
(
[0] => Array
(
[id_person] => 4
[id_last_name] => 251452
[last_name] => smith
)
[1] => Array
(
[id_person] => 15
[id_last_name] => 251453
[last_name] => johnson
)
)
Assume these come from two different queries from the database.
I want to get the first name from the first array for each one and get the last name from the second array for each one and make one array that has this data along with others.
Have a look into multi dimensional arrays.
Also have a look at stdclass and maybe creating an array of these which can store many variables within a single definition - which can help in many ways. (standard class)
Nested looping - this would just dump all sub arrays into an object
// Object and array examples for an InnerArray
$Object = new stdclass();
$AllOfIt = array();
$cnt = 0;
foreach($OuterArray as $OuterKey => $InnerArray)
{
$cnt++;
foreach($InnerArray as $InnerKey => $InnerValue)
{
$Object->$cnt = $InnerValue;
$AllOfIt[$cnt] = $InnerValue;
}
}

Categories