i can't get dynamic datas that are contained into an array, here the array
[addresses] => Array
(
[DYNAMIC-ADDRESS] => -249310
)
I'am tryng to get the DYNAMIC-ADDRESS, or rather the text contained on the ['...'] (In this case the text is DYNAMIC-ADDRESS) for get the balance, and for do a check on my database, i have already tried
$ReceiveAddress=$DATA['addresses']['0'];
And then for get the balance
$ReceiveAddress=$DATA['addresses']["$ReceiveBitcoinAddress"];
But this doesn't work...
In case this is the only key-value pair in the array, you can use key.
http://php.net/manual/en/function.key.php
$dynamic_address = key($array);
$value = $array[$dynamic_address];
In case you're handling an array with several elements, Consider using foreach loop.
[addresses] => Array
(
[DYNAMIC-ADDRESS] => -249310
)
foreach($addresses as $param => $value){
//$param would be DYNAMIC-ADDRESS in a specific "round"
//$value would be -249310
}
I would consider to change the format of the returned data's array, unless its a 3rd party API - there's no reason you should work with unknown key's names.
Related
I'm trying to add new key => value pairs to an array that I have. The keys are integers that correspond to user IDs. The values would initially just be the string "requested".
The array in question is stored in serialized form at index zero of another array users_attending in a larger array of metadata related to the post that I am modifying.
My process is like this:
Pull a variable that might contain the array from a certain WordPress function. If there's no array then that variable is just empty, we wouldn't see an error.
If the array variable is non-empty and does not contain a key that is equal to the user's ID, then add the key => value pair.
If the array variable is non-empty and does contain a key that is equal to the user's ID, then print that the user is already there.
In all other situations (meaning when the array variable is empty) then create a new array and add the first key => value pair.
However, via this process I see all newly added arrays nesting within first array, then the second array, and so on. When I test the contents of the array with print_r($eventUsers), I see this kind of output:
Array
(
[0] => Array
(
[0] => Array
(
[4] => requested
)
[4] => requested
)
[4] => requested
)
For context, here is how users_attending section looks in the larger array of all post metadata (it is automatically serialized by the update_post_meta() function):
[users_attending] => Array
(
[0] => a:2:{i:0;a:2:{i:0;a:1:{i:4;s:9:"requested";}i:4;s:9:"requested";}i:4;s:9:"requested";}
)
Here's the relevant code:
$currentUser = get_current_user_ID(); //let's say this is 4 for now
$eventUsers = get_post_meta(get_the_ID(), 'users_attending');
if (!empty($eventUsers) && !array_key_exists($currentUser, $eventUsers)) {
$eventUsers[$currentUser] = "requested";
update_post_meta(get_the_ID(), 'users_attending', $eventUsers);
//array exists but user is not yet attending
} elseif (!empty($eventUsers) && array_key_exists($currentUser, $eventUsers)) {
//user is already attending
} else {
$eventUsers = array();
$eventUsers[$currentUser] = "requested";
update_post_meta(get_the_ID(), 'users_attending', $eventUsers); //note that this adds the metadata key and value if it's not already there
//first user attending
}
What can I do to make sure that the key => value pairs are being added to the first array within the zero index of users_attending instead of this nesting behavior that I am seeing?
The program looks ok, try deleting the data from previous runs.
Also consider the function get_post_meta maybe returns an array of arrays instead of the array you want.
I am trying to figure out how to reorganize an array..
I have a multidimensional array(Ill call that original_array) and I would like to take the first array within original_array and set the values as keys in a new array. I also want to take the values of the second array in original_array and make them keys and then set the values of the third array in original_array as the values for those keys.
Here is an example of original_array:
Array (
[id] => Array (
[0] => 1
[1] => 3
)
[reward] => Array (
[0] => Movie
[1] => Trip
)
[cost] => Array (
[0] => 50
[1] => 200
)
)
Basically what I would like to do is look like this:
Array (
[1] => Array (
[Movie] => 50
)
[3] => Array (
[Trip] => 200
)
)
Is there a simple and elegant way to merge these like this?
I have spent hours trying to figure this out using array_merge, array_merge_recursive.. etc. And have search SO far and wide for a similar questions, but I haven't found anything that does what I am after.
I was able to correctly combine the 2nd and 3rd arrays in original_array with array_combine. But, I am at a loss as how to combine that result with the 1st array's values in original_array.
Thanks in advance to any help!
Well, the dirty way would be just use combine array functions like array_combine with the input:
$new_array = array_combine(
$array['id'], // parent keys
// combine chunked combined sub keys :p
array_chunk(array_combine($array['reward'], $array['cost']), 1, true)
);
There may be some incantation of array_*() merging functions that could produce what you're looking for, but it is far easier to just iterate over the original array's [id] sub-array and use its values to create new sub-array keys in a different output array.
// To hold your output
$output = array();
// Iterate the original array's [id] sub-array
foreach ($original['id'] as $idxkey => $newkey) {
// Add a sub-array using $newkey to the output array
$output[$newkey] = array(
// Using the index (not value), retrieve the corresponding reward
// value to use as the new array key
// and corresponding cost to use as the new subarray value
$original['reward'][$idxkey] => $original['cost'][$idxkey]
);
}
Here is a demonstration: https://3v4l.org/2pac3
This should work for you:
First you can get the keys for the main array into a separate variable with array_shift(), which will just remove the first element from your array, which is the array holding the keys.
Then use array_map() to loop through both of your subArrays and use reward as key with the cost values as value and return it in an array. At the end you just have to array_combine() your keys $keys with the new created array.
Code:
<?php
$keys = array_shift($arr);
$result = array_combine($keys, array_map(function($k, $v){
return [$k => $v];
}, $arr["reward"], $arr["cost"]));
print_r($result);
?>
You might wanna take a look at BaseArrayHelper from Yii 2.0 Framework.
Although this file is part of a framework it has only very few dependencies and you should be able to use just this file or parts of it in your code with small modifications.
An example for your use case can be found in the index() method.
I am reading a GEDCOM-formatted family tree flat file, and producing an array from the data for staging into table. If I encounter the values CONC <some value>, then, instead of adding an element, I need to append <some value> to the value of the last element that was just inserted (regardless of dimension depth).
I tried with current(...) etc but does this work for a multidimensional associative array?
please consider following element in an array:
[#N163#] => Array ( [INDI] => Array ( [TEXT] => Some data of this person) )
if the next line reads "1 CONC including his profession"
instead of adding a line as such
[#N163#] => Array (
[INDI] => Array ( [TEXT] => Some data of this person)
[INDI] => Array ( [CONC] => including his profession) )
I would like the array to look as follows:
[#N163#] => Array (
[INDI] => Array ( [TEXT] => Some data of this person including his profession) )
What I have researched thus far:
end($theArray)
to set pointer to last inserted element followed by $theArray[key($theArray)] = .... to update this element.
But I did not get this method to work for multidimensional arrays and/or it became really messy.
And:
merging two arrays using e.g. += notation,
but this only seems to overwrite a new element, not affect the last one, if keys are same
And:
examples with foreach calls, which does not help in my case.
Hope somebody can shed some light... many thanks!
When you adding $array[#N163#][INDI][TEXT] = 'smtng'; you can save position
$pos = &$array[#N163#][INDI][TEXT];
And if you need concatenate, write
$pos .= "concate line";
I am using an API which has a lot of data inside lots of arrays which as you may know can be quite confusing.I am relatively new to API's and this one in particular has no documentation.
My code below is grabbing the recent_games() function which is pulling the whole API then I am using foreach loops to get inside the data.
$games = $player->recent_games();
foreach($games['gameStatistics']['array'] as $key => $gameStatistic) {
$game_date[strtotime($gameStatistic['createDate'])] = $gameStatistic;
}
// order data
krsort($game_date);
foreach ($game_date as $game => $data) {
$statistics[$data] = $data['statistics'];
}
I am getting errors such as illegal offset for:
$statistics[$data] = $data['statistics'];
Is there a way to continue down the nesting of arrays ($game_date) to get to the data that I need?
Let me know if you need more info.
Thanks
EDIT more info:
The first foreach loop at the top loops a unix timestamp key per game. Looks like this:
[1370947566] => Array
(
[skinName] => Skin_name
[ranked] => 1
[statistics] => Array
(
[array] => Array
(
[0] => Array
(
[statType] => stat_data
[value] => 1234
)
[1] => Array
(
[statType] => stat_data
[value] => 1234
)
As you can see its quite nested but I am trying to get to the individual statistics array. I hope that helps?
$statistics[$data] = $data['statistics'];
There is absolutely no way this line is correct.
The right hand side uses $data as if it were an array, indexing into it. The left hand side uses $data as a key into an array. Since the only valid types for keys are strings and integers, $data cannot satisfy the requirements of both expressions at the same time -- it cannot be an array and a string or integer.
It's obvious from the error message that $data is in fact an array, so using it as $staticstics[$data] is wrong. What do you want $statistics to be?
I have been working with parsing some remote JSON with PHP. I have been able to download the JSON and assign it to a variable, and I have used the array functionality with json_decode:
$data = json_decode($remotejson, true);
I have then printed the complete array back to verify the contents of the array:
echo print_r($data);
The array prints back and I can see the keys and values:
[files] => Array
(
[/photogalleryupload.thumbs/1934307_000001.jpg] => Array
(
[source] => derivative
[format] => Thumbnail
[original] => moviefile_1934307.mp4
)
I am trying to get the value of the first nested key name which is "/photogalleryupload.thumbs/1934307_000001.jpg" and assign it to a variable.
For example, I would like the following code:
echo $data['files'][0];
To return this:
/photogalleryupload.thumbs/1934307_000001.jpg
This does not work.
The difficulty I am having is that this value I am trying to return is a 2nd level key name and I have been having trouble finding a way of assigning it to a variable.
$keys = array_keys($data['files'])
$key = $keys[0]