Jump to content

Recommended Posts

 

This is a Facebook var that give the basic information of the user. like userID the picture names and crap.

I'm wanting to take the array and brake it apart so i can use xml with it.

 

$test_array = array ($user_profile);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($test_array, array ($xml, 'addChild'));
print $xml->asXML(); 

 

the $user_profile var gives all the information.

 

but when it echos out of the php it echos backwards.

I need to flip it somehow any ideas.

 

This is what i mean when I say backwards.

 

<?xml version="1.0"?> 
<root>
<100000215611178>id</100000215611178>
<Rob Puckett>name</Rob Puckett>
<Rob>first_name</Rob>
<Puckett>last_name</Puckett>
<//www.facebook.com/profile.php?id=100000215611178>link<///www.facebook.com/profile.php?id=100000215611178>
<115250758498809>id</115250758498809>
<WCHS>name</WCHS>
<139539632730771>id</139539632730771>
<1972>name</1972>
<High School>type</High School>
<male>gender</male>
<-4>timezone</-4>
<en_US>locale</en_US>
<1>verified</1>
<02:34+0000>updated_time</02:34+0000>
</root> 

 

you see how it is backwards? how can I flip it so its like this.

 

<?xml version="1.0"?>

<root>
<id>100000215611178</id>
<name>Rob Puckett</name>
<first_name>Rob</first_name>
<last_name>Puckett</last_name></root> 

Link to comment
https://forums.phpfreaks.com/topic/243138-php-array-var-how-can-i-brake-it-apart/
Share on other sites

<pre> Array
(
    [id] => 100000215611178
    [name] => Rob Puckett
    [first_name] => Rob
    [last_name] => Puckett
    [link] => http://www.facebook.com/profile.php?id=100000215611178
    [education] => Array
        (
            [0] => Array
                (
                    [school] => Array
                        (
                            [id] => 115250758498809
                            [name] => WCHS
                        )

                    [year] => Array
                        (
                            [id] => 139539632730771
                            [name] => 1972
                        )

                    [type] => High School
                )

        )

    [gender] => male
    [timezone] => -4
    [locale] => en_US
    [verified] => 1
    [updated_time] => 2011-07-28T11:02:34+0000
)
</pre>

This isn't RegEx, (I realize now it was a silly mod move) but anyhoo

 

<?php 

$profile = array(
'id' => '100000215611178',
'name' => 'Rob Puckett',
'first_name' => 'Rob',
'last_name' => 'Puckett',
'link' => 'http://www.facebook.com/profile.php?id=100000215611178',
'education' => array(
	'school' => array(
		'id' => '115250758498809',
		'name' => 'WCHS',
	),
	'year' => array(
		'id' => '139539632730771',
		'name' => '1972'
	),
	'type' => 'High School'
),
'gender' => 'male',
'timezone' => '-4',
'locale' => 'en_US',
'verified' => '1',
'updated_time' => '2011-07-28T11:02:34+0000'
);

$xml = new SimpleXMLElement('<root/>');

arrayToXML( $xml, $profile );

echo $xml->asXML();

function arrayToXML( SimpleXMLElement &$xml, $array ) {
foreach( $array as $name => $value ) {
	if( !is_array($value) )
		$xml->addChild( $name, $value );
	else {
		$child = $xml->addChild( $name );
		arrayToXML( $child, $value );
	}
}
}

?>

 

Outputs

*****Modified to be more pretty

 

<?xml version="1.0"?> 
<root>
<id>100000215611178</id>
<name>Rob Puckett</name>
<first_name>Rob</first_name>
<last_name>Puckett</last_name>
<link>http://www.facebook.com/profile.php?id=100000215611178</link>
<education>
	<school>
		<id>115250758498809</id>
		<name>WCHS</name>
	</school>
	<year>
		<id>139539632730771</id>
		<name>1972</name>
	</year>
	<type>High School</type>
</education>
<gender>male</gender>
<timezone>-4</timezone>
<locale>en_US</locale>
<verified>1</verified>
<updated_time>2011-07-28T11:02:34+0000</updated_time>
</root> 

Ya I cant get it working like this. I only have this one var to work with

 

$user_profile

and if i <pre><?php print_r $user_profile ?> </pre>

I can see the array layout.

 

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}





$profile = array ($user_profile); <----------------------Get a error here!
$xml = new SimpleXMLElement('<root/>');



arrayToXML( $xml, $profile );

echo $xml->asXML();

function arrayToXML( SimpleXMLElement &$xml, $array ) {
foreach( $array as $name => $value ) {
	if( !is_array($value) )
		$xml->addChild( $name, $value );
	else {
		$child = $xml->addChild( $name );
		arrayToXML( $child, $value );
	}
}
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.