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.
Related
I would like to use the first value of a dynamically created array but I get the first letter of the array name instead.
$log = "dets_".$id;
$$log = array();
while ($c = mysql_fetch_assoc($cuenta)) { array_push($$log,$c['id'].'::'.$c['fecha']); }
When I print $$log I get something like this:
Array ( [0] => 124::2017-04-07 [1] => 119::2017-04-07 [2] => 118::2017-04-05 )
But when I try to access the first key:
echo $$log[0];
I get "$d" and not "124::2017-04-07". I also tried $log[0] and get "d".
Thank you.
You could access the first element by somewhat tricky expression:
var_dump (${${'log'}}[0]);
http://php.net/manual/en/language.variables.variable.php
A bit of explanation Taken from PHP Manual
In order to use variable variables with arrays, you have to resolve an
ambiguity problem. That is, if you write $$a[1] then the parser needs
to know if you meant to use $a[1] as a variable, or if you wanted $$a
as the variable and then the [1] index from that variable. The syntax
for resolving this ambiguity is: ${$a[1]} for the first case and
${$a}[1] for the second.
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
$a=Array ([storage] => [submitted] => 1 [values] => Array ( [q] => googl [op]))
How can I get the value of q from this array $a->q doesn't give me the value. Why?
You use the -> on objects. For an array you need to index the variable like this:
echo $a['values']['q'];
echo $a['values']['q'];
This is 2-d array you can get value like above.
you can also use the foreach to get retrieve values of arrays.
You should use $a['values']['q'].
to get it do this:
echo $a['values']['q'];
You use the -> on objects. For an array you need to index the variable like this:
echo $a['values']['q'];
Check this foreach tutorial for u can get more information
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
I have the following array (in php after executing print_r on the array object):
Array (
[#weight] => 0
[#value] => Some value.
)
Assuming the array object is $arr, how do I print out "value". The following does NOT work:
print $arr->value;
print $val ['value'] ;
print $val [value] ;
So... how do you do it? Any insight into WHY would be greatly appreciated! Thanks!
echo $arr['#value'];
The print_r() appears to be telling you that the array key is the string #value.
After quickly checking the docs, it looks like my comment was correct.
Try this code:
print $arr['#value'];
The reason is that the key to the array is not value, but #value.
You said your array contains this :
Array (
[#weight] => 0
[#value] => Some value.
)
So, what about using the keys given in print_r's output, like this :
echo $arr['#value'];
What print_r gives is the couples of keys/values your array contains ; and to access a value in an array, you use $your_array['the_key']
You might want to take a look at the PHP manual ; here's the page about arrays.
Going through the chapters about the basics of PHP might help you in the future :-)