Jump to content

Can Someone Help Me Learn Arrays


Mancent

Recommended Posts

Ok here is the code im working with. as it it right now it is displaying xml

 

but I want to just echo tags in the array. but also it can keep making new. I do not know how to explain this. because I do not understand!

 

try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/'.$user_id.'/friends?access_token='. $access_token .'&fields=id,name');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
$profile = array ($user_profile);
echo $name[] = $user_profile['name'];
$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->$name = $value;
}
else {
$child = $xml->addChild( $name );

arrayToXML( $child, $value );
}
}
}

 

 

Now this works as xml..

 

and it displays a list of my friends.

 

<root><0><data><0><id>520510214</id><name>Angela L Taylor</name><first_name>Angela</first_name><last_name>Taylor</last_name><gender>female</gender><link>http://www.facebook.com/angela.martin.taylor</link><picture><data><url>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc6/186371_520510214_2129561780_q.jpg</url><is_silhouette></is_silhouette></data></picture></0><1><id>1627082340</id><name>April Mosier</name><first_name>April</first_name><last_name>Mosier</last_name><gender>female</gender><link>http://www.facebook.com/amosier2372</link><picture><data><url>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/195320_1627082340_1747498710_q.jpg</url><is_silhouette></is_silhouette></data></picture></1><2><id>100000117823779</id><name>Barb Pitman</name><first_name>Barb</first_name><last_name>Pitman</last_name><gender>female</gender><link>http://www.facebook.com/barb.pitman.7</link><picture><data><url>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/203336_100000117823779_2777051_q.jpg</url><is_silhouette></is_silhouette></data></picture></2><3><id>100000215611178</id><name>Rob Puckett</name><first_name>Rob</first_name><last_name>Puckett</last_name><gender>male</gender><link>http://www.facebook.com/robpuk56</link><picture><data><url>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc6/623477_100000215611178_1780460535_q.jpg</url><is_silhouette></is_silhouette></data></picture></3><4><id>100000545723958</id><name>Dodi Thornburg</name><first_name>Dodi</first_name><last_name>Thornburg</last_name><gender>female</gender><link>http://www.facebook.com/dodi.thornburg</link><picture><data><url>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/372752_100000545723958_586143298_q.jpg</url><is_silhouette></is_silhouette></data></picture></4><5><id>100000654467543</id><name>Corie Bradburn</name><first_name>Corie</first_name><last_name>Bradburn</last_name><gender>female</gender><link>http://www.facebook.com/corie.bradburn</link><picture><data><url>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/371610_100000654467543_700847477_q.jpg</url><is_silhouette></is_silhouette></data></picture></5><6><id>100000811812567</id><name>Casey Taylor</name><first_name>Casey</first_name><last_name>Taylor</last_name><gender>male</gender><link>http://www.facebook.com/casey.taylor.90834</link><picture><data><url>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc6/260700_100000811812567_713679838_q.jpg</url><is_silhouette></is_silhouette></data></picture></6><7><id>100000838601612</id><name>Arju Dares</name><first_name>Arju</first_name><last_name>Dares</last_name><gender>male</gender><link>http://www.facebook.com/arju.dares</link><picture><data><url>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/211555_100000838601612_2027547717_q.jpg</url><is_silhouette></is_silhouette></data></picture></7><8><id>100000898203770</id><name>Kary Kiser</name><first_name>Kary</first_name><last_name>Kiser</last_name><gender>female</gender><link>http://www.facebook.com/kary.kiser</link><picture><data><url>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/195333_100000898203770_839410129_q.jpg</url><is_silhouette></is_silhouette></data></picture></8><9><id>100001335667661</id><name>Joshua Bigboy Springman</name><first_name>Joshua</first_name><last_name>Springman</last_name><gender>male</gender><link>http://www.facebook.com/joshua.springman</link><picture><data><url>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/371691_100001335667661_437761407_q.jpg</url><is_silhouette></is_silhouette></data></picture></9><10><id>100001946559796</id><name>Chris Teer</name><first_name>Chris</first_name><last_name>Teer</last_name><gender>male</gender><link>http://www.facebook.com/cteer2</link><picture><data><url>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc6/203180_100001946559796_176613_q.jpg</url><is_silhouette></is_silhouette></data></picture></10><11><id>100004489140875</id><name>Gabriella Ray</name><first_name>Gabriella</first_name><last_name>Ray</last_name><gender>female</gender><link>http://www.facebook.com/gabriella.ray.75</link><picture><data><url>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/369519_100004489140875_774270663_q.jpg</url><is_silhouette></is_silhouette></data></picture></11></data><paging><next>https://graph.facebook.com/100001957015772/friends?fields=id,name,first_name,last_name,picture,gender,link&limit=21&offset=22&__after_id=100004489140875</next><previous>https://graph.facebook.com/100001957015772/friends?fields=id,name,first_name,last_name,picture,gender,link&limit=1&offset=0&__before_id=520510214</previous></paging></0></root>

 

 

 

