Jump to content

How to add extra index and data to this PHP array?


Merovingian

Recommended Posts

I want to have extra index/data field in following $contacts array.

Please see below code ($contacts is initialized here) :

public function getMyContacts()
{
if (!$this->login_ok)
{
$this->debugRequest();
$this->stopPlugin();
return false;
}
else $url=$this->login_ok;
$res=$this->get($url,true);
if ($this->checkResponse("get_friends",$res))
$this->updateDebugBuffer('get_friends',"{$url}",'GET');
else
{
$this->updateDebugBuffer('get_friends',"{$url}",'GET',false);
$this->debugRequest();
$this->stopPlugin();
return false;
}

$nextPage=true;$page=0;$contacts=array();
while($nextPage)
{
$nextPage=false;
if (preg_match_all("#\<td\>\<a href\=\"\/profile\.php(.+)\>(.+)\<\/a\>#U",$res,$names))
if (!empty($names[2]))	
if (preg_match_all("#\<small\>\<a href\=\"\/inbox\/(.+)\"\>#U",$res,$hrefs))
foreach($hrefs[1] as $key=>$href)
if (!empty($names[2][$key])) $contacts["/inbox".$href]=htmlspecialchars($names[2][$key]);

if (preg_match_all("#\<div class\=\"pager\"\>(.+)\<\/div\>#U",$res,$pagerBulk))
{
if (!empty($pagerBulk[1][1]))
{
$temps=explode(' ',$pagerBulk[1][1]);
$key=count($temps)-2;
if (!empty($temps[$key]))
{
if (preg_match("#\<a href\=\"(.+)\"\>#U",$temps[$key],$next)) $nextPage='http://m.facebook.com'.str_replace('&','&',$next[1]);
else return $contacts;	
}
else return $contacts;
}
else return $contacts;
}
else return $contacts;
if (!empty($nextPage)) $res=$this->get($nextPage,true);
$page++; 
}
return $contacts;
}

 

print_r($contacts) gives following output :

Array
(
[/inbox?rc89419ad&r25b4c932&compose&ids=645976873&refid=5] => Aditya Hajare 
[/inbox?r5aab8b97&r82f9007d&compose&ids=713878257&refid=5] => Kartik Iyer 
[/inbox?r9fb90a53&rddc4cc0d&compose&ids=714935023&refid=5] => Keyur Girish Godse 
[/inbox?r6425e4a9&re24d1756&compose&ids=100000273909058&refid=5] => Kiran Kamble 
)

 

In following code above array is processed :

foreach ($contacts as $email=>$name)
{
echo $email;
echo $name;
}

 

How do i add 3rd index or data to above $contacts array and access or fetch it in foreach() loop written above?

 

For. e.g.

I want to have $contacts as :

3 x 3 array... like 3rd data will b lets say some id or name etc..

so in foreach() loop i should be able to access that 3rd new index data as follows :

 

foreach($contacts as ----i dont know what to write here for 3 x 3 array-----)
{
echo $email;
echo $name;
echo $new_third_data;
}

 

It will be great if somebody tell me exact code about how to add extra index/data field to that array in future and process/access it.

I did var_dump($contacts) outside foreach() loop. And following is the output :

array(4)
{
["/inbox?ra0f11597&r54bf4afc&compose&ids=645976873&refid=5"]=> string(13) "Aditya Hajare" ["/inbox?r6a6a9b50&rb0cb2671&compose&ids=713878257&refid=5"]=> string(11) "Kartik Iyer" ["/inbox?ra8539e07&r1bee71cc&compose&ids=714935023&refid=5"]=> string(18) "Keyur Girish Godse" ["/inbox?rfd3f9377&rbcd37703&compose&ids=100000273909058&refid=5"]=> string(12) "Kiran Kamble" }

 

What i want to do is, currently there are 2 data fields i'm getting in $contacts....

i.e.

1. : ["/inbox?ra0f11597&r54bf4afc&compose&ids=645976873&refid=5"]

2. : Aditya Hajare

 

that means 1st one is inbox link and 2nd one is username... if i process above array as,

foreach($contacts as $inbox_link => $name)

the i can directly access inbox link as $inbox_link and name as $name.

 

Now i want to have 3rd filed in same array... like... i want user country in $contacts array...

so it should look like,

Array
{
[inbox link 1] => username1 => usercountry1
[inbox link 2] => username2 => usercountry2
[inbox link 3] => username3 => usercountry3
[inbox link 4] => username4 => usercountry4
}

 

So how do i modify $contacts array as above and access those 3 things (inbox link, username, user country) in foreach() loop?

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.