First i am a beginner in programming in general, i am trying to create a program for using gps locations from Lightroom on a map in googlemaps.
When i use the print the strings below ti the screen i see 5 different value's, this is also what i want, but...
I want to create also 5 different markers on the map this is done by the addMarkerByCoords Function but how can i use the 5 value per strings in the function ?
I have tried array, foreach but i cannot getting to work. The not working part can and probably will be my fault. LOL
print_r ("$Loncoord");
print_r ("$Latcoord");
print_r ("$gui");
//$map->formatOutput = true;
$map->addMarkerByCoords("$Loncoord","$Latcoord","$gui",'<b>Old Chicago</b>');
Can somebody give me a hint ?
To: Jonathan Sampson:
outputs print_r :-5.68166666667, +24.6513888889,IMG_3308,index.html,Landschap
To: Anti Veeranna I removed the " marks (and the program still works), but can you explain why this is better ?
And to the others Thank you very very much for the effort,work and really quick responses.
Assuming that this is PHP, you could us an array of arrays, and then loop.
Something like this:
$items = array(
array(
'long' => 12.34567,
'lat' => 34.56789,
'gui' => '????',
'location' => 'old chicago'
),
...
array(
'long' => 12.34567,
'lat' => 34.56789,
'gui' => '????',
'location' => 'old chicago 5'
)
);
foreach ($items as &$item) {
$map->addMarkerByCoords(
$item['long'],
$item['lat'],
$item['gui'],
$item['location']
);
}
unset($item);
$map->addMarkerByCoords(Array($Loncoord, $Latcoord, $gui, '<b>Old Chicago</b>));
??
Related
I have a problem with the array PHP function, below is my first sample code:
$country = array(
"Holland" => "David",
"England" => "Holly"
);
print_r ($country);
This is the result Array ( [Holland] => David [England] => Holly )
I want to ask, is possible to make the array data become variables? For second example like below the sample code, I want to store the data in the variable $data the put inside the array.:
$data = '"Holland" => "David","England" => "Holly"';
$country = array($data);
print_r ($country);
But this result is shown me like this: Array ( [0] => "Holland" => "David","England" => "Holly" )
May I know these two conditions why the results are not the same? Actually, I want the two conditions can get the same results, which is Array ( [Holland] => David [England] => Holly ).
Hope someone can guide me on how to solve this problem. Thanks.
You can use the following Code.
<?php
$country = array(
"Holland" => "David",
"England" => "Holly"
);
foreach ($country as $index => $value)
{
$$index = $value;
}
?>
Now, Holland & England are two variables. You can use them using $Holland etc.
A syntax such as $$variable is called Variable Variable. Actually The inner $ resolves the a variable to a string, and the outer one resolves a variable by that string.
So there is this thing called
Destructuring
You can do it something like ["Holland" => $eolland, "England" => $england] = $country;
And now you have your array elements inside the variables.
Go read the article above if you want more information about this because it gets really useful (especially in unit tests usind data provders from my experience).
If you want to extract elements from an associative array such that the keys become variable names and values become the value of that variable you can use extract
extract($country);
To check what changed you can do
print_r(get_defined_vars());
Explanation on why the below does not work
$data = '"Holland" => "David","England" => "Holly"';
When you enclose the above in single quotes ' php would recognise it as a string. And php will parse a string as a string and not as an array.
Do note it is not enough to create a string with the same syntax as the code and expect php to parse it as code. The codes will work if you do this
$data = ["Holland" => "David","England" => "Holly"];
However, now $data itself is an array.
A simple copy of an array can be made by using
$data = $country;
Hey I am trying to pass data back in json format using json_encode.
However it seems to return like this:
[{‘county’:’us’,’sector’:’retail’}]
However the end user has said that they expect the response without the square brackets.
How do I do this?
Thanks.
Depends on how you are generating the value you encode... json_encode has an option JSON_FORCE_OBJECT you could use but from what I can see that's probably not what you want, as it seems to me this would only turn the "square brackets" into a wrapper object. Nevertheless, if you'd like to try:
json_encode($value, JSON_FORCE_OBJECT);
Edit: as others have said, it seems likely json_encode($value[0]) is what you want.
Assuming your array is:
$data = [
'country' => 'us',
'sector' => 'retail',
];
Then just return a json_encoded array like so:
json_encode($data);
// {"country":"us","sector":"retail"}
However, if your array with data is "nested" like so:
$data = [[
'country' => 'us',
'sector' => 'retail',
]];
then return the encoded first element of it:
json_encode($data[0]);
// {"country":"us","sector":"retail"}
My experience of this happening is that you're accidentally storing your data as the first item of an array.
For example you may have done:
$json_store = Array();
$json_store[] = Array('country' => 'us', 'sector' => 'retail');
$json = json_encode($json_store);
A way of testing for this is to see if doing the following removes the brackets:
$json_encode($json_store[0])
If it does, you've a unnecessarily nested array that you should fix. Using the above line probably should be avoided as it's better to fix how the data is stored in the first place.
Ok I am making a blog system using MongoDB as the back end. I want to do the same thing as wordpress when you edit it saves the past versions and allows you to revert to them if needed.
I would like to do the same.
I have a few ways to doing it. but un sure if this is the best easiest way to do it and would like some suggestions.
the first is a find and the insert $SET
<?php
$cursor = $collection->find(array("_id"=> new MongoId($data)));
if ($cursor->count() > 0)
{
while( $cursor->hasNext() ) {
foreach($cursor->getNext() as $key => $value)
{
define("_".strtoupper($key), $value);
}
}
$cursor = $collection->update(array("_id" => new MongoId($data)),
'$set'=>array("title"=>$data['TITLE'], "content"=>$data['content'], "past_versons"=>array("title" => _TITLE, "content" => _CONTENT)));
}
?>
So my question is this the way I would do it.
here a sample JSON
{
"title":"blog title",
"content":"blog content",
"past_verson":[{"title":"blog title past","content":"past blog content"}]
}
In your example, you're iterating over a MongoCursor even though you only expect to work with one document at most. MongoCollection::findOne() is going to be more appropriate here, as it will return the document array of the first result or null if no documents matched.
Even if you needed to iterate across multiple results, you'd do better to take advantage of the iterable nature of MongoCursor (it implements Iterator, which extends Traversable, which means you can use it in a foreach loop). I also don't think define() is appropriate for storing what is essentially a temporary variable here. Moreover, suppose this code needed to run twice (error due to redefinition) or the title or content was not a scalar (another error).
Consider the following rewrite (I assumed $data['id'] is correct and you made a typo in the above code by using $data alone for the MongoId):
$document = $collection->findOne(array('_id' => new MongoId($data['id'])));
if (null !== $document) {
$collection->update(
array('_id' => new MongoId($data['id'])),
array(
'$set' => array(
'title' => $data['title'],
'content' => $data['content'],
),
'$push' => array(
'past_versions' => array(
'title' => $document['title'],
'content' => $document['content'],
),
),
)
);
}
One deviation here, which you may not need, is that I'm aggregating old versions into a past_versions array. We can use the $push operator to append to an array field in the document. Alternatively, you could simply use $set if you only need/want to store the most recent version.
I want to append the key from $teasers to the key of the array contained within each $teasers element and then create an associative array that contains the appended keys for its key and the content of the sub array for its content. Following? I hope so, if not here's an example of my desired end product:
This is a simplified version of the multidimensional array I'm working with:
$teasers[0]=array('id' => 4,
'title' => 'How to determine the speed of an african swallow',
'content'=> 'Its really quite simple...')
$teasers[1]=array('id' => 5,
'title' => 'Man alleged to be cereal killer after breakfast',
'content' => 'Turns out eating a delicious bowl of froste...')
I'm working towards turning it into this:
$data Array(
'/*Key from subarray+$teasers key*/' => '/*content from subarray*/',
'id0' => '4',
'title0' => 'How to determine the speed of an african swallow',
'content0' => 'Its really quite simple...',
'id1' => '5',
'title1' => 'Man alleged to be cereal killer after breakfast',
'content1' => 'Turns out eating a delicious bowl of frosted...')
Still not following? I don't blame you. Maybe this will help. I was originally going about it like this until I realized you can't pass a variable into a foreach loop. ):
foreach ($teasers as $key => $blogentry) {
foreach($blogentry as $entrykey => $content){
//Basically I would like to append $key to the end of $entrykey
//so that I can essentially access all of the data from one array($data[])
$data[$entrykey.$key] = $content;
}
}
I'm more than happy to do anything I can to make things more clear or help you help me. Thanks for your time in advance!!
I think your code should work. Add $data = array(); before your loops.
Lets say I have an multidimensional string array:
.food = array(
'vegetable' => array(
'carrot' => 'blablue',
'potato' => 'bluebla',
'cauliflower' => 'blabla'
),
'Fruit' => array(
'apple' => 'chicken65',
'orange' => 'salsafood',
'pear' => 'lokkulakka'
)
);
is it possible to access the array by using index as numbers, instead of using the name of the key?
So for accessing chicken65 , I will type echo $food[1][0]; I don't want to use numbers as key, because its a big array and its more user-friendly if I use string as key and it will let me do for-loops on advanced level.
You can do foreach loops to achieve much the same thing as a typical for-loop.
foreach ($foods as $key => $value) {
//For the first iteration, $key will equal 'vegetable' and $value will be the array
}
$food[1][0] != $food[1]['apple'], so you cannot use numeric keys in this case.
try
$new_array = array_values($food);
however, variable can't start with .. It should start with $
you may want to try the function array_values but since you are dealing with multidemsional arrays, here is a solution posted by a php programmer
http://www.php.net/manual/en/function.array-values.php#103905
but it would be easier to use foreach instead of for.
You can use array_keys() on the array. The resulting array can be traversed via a for-loop and gives you the associative key.
I will show it to you for the first dimension:
$aTest = array('lol' => 'lolval', 'rofl' => 'roflval');
$aKeys = array_keys($aTest);
$iCnt = count($aKeys);
for($i = 0; $i < $iCnt; ++$i)
{
var_dump($aTest[$aKeys[$i]]);
}
You would need to do this for two dimensions but I would not recommend this. It is actually more obstrusive and slower than most other solutions.
I don't think there is something native to go this route.
And it does seem like you are trying to stretch array use a bit.
You should go OOP on this one.
Create a FoodFamilly object and a Food object in which you can store arrays if necessary and you'll be able to write a nice user-friendly code and add indices if needed.
OOP is almost always the answer :)