0

I m new in PHP.When I try to store variable in array i got this error ** Array to string conversion ** PHP Code is :

$record = simplexml_load_file('demo.xml');
foreach ($record as $item):       
    $a=$item->item2->record->p21;   
    $b=$item->item2->record->bq_21; 
    echo $arr1 = array($a,$b); 
endforeach;

I wants value only ....If print_r is used then its gives this o/p

Array ( [0] => SimpleXMLElement Object ( [0] => 26 ) [1] => SimpleXMLElement Object ( [0] => 1 ) )

I want this only

26 1
1
  • It seems you need $arr1 for later use, so just remove echo statement (although $arr1 is overwritten each time foreach is executed), or if you want to just print result, then echo $a.' '.$b like this. Commented Dec 5, 2013 at 10:58

3 Answers 3

1

Use print_r() instead of echo

print_r(array($a,$b));

You have to cast simpleXML Object to a string. (string)$a and (string)$b

Sign up to request clarification or add additional context in comments.

2 Comments

If I use this then its gives o/p in this format.Array ( [0] => SimpleXMLElement Object ( [0] => 26 ) [1] => SimpleXMLElement Object ( [0] => 1 ) )..I just want value
You have to cast simpleXML Object to a string. (string)$a
0
$record = simplexml_load_file('demo.xml');
foreach ($record as $item):       
    $a=$item->item2->record->p21;   
    $b=$item->item2->record->bq_21; 
    $arr1 = array($a,$b);
    print_r($arr1);
endforeach;

Comments

0

var_dump (http://sg3.php.net/var_dump) should be used for debugging data in an object. Also print_r (https://www.php.net/print_r) can be used that helps printing arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.