I am trying to make a custom voting plugin for wordpress.
I want to use post_meta to store the data.
It should have two sets of informations (user_id and a timestamp) for every vote in every post.
So I set up an array with the data:
$up_vote = Array(
'ls_user_id' => $current_user->ID,
'ls_timestamp' => current_time('mysql')
);
The data inside the post_meta should be an Array of these Arrays.
To do this, I first create an empty Array, get the old data, and then push my first array inside like this:
$ls_up_votes = Array();
$ls_up_votes = get_post_meta(get_query_var('ls_id'), 'ls_up_votes');
array_push($ls_up_votes, $up_vote);
And later I store it like this:
update_post_meta(get_query_var('ls_id'), 'ls_up_votes', $ls_up_votes);
Now the problem:
Always when I try to get the data (and work with it), it gives me an Array which has only one element (sizeof() returns 1).
How can I store a multi-dimensional Array here?
What am I doing wrong?
thanks for your time! :)
[Updated]
I just noticed that you're using array_push; instead you should be using array_merge. Otherwise you're going to push an array into the existing array making it multi-dimensional, i.e. see note.
You'll also want to make sure you're using the correct params in get_post_meta, see below.
Make sure to use return meta as an array and not a serialized string, get_post_meta has a third paramater that should be set to TRUE if you want an the array returned.
Example:
get_post_meta( $post_id, 'ls_up_votes', TRUE );
Related
If I would like to match the word that I need to find it the same as <lookup> in Excel. I intend to create a form for Example. I have a file certain big data and I will create a box for fill in data that you need then Enter it show the Description of data. Now I got stuck I don't know how to write to the script I have learned in youtube but don't have a solution that nearby with my need it nearby just <Indexof> function.
var data = Activesheet.getRange(1,1,Activesheet.getLastRow()-1,1).getValues();
Logger.log(data.indexOf("TPBSA"));
data is a 2D(two dimensional) array. indexOf only works with a 1D array. flatten the array before indexOf:
const data = [["A1"],["A2"],["TPBSA"],["A4"]];
console.info(data.flat().indexOf("TPBSA"));
//or
console.info(data.findIndex(e=>e[0]==='TPBSA'))
I'm using React Native and Open Weather Map API to build a small weather app. The data I retrieve from the API call is more than I want to use so I parse out just the pieces I need and store them in an array. I then take the array and set it to a state object. I can reference the object, but instead of saying my array is an array, it says it's an object, and thus won't let me use any array methods on it. How do I get around this?
//reponseData is the data retrieved from the API call; the data retrieved is an object with arrays and objects
within. The forecast data for the next five days is given in 3 hour increments, so you have a 40 item array of
data pieces. I loop through this list of 40 items, pull out just what I need...
let forecastArray = [];
for (let i=0; i<responseData.list.length; i++) {
let day = responseData.list[i].date
let high = responseData.list[i].weather[0].hiTemp
let low = responseData.list[i].weather[0].loTemp
let condition = responseData.list[i].sys.condition
forecastArray.push(day)
forecastArray.push(high)
forecastArray.push(low)
forecastArray.push(condition)
this.setState({
forecastData: forecastArray
})
When I log, I get an array....
console.warn("forecast is: ", this.state.forecastData)
OUTPUTS: forecast is: ["11-06-2019", 52.5, 47.3, "sunny", "11-06-2019", 63.9, 39.7, "sunny", ...]
Referencing this.state.forecastData[2], for example, however was giving me errors. So I checked the typeof this.state.forecast to see why and it says the array is an Object? I need to further divide out the array data and manipulate it. The first several items (e.x. forecastData[0] through forecastData[9] would be for the forecasted weather for 11-06-2019 at 3pm, ,6pm, 9pm so I need to pull those items, get the highest high and lowest low, etc. I can't do that since I can't even reference the items in the array.
Things I've tried:
using Object.entries and Object.assign methods, but that just splits the items into several arrays, with the first item being the location number and the second item being the array item content. I've tried manipulating the array within the component that uses it, but it still is an Object not an Array, so I can't reference the individual items. The data set is large enough I don't think it would be best practice to push each of the 40+ items into their own state object key.
in Javascript array is a subset of object , i'e it has the same prototype. So typeof Array will be an object. Thats not an issue. Can you update with the error which you are getting while accessing this.state.forecastData[2] coz what i . believe its something not with syntax , rather with the duration of API call.
I would recommend when accessing this.state.forecastData[2] first check if its length is greater than 0 , that way you are sure that there is data inside the array.
constructor(props){
forecastData:[]
}
and when you use it ,
if(this.state.forecastData.length > 0){
this.state.forecastData[2]
}
Try this, and revert with doubts.
Thank you, Gaurav Roy! You were partly correct. The typeof didn't matter at all. But the issue wasn't with the duration of my API, it was that my component was trying to render when it didn't have the data from the API call yet! I put in a conditional
{this.state.forecast !== null && <Forecast forecast=this.state.forecastData />}
and got things working now. Thanks for the quick reply!
I'm having trouble getting a specific set of values from an array of arrays because the values come up as aundefineda. my array is stored in state and set up like this:
survey: [{names: ["sara", "tom"]}, {age: ["17", "33"]}, {col: ["blue", "green"]}]
To access just the names, I've been trying to use the line:
console.log(this.state.survey.names)
When I check, the console has "undefined" for the names. When I check the value of survey, it shows up as an array of arrays, where each nested array has two values stored inside.
I want to check this because I am trying to map out the nested arrays and mapping out Survey names isn't working because the length shows 0. I am successful in mapping out nested arrays when they are not stored in state, so I am wondering what I am missing or if my syntax is incorrect.
You're trying to access an array as if it was an object, instead of an array of objects. A simple fix is -
this.state.survey.find(item => item.names).names
In the long term, unless you have a reason to store this as an array, it would be a lot easier to reorganize and store as objects. If you restructure this to be something like
survey: {
names: ['name1', 'name2'],
age: ...etc
If you are trying to say that Sara is 17 and her col is blue, why not just store that as a single object?
I am trying to append/remove items from an array inside of a Firestore Document but every time the entire array is replaced instead of the new value being appended. I have tried both of the following:
batch.setData(["favorites": [user.uid]], forDocument: bookRef, options: SetOptions.merge())
batch.updateData(["favorites": [user.uid]], forDocument: bookRef)
I know that instead of an array I can use an object/dictionary but that would mean storing additional data that is irrelevant (such as the key), all I need is the ID's stored inside the array. Is this something that is currently possible in Firestore?
Update elements in an array
If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present. arrayRemove() removes all instances of each given element.
let washingtonRef = db.collection("cities").document("DC")
// Atomically add a new region to the "regions" array field.
washingtonRef.updateData([
"regions": FieldValue.arrayUnion(["greater_virginia"])
])
// Atomically remove a region from the "regions" array field.
washingtonRef.updateData([
"regions": FieldValue.arrayRemove(["east_coast"])
])
See documentation here
Actually, nowadays it is possible. With latest updates db.collection.updateData
method actually appends new item to array instead of replacing it.
Example usage can be found in Firebase documentation.
If you need to do it manually, you can use
FieldValue.arrayUnion([user.uid])
Nope. This isn't possible.
Arrays tend to be problematic in an environment like Cloud Firestore where many clients could theoretically append or remove elements from an array at the same time -- if instructions arrive in a slightly different order, you could end up with out-of-bounds errors, corrupted data, or just a really bad time. So you either need to use a dictionary (where you can specify individual keys) or replace the entire array.
I can append to a single array using
{append var='name' value='Bob' index='first'}
However, if I have a multi-dimensional array such as:
$name[first][last] = ['this','array']
and I want to append another value to the array at $name[first][last] e.g. to make the array like this:
$name[first][last] = ['this','array','appended']
how can I do this in the smarty template?
You can do this without using append:
{$name[first][last][] = 'this'}
{$name[first][last][] = 'array'}
{$name[first][last][] = 'appended'}
I must highlight though - templates should be used for specific purpose: to display prepared data; having to do the above is a code smell
I've tested many cases to try achieve it and I think it's not possible (in documentation there is also no info or example of multidimensional key or var)
You should also really think do you need it at all. Logic should be in PHP and role of Smarty is only displaying data not manipulating them