But what I want to do. is I want to just echo each and every one of the arrays [0] - how ever many there are [5000] and then I want to echo the name and the ID for each array But I do not understand how to do this can any one help me?

 

 

and my end resault would be this.

 

<div class="content">
         <div><a href="/profile/id1"><img src="USERIMAGE1" title="USERNAME1" alt="USERNAME1" class="thumb" /></a></div>
       </div>
<div class="content">
         <div><a href="/profile/id2"><img src="USERIMAGE2" title="USERNAME2" alt="USERNAME2" class="thumb" /></a></div>
       </div>
<div class="content">
         <div><a href="/profile/id3"><img src="USERIMAGE3" title="USERNAME3" alt="USERNAME3" class="thumb" /></a></div>
       </div>
<div class="content">
         <div><a href="/profile/id4"><img src="USERIMAGE4" title="USERNAME4" alt="USERNAME4" class="thumb" /></a></div>
       </div>

Link to comment
https://forums.phpfreaks.com/topic/272300-can-someone-help-me-learn-arrays/
Share on other sites

Im not understanding.

 

try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/'.$user_id.'/friends?access_token='. $access_token .'&fields=id,name');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
$profile = array ($user_profile);


foreach( $profile as $name => $value)
{
if( !is_array($value) )
{


echo $profile->$name = $value;
}
else
{
echo $child =  $profile->addChild( $name );

}
}

I just can not seem to get it.. it will not echo any value i just do not see what i am doing wrong,

 

try {
   // Proceed knowing you have a logged in user who's authenticated.
   $user_profiles = $facebook->api('/'.$user_id.'/friends?access_token='. $access_token .'&fields=id,name');
 } catch (FacebookApiException $e) {
   error_log($e);
   $user = null;
 }
 $profile = array ($user_profiles);



foreach( $profile as $name => $value)
{
?>
<div class="content">
         <div><a href="/profile?id=<? echo $value['id'] ?>"><img src="http://graph.facebook.com/<? $value['id'] ?>/picture?type=large" title="<? echo $value['name'] ?>" alt="<? echo $value['name'] ?>" class="thumb" /></a></div>
       </div>
<?




}

Thank you Admin That was hard but it worked!

 

<!-- THIS IS THE FOOTER START-->
<div id="thumbnails_wrapper">
 <div id="outer_container">
   <div class="thumbScroller">
     <div class="container">

     <?
  try {
   // Proceed knowing you have a logged in user who's authenticated.
   $user_profiles = $facebook->api('/'.$user_id.'/friends?access_token='. $access_token .'&fields=id,name');
 } catch (FacebookApiException $e) {
   error_log($e);
   $user = null;
 }


$friendsList = array();





foreach( $user_profiles as $key=>$value)
{
 foreach ($value as $fkey=>$fvalue) {
  $friendsList[] = $fvalue['id'];
  $friendsName[] = $fvalue['name'];
?>
<div class="content">
         <div><a href="/profile?id=<? echo $fvalue['id'] ?>"><img src="http://graph.facebook.com/<? echo $fvalue['id'] ?>/picture?type=large" title="<? echo $fvalue['name'] ?>" alt="<? echo $fvalue['name'] ?>" class="thumb" /></a></div>
       </div>
<?
 }



}


  ?>


     </div>
   </div>
 </div>
</div>
</div>



<!-- THIS IS THE FOOTER END-->

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.