Merovingian Posted October 5, 2009 Share Posted October 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176509-how-to-add-extra-index-and-data-to-this-php-array/ Share on other sites More sharing options...
MadTechie Posted October 5, 2009 Share Posted October 5, 2009 can you post an example if your new array, as i'm not sure what you mean *just do a var_dump($contacts); Quote Link to comment https://forums.phpfreaks.com/topic/176509-how-to-add-extra-index-and-data-to-this-php-array/#findComment-930445 Share on other sites More sharing options...
Merovingian Posted October 5, 2009 Author Share Posted October 5, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/176509-how-to-add-extra-index-and-data-to-this-php-array/#findComment-930456 Share on other sites More sharing options...
jon23d Posted October 5, 2009 Share Posted October 5, 2009 I would love to help you, really, but I can't read your code... Use indentation. Code blocks should be obvious. Logical errors creep in really easily with code that looks like that. I tried to tab it for you, but I have no idea... Quote Link to comment https://forums.phpfreaks.com/topic/176509-how-to-add-extra-index-and-data-to-this-php-array/#findComment-930469 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.