I have XML file I want to retrieve the attribute names from it by using this code below. the code work and retrieve all attribute names but under the last attribute names also show this error
Fatal error: Call to a member function attributes() on a non-object in C:\wamp\www\php\xml\EnglishArabic\new.php on line 13
<?php
$en = simplexml_load_file('ENstrings.xml');
$enlen = sizeof($en->string);
for ($i=0; $i<=$enlen ; $i++) {
foreach ($en->string[$i]->attributes() as $key => $value) {
echo $value. '<br \>';
}
}
?>
That's because the index ($en->string[$i]) starts from 0 to length-1, so your loop supposed to stop just before $i reaches the length :
for ($i=0; $i<$enlen ; $i++) {
Related
having a bit of difficulty with a multi-dimensional array. I have shortened it, but the array looks like this
array(192) {
["count"]=> int(191)
[0]=>array(124) {
[11]=>string(10) "usnchanged"
["homemta"]=>array(2) {
["count"]=>int(1)
[0]=>string(206) "Some String"
}
[12]=>string(7) "homemta"
["proxyaddresses"]=>array(2) {
["count"]=>int(1)
[0]=>string(46) "SMTP:remove=email#email.com"
}
}
}
}
I am trying to get the email addresses which will be listed under proxyaddresses. What I am doing at the moment is the following:
for($i=0; $i<$data["count"]; $i++) {
foreach($data[$i]["proxyaddresses"] as $object) {
print_r($object);
}
}
This gets me the data I need, but inbetween all the data I get a lot of warnings like
Notice: Undefined index: proxyaddresses in index.php on line 88
Warning: Invalid argument supplied for foreach() in index.php on line
88
So I presume it is not liking something. How would I properly do the loop based on the above array structure?
Thanks
It's because proxyaddresses element is not present for each loop. You have to check if it's set or not to avoid warning by using php isset() function.
for($i=0; $i<$data["count"]; $i++) {
if(isset($data[$i]["proxyaddresses"])){
foreach($data[$i]["proxyaddresses"] as $object) {
print_r($object);
}
}
}
for($i=0; $i<$data["count"]; $i++) {
if(!isset($data[$i]["proxyaddresses"])) continue;
foreach($data[$i]["proxyaddresses"] as $object) {
print_r($object);
}
}
I have this piece of code which works well if the ['extensions'] array exists.. but if the array does not exist then it returns errors. How can I fix this code to not return anything if the extensions array does not exist?
-[UPDATE-
Sorry i had previously inserted the wrong code.. here is the proper code i need checked.
$oid = array('id-ce-subjectAltName');
$count = count($cert['tbsCertificate']);
for($i = 0; $i < $count; $i++) {
if(array_key_exists('extensions', $cert['tbsCertificate']) &&
in_array($cert['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
$value = $cert['tbsCertificate']['extensions'][$i]['extnId'];
echo "\n",'<b>[SANs]</b>',"\n","\n";
}
}
I get this warning when ['extensions'] does not exist - I would like to prevent any warnings from being generated.
Notice: Undefined index: extensions in
C:\xampp\htdocs\labs\certdecode\certdecode.php on line 142
AFTER UPDATE:
How is the structure of the array?
You count the number of items in $cert['tbsCertificate'],
but in your loop, your $i is for the number of items in $cert['tbsCertificate']['extensions'].
So maybe you try to do something like this?:
$oid = array('id-ce-subjectAltName');
if (array_key_exists('extensions', $cert['tbsCertificate']) &&
is_array($cert['tbsCertificate']['extensions'])
) {
$count = count($cert['tbsCertificate']['extensions']);
for ($i = 0; $i < $count; $i++) {
if (in_array($cert['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
$value = $cert['tbsCertificate']['extensions'][$i]['extnId'];
echo "\n", '<b>[SANs]</b>', "\n", "\n";
}
}
}
okay, seems to work by adding an isset() at the start of the code:
if(isset($cert['tbsCertificate']['extensions'])) {
thanks guys!
I have some problem with this code. Warning: Illegal offset type in Line 22
$this->word[$kata][]=array($i,$j);
and the full code is below
private $jmldoc = 0; private $word = array();
public function getIndex($D) {
$this->jmldoc = count($D);
for($i=0; $i<$this->jmldoc; $i++) {
$pp = new prePro($D[$i]);
$kata = $pp->tokenize();
$n = count($kata);
for($j=0; $j<$n; $j++) {
$this->word[$kata]=array($i,$j);
}
}
}
Can you help me to fix it?
You are passing an array, not a string/integer index to your $this->word.
//I suppose from the context of your code that $kata is an array also
//so if that's true, it can't be used as an index
$this->word[$kata][]=array($i,$j);
Keep in mind that $this->word is an array. So probably there is something wrong with your program logic. To fix this, use an integer or string to access the elements of an array.
I'm trying to learn about static variables inside a function. So I created this:
<?php
// Create a function that has a counter
function counter_inside_function()
{
static $counter = 0;
++$counter;
return $counter;
}
// counter_inside_function() in a variable
$counter_function = counter_inside_function();
// Create a loop and place the function inside it
$count = 1;
while ($count < 11) {
echo $counter_function, '<br>';
// echo counter_inside_function(), '<br>';
$count++;
}
I was expecting to increment the counter but it didn't. However, if I uncomment line 21 and echo the function directly (not the $counter_function variable, that's when it incremented. What I don't get is it started counting from 2 instead of 1. But when I removed $counter_function = counter_inside_function(); I got the result I wanted.
You should move $counter_function = counter_inside_function(); inside your while loop so that the function is called on every iteration:
<?php
// Create a function that has a counter
function counter_inside_function()
{
static $counter = 0;
++$counter;
return $counter;
}
// Create a loop and place the function inside it
$count = 1;
while ($count < 11) {
$counter_function = counter_inside_function();
echo $counter_function, '<br>';
$count++;
}
When you call the function counter_inside_function() on line 13 and stored its return value into a variable what you've done is you've run that function one time and its returning 1. Now since the variable in counter_inside_function() is static its going to keep that value the next time you call it. That's why it seems as if its starting at 2 when really you've incremented it to 1 before the while loop and then during the loop it seems as though it is starting at 2.
Now the issue with the loop is that you echoing the variable $counter_function 20 times does not mean you're calling the function counter_inside_function() 20 times. All you're doing is taking the number that was stored in it the first call (which is 1) and echoing it out 20 times. So if you remove the comment on line 21 and remove the function call on line 13 (so that it doesn't increment to 1 before your loop begins) your program will give you the results you want.
This is how your code should look like:
// Create a function that has a counter
function counter_inside_function()
{
static $counter = 0;
++$counter;
return $counter;
}
// Create a loop and place the function inside it
$count = 1;
while ($count < 11) {
$counter_function = counter_inside_function();
echo $counter_function, '<br>';
$count++;
}
Im working on a new minimal Project, but i've got an error, i dont know why.
Normally, i use arrays after i first created them with $array = array();
but in this case i create it without this code, heres an example full code, which outputs the error:
<?php $i = array('demo', 'demo'); $array['demo/demodemo'] = $i; ?>
<?php $i = array('demo', 'demo'); $array['demo/demodemo2'] = $i; ?>
<?php
foreach($array as $a)
{
echo $a[0] . '<br>';
}
function echo_array_demo() {
foreach($array as $a)
{
echo $a[0] . '<br>';
}
}
echo_array_demo();
?>
I create items for an array $array and if i call it (foreach) without an function, it works. But if i call in in a function, then the error comes up...
I ve got no idea why
Thank you...
Functions have their own variable scope. Variables defined outside the function are not automatically known to it.
You can "import" variables into a function using the global keyword.
function echo_array_demo() {
global $array;
foreach($array as $a)
{
echo $a[0] . '<br>';
}
}
Another way of making the variable known to the function is passing it as a reference:
function echo_array_demo(&$array) {
foreach($array as $a)
{
echo $a[0] . '<br>';
}
}
echo_array_demo($array);
Check out the PHP manual on variable scope.