hey guys,
i think i lost my mind.
print_r($location); lists some geodata.
array (
'geoplugin_city' => 'My City',
'geoplugin_region' => 'My Region',
'geoplugin_areaCode' => '0',
'geoplugin_dmaCode' => '0',
'geoplugin_countryCode' => 'XY',
'geopl ...
when I iterate through it with a foreach loop I can print each line.
However shouldn't it be possible to just get a specific value out of the array?
like print $location[g4]; should print the countryCode shouldn't it? Thank you!
echo $location['geoplugin_countryCode'];
Yes, you can get a specific value by key. The keys in your case are the geoplugin_ strings.
To get the country code:
// XY
$location['geoplugin_countryCode'];
$location['geoplugin_countryCode'];
would access country code
Where does "g4" come from? Did you mean "4"?
If you had a normal numerically-indexed array then, yes, you could write $location[4]. However, you have an associative array, so write $location['geoplugin_countryCode'].
there you are using an associative array, it is an array with a user defined key:value pair (similar to dictionaries on Python and Hash Tables on C#)
You can access the elements just using the Key (in this case geoplugin_city or geoplugin_region)
Using the standard array syntax:
$arrayValue = $array[key]; //read
$array[key] = $newArrayValue; //write
For example:
$location['geoplugin_city']; or $location['geoplugin_region'];
If you are not familiarwith PHP arrays you can take a look here:
http://php.net/manual/en/language.types.array.php
For a better understanding on array manipulation with PHP take a look of:
http://www.php.net/manual/en/ref.array.php
Related
For example, lets say I have an array that looks liked
$stuff = array('random_string_1' => 'random_value_1','random_string_2' => 'random_value_2');
and then I call the asort() function and it changes the array around. How do I get the new first string without knowing what it actually is?
If you want to get the first value of the array you can use reset
reset($stuff);
If you want to also get the key use key
key($stuff);
If you need to get the first value, do it like this:
$stuff = array('random_string_1' => 'random_value_1','random_string_2' => 'random_value_2');
$values = array_values($stuff); // this is a consequential array with values in the order of the original array
var_dump($values[0]); // get first value..
var_dump($values[1]); // get second value..
When I put print_r($data); I get the following
Array
(
[name] => Cheese
)
Is there a way to get the key name in a variable on its own?
There may be occasions that name could be email and other values.
Use array_keys():
var_dump(array_keys($data));
Return all the keys or a subset of the keys of an array
Do you mean you know the value but you don't know the key? If so you could write something like this:
$array = ['name' => 'Cheese'];
array_flip($array);
var_export($array['Cheese']); // Output: name
You can have the array key extracted to their own variables using the extract function. For example
$a = array("color"=>"blue");
extract($a);
echo $color;
I am trying to take an array of filenames and output the following...
a:1:{s:4:"docs";a:4:{i:0;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image1.jpg";}i:1;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image2.jpg";}i:2;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image3.jpg";}i:3;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image4.jpg";}}}
This is what I have so far...
<?php
$serialized_data = serialize(array('http://www.example.com/image1.jpg', 'http://www.example.com/image2.jpg', 'http://www.example.com/image3.jpg', 'http://www.example.com/image4.jpg'));
echo $serialized_data . '<br>';
?>
But this is giving me...
a:4:{i:0;s:34:"http://www.example.com/image1.jpg";i:1;s:34:"http://www.example.com/image2.jpg";i:2;s:34:"http://www.example.com/image3.jpg";i:3;s:34:"http://www.example.com/image4.jpg";}
Where am I going wrong?
There is nothing wrong with the serialized array. You're just not creating the array like you want it to be. PHP can't guess how you really want your array to be, so you have to tell PHP how you want it to be. So what you need to do is to change the input array correctly.
You're giving
array('http://www.example.com/image1.jpg', 'http://www.example.com/image2.jpg', 'http://www.example.com/image3.jpg', 'http://www.example.com/image4.jpg')
and that's completely different from the serialized array how it should be. Your Array needs to look like this
array('docs' => array(array('property_imgurl' => 'http://www.example.com/image1.jpg'), array('property_imgurl' => 'http://www.example.com/image2.jpg'), array('property_imgurl' => 'http://www.example.com/image3.jpg'), array('property_imgurl' => 'http://www.example.com/image4.jpg')))
Look at this eval
You're just missing the array key definitions.
$serialized = array(array('docs' => array(array('property_imgurl' => 'http://www.example.com/image4.jpg'))));
As you can see, each URL has a key of property_imgurl and each of those array is part of a parent array with a key of docs
Here's the eval.in
Hi I recently came across an example of json_encode function. Very confused about 1 part:
<?php
$runners=array{
'fname'=>5
'you' => 6
};
echo json_encode (array("runners"=>$runners));
?>
Question is, why can't the code on the last row simply be:
echo json_encode ($runners);
Thanks,
First of all, your array declaration is incorrect and you will get a syntax error if you run the code. You should use array(...) not array{...}. And the values need to be comma-separated. For example:
array(
key => value,
key2 => value2,
key3 => value3,
...
)
The following should work:
$runners = array(
'fname' => 5,
'you' => 6
);
echo json_encode($runners);
Output:
{"fname":5,"you":6}
How are these two different
The end result is different for both cases. When you do json_encode(array("runners"=>$runners));, the array is multi-dimensional, and the JSON output will change, too:
{"runners":{"fname":5,"you":6}}
Which one should you use
Depends. In the first array, you are simply creating two keys named fname and you, and in the second, you also add another key, runners, thereby making the array multi-dimensional. If you want that information to be present in the resulting JSON string, you should use the second one. If not, use the first one.
First you have use { in array it is not correct. and you have not have , between array elements.
you can use both. but you have to access 2 json by different ways. depending in your choice.
for first (it should be best choice) echo json_encode ($runners); you have one dimensional array.
$runners=array(
'fname'=>5,
'you' => 6
);
echo json_encode ($runners);
OUTPUT:
{"fname":5,"you":6}
In second you have 2d array.
$runners=array(
'fname'=>5,
'you' => 6
);
echo json_encode (array("runners"=>$runners));
OUTPUT:
{"runners":{"fname":5,"you":6}}
Live Demo : https://eval.in/92104
Firstly your array is wrogly used. It will use small brackets () not curly {}. So your array will become :
$runners=array(
'fname' => 5,
'you' => 6
);
Now when you do json_encode() as: echo json_encode ($runners); you will get the output:
{"fname":5,"you":6}
And if you do : echo json_encode (array("runners"=>$runners)); you will get output:
{"runners":{"fname":5,"you":6}}
please tell me what is below code doing? is it creating array with two values or its creating to string index which will take value later?
var $requiredValues=Array(
'MaxResponses',
'AvailableOnlyIndicator'
);
$requiredValues=Array(
'MaxResponses',
'AvailableOnlyIndicator'
);
It is assigning index 0 to value MaxResponses and 1 to value AvailableOnlyIndicator.
So array with two values.
Note: var keyword won't work in you are under php 5+.
That code isn't valid php. This is:
$requiredValues=Array(
'MaxResponses',
'AvailableOnlyIndicator'
);
That code is creating an array with two elements.
The code should be:
<?php array('MaxResponses', 'AvailableOnlyIndicator'); ?>
And the array would be:
Array
(
[0] => MaxResponses
[1] => AvailableOnlyIndicator
)
It's creating an array with two values, with an automatic numeric index. Syntax is slightly off, but it should work.