I had an XML that I turned into an array to sort it, now I want to save it as an XML again. This would not be a problem if I didn't have the following: [caseid] => Array ( [#text] => 885470 ...
I need: <caseid> 885470 </caseid>
Writing the DOM has been fine for the fieldname "caseid", but the fieldvalue is an array titled "#text" that I cannot figure out how to parse.
I can't search google for "#" symbols, so searching has been pretty tough.
I was able to access the number in the array by referencing it as a string. Stupid way to do it, but it works; the best solution would be to edit the array to xml conversion, but accessing array elements via strings worked too.
I was previously accessed array elements like so:
print_r($array[caseid][#text]); //<--Does not work with #
print_r($array[caseid]['#text']); //works
Again, not the prettiest, but a viable workaround.
Related
I am trying to read string in session array.
Here is the code I entered $fruit_type into my session:($number is 2622232 here)
$_SESSION['fruit'][$number]=$fruit_type;
When I used print_r($_SESSION['fruit']), I get the following array:
Array ( [2622232] => [] => apple )
My question is how can I get the string "apple"? My editor give me error message when I tried to use $_SESSION['fruit'][$number][] to read string.
Any idea about my situation?
The only way to get that print_r() output is with an empty string as key [''], so you need to find where you do that and fix it. However you can access it:
echo $_SESSION['fruit'][2622232][''];
$_SESSION['fruit'][$number][] is wrong as you don't pass an index. Something like $_SESSION['fruit'][$number][0] should work.
You can find more information about arrays in the PHP documentation (here the example that solves your problem)
I have a select multiple tag with a name city[]. The [] brackets are supposed to signify an array in the url query string for the PHP later.
I'm using jQuery .serialize() to get the form value to build a query string for an AJAX call. However, it looks like .serialize() is encoding the URL and not writing the brackets
I should get
index.php?city[]=METROPLOIS&city[]=GOTHAM
Instead I'm getting
index.php?city%5B%5D=METROPLOIS&city%5B%5D=GOTHAM
Is there a way to make it stop encoding for just the name? There may be some instances where the city name has a space, so I'll still need it to encode that.
I think you are just a bit confused since you may be unfamiliar with URL escaping.
But, this works perfectly in PHP. For instance, print_r($_GET) will output:
Array
(
[city] => Array
(
[0] => METROPLOIS
[1] => GOTHAM
)
)
Being city interpreted correctly as an array and containing the expected values. Since, city%5B%5D is a valid URL encoded string for city[].
Ok so I am stuck with this xml in PHP stuff. I have gotten pretty far considering its my first 3 hours into XML all together ever in my entire life.
I am having trouble pulling data from a XML thing that has # in the name. See below (obviously im not going to post the whole XML thing but u can see how i got there below that.
SimpleXMLElement Object
(
[#attributes] => Array
(
[date] => 2010-09
[reserved] => 6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
)
)
How i got there:
echo $this->General_functions->naked($xml->property[0]->availability->month[0]);
General_functions->naked is just a fast function to wrap and print_r around the given attribute.
My question is, HOW do i get the values inside #attributes cause no matter what i try i cant figure it out. Ive searched the web for a good 45 mins with no real answer.
Thanks in advance.
David
You need to use the attributes() method to get the results as another class. So, for example, to get the date attribute:
$myElement->attributes()->date
Also note that it's not a string, it's a SimpleXML attribute. If you want to get its actual value, you need to cast it to string explicitly:
(string)$myElement->attributes()->date
Access attributes of an element just as you would elements of an array:
(string) $xml->property[0]->availability->month[0]['date']
Edited to add the cast.
Given this in the Smarty template:
<pre>{$user->settings['sendStats']|#print_r:1}</pre>
The output in the browser is this:
Array
(
['period'] => daily
['ofPeriod'] => year
['points'] => 1000
)
Doing any of these:
<pre>{$user->settings['sendStats']['period']|#print_r:1}</pre>
<pre>{$user->settings['sendStats'][ofPeriod]|#print_r:1}</pre>
<pre>{$user->settings['sendStats'].points|#print_r:1}</pre>
<pre>{$user->settings.{'sendStats'}.{'period'}|#print_r:1}</pre>
<pre>{$user->settings.{sendStats}.{period}|#print_r:1}</pre>
with or without the |#print_r:1 gives no output in the browser.
I also tried assigning $user->settings to a Smarty variable and I get the exact same result (as expected).
How do I access the elements of the $user->settings['sendStats'] array?
{$user->settings.sendStats.period|#print_r:1} should work just fine. Also have a look at the Variables page in the docs…
The array values itself are not arrays (your array is not multidimensional), so you should drop the |#print_r:1 and you should be fine. Should look something like:
<pre>{$user->settings['sendStats']['period']}</pre>
Finally figured it out. The array keys contained single quotes, so the answer would have been:
{$user->settings['sendStats']["'period'"]}
I fixed it so the keys didn't contain quotes anymore.
I'm trying to call to a specific part of an array with the key # and it's not working. I can output the array and see it...
Array
(
[6] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
[7] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
[8] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
)
This array is $emailDB. I can call to the array manually with $emailDB[7] and it works, but if my call is dynamic like this it won't work...
<?php
$value = 7;
print_r($emailDB[$value]);
?>
I've never had an issue like this with an array so it's very odd. What really sucks is I'm under deadline with a form not working on a client's site...joy.
We tried this with no luck...
<?php
$value = 7;
print_r($emailDB[intval($value)]);
?>
I thought intval() would assist but it did not.
You're post implies a bug in php itself, which I highly doubt. What's more likely is that what you posted doesn't properly represent the code you're running.
Why don't try this. Make a brand new empty php file. Hardcode the array keys and values and assign them to the $emailDB variable, and then try
$value = 7;
print_r($emailDB[$value]);
You will see you don't have the problem that you claim. You have now started the debugging process, and now you can look at the working, and non working code to compare the difference.
Well, you are echoing an Array, which I assume is printing "Array" onto your screen. If you want to echo the actual contents of the array, you need to use print_r($array) or echo print_r($array, true). You can also try putting the value in quotes, like $emailDB["{$value}"] to see if that works, I sometimes have troubles with integers not going into things properly.
I agree with you all. It had to have been something whacky with how we were pulling in the data somehow. It was a tab-separated file we were exploding. I just re-wrote the whole thing entirely and imported the data into MySQL and all was well.
In hindsight, I have a sneaking suspicion it was a trim() command that was needed and likely nothing more. Dang it...too late, but I learned something about checking over the code for those types of things.