Xname Posted May 9, 2012 Share Posted May 9, 2012 Hi...Dear frnds.I hope u are all fine. I am using php.Here is a problem in php code. I want to get the foreach loop in the form of array. Example: suppose here is foreach loop. which execute for two times. $any_array = array(); foreach($something as $anything) { //when it is execute first time.return. A,B,C and D. //when it is executed for second time.return. E,F,G and H. $any_array[] = $anything; } print_r($any_array); // it print out only " E,F,G and H. " // but I want to get the both. " A,B,C and D... E,F,G and H. " How can I get it.Please tell me about this. Here is a simple example.it is not for execute for two time and are bigger then A,B,C and D... E,F,G and H. what can i do for get it. ......................THANKS..........… Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/ Share on other sites More sharing options...
cyberRobot Posted May 9, 2012 Share Posted May 9, 2012 Your code seems to work for me: <?php $something = array('A,B,C and D.', 'E,F,G and H.'); $any_array = array(); foreach($something as $anything) { $any_array[] = $anything; } print_r($any_array); ?> The output looks like: Array ( [0] => A,B,C and D. [1] => E,F,G and H. ) What does $something look like in your code? Also, is there something else that you're doing with the foreach loop? Right now the above code could be accomplished with a simple assignment operation: <?php $something = array('A,B,C and D.', 'E,F,G and H.'); $any_array = $something; print_r($any_array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344200 Share on other sites More sharing options...
Xname Posted May 9, 2012 Author Share Posted May 9, 2012 something is not equal to $something = array('A,B,C and D','E,F,G and H'); In the above code $something is any array.Which execute only foreach loop. Ok lets Define it more. $result_array = array(); $something = array('url1','url2'); foreach($something as $links) { //suppose here is code which get the links from url1 and url2 $url_links = get_url_links($links); //do not worried about this this is only suppose code // which get the links of the website // when it is executed first time it gets the links from "url1" // i.e www.google.com and www.facebook.com // when it is executed second time it get the links from "url2" // i.e www.wikipedia.com and www.phpfreaks.com. $result_array[] = $urls_links } print_r($result_array); // it can return only those links which can be taken from "url2" // i.e www.wikipedia.com and www.phpfreaks.com. But I want to get the all links i.e www.google.com and www.facebook.com...www.wikipedia.com and www.phpfreaks.com. Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344264 Share on other sites More sharing options...
cyberRobot Posted May 9, 2012 Share Posted May 9, 2012 Other than the following errors, the code looks fine: <?php //... $result_array[] = $urls_links //<-- should be $url_links and is missing a semi-colon //... ?> But I would imagine this isn't your real code since you're getting results. The problem seems to be somewhere else in the code. With a few modifications to your example, the following gives me results from both elements in the $something array: <?php $result_array = array(); $something = array('http://www.google.com/','https://vimeo.com/'); foreach($something as $links) { $url_links = parse_url($links); $result_array[] = $url_links['host']; } print_r($result_array); ?> Output: Array ( [0] => www.google.com [1] => vimeo.com ) Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344278 Share on other sites More sharing options...
Mahngiel Posted May 9, 2012 Share Posted May 9, 2012 Any reason you're looping? Why not just use array_merge() ? Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344279 Share on other sites More sharing options...
cyberRobot Posted May 9, 2012 Share Posted May 9, 2012 Any reason you're looping? Why not just use array_merge() ? As mentioned in Reply #2, Xname looks to be doing something more complicated than my example...and the one mentioned in the original post. Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344280 Share on other sites More sharing options...
Xname Posted May 10, 2012 Author Share Posted May 10, 2012 Hi, Frnds..I hope u r all fine. First of all telling u that i can not use the array_merg().Because i had some resaon due I can not use the array_merg(); Friend "cyberRobort" I had not this question which u answered.Please focus on the question. I said,$something is equal to url1 and url2.This is not thing where is my result. Please focus on this. <?php $something = array('google.com','php.net'); $result_array = array(); foreach($something as $links) { $url_links = get_links_from_site($links); // ignore this.This is only supposed code which get links. //here is the code which get the links from site // suppose this links is link1 and link2.ok // this result comes when it is execute first time // my mean when $links is equal to 'google.com'; //here is the code which get the links from site // suppose this links is link3 and link4.ok // this result comes when it is execute second time // my mean when $links is equal to php.net'; $result_array = $url_links; } print_r($result_array); ?> Its ut put is Link3 and link4. But i want to get the result Link1 , Link2 , Link3 and Link4. Please focus that my result is not 'goole.com' and 'php.net'. this is the links where I is my result. I define the problem more.ok <?php $something = array('google.com','php.net'); $result_array = array(); foreach($something as $links) { $url_links = get_links_from_site($links); // ignore this.This is only supposed code which get links. //here is the code which get the links from site // suppose this links is link1 and link2.ok // this result comes when it is execute first time // my mean when $links is equal to 'google.com'; //here is the code which get the links from site // suppose this links is link3 and link4.ok // this result comes when it is execute second time // my mean when $links is equal to php.net'; $result_array = $url_links; print_r($result_array); } ?> When u execute the $result_arrry in foreach loop then it gets the result link1 , link2 , link3 and link4. But when it is execute out side the foreach loop then it gets the result link3 and link4. This is the problem, I want to get the result link1 , link2 , link3 and link4. when it is result_array execute outside the foreach loop. I think u can now understand this problem. please help me ................thanks......... Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344386 Share on other sites More sharing options...
Drummin Posted May 10, 2012 Share Posted May 10, 2012 You just need to check if $result_array isset and if NOT, set it, otherwise you are changing it back to empty when the script is run again. <?php if (!isset($result_array)){ $result_array = array(); } //Add one link $something = array('Link1'); foreach($something as $links) { //I will assume this is a function you are using so left in place // $url_links = get_links_from_site($links); // ignore this.This is only supposed code which get links. $url_links = $links; $result_array[] = $url_links; } //add another link if (!isset($result_array)){ $result_array = array(); } $something = array('Link2'); foreach($something as $links) { //I will assume this is a function you are using so left in place // $url_links = get_links_from_site($links); // ignore this.This is only supposed code which get links. $url_links = $links; $result_array[] = $url_links; } //let's add two more if (!isset($result_array)){ $result_array = array(); } $something = array('Link3','Link4'); foreach($something as $links) { //I will assume this is a function you are using so left in place // $url_links = get_links_from_site($links); // ignore this.This is only supposed code which get links. $url_links = $links; $result_array[] = $url_links; } //and two more again if (!isset($result_array)){ $result_array = array(); } $something = array('Link5','Link6'); foreach($something as $links) { //I will assume this is a function you are using so left in place // $url_links = get_links_from_site($links); // ignore this.This is only supposed code which get links. $url_links = $links; $result_array[] = $url_links; } //and one more if (!isset($result_array)){ $result_array = array(); } $something = array('Link7'); foreach($something as $links) { //I will assume this is a function you are using so left in place // $url_links = get_links_from_site($links); // ignore this.This is only supposed code which get links. $url_links = $links; $result_array[] = $url_links; } echo "<pre>"; print_r($result_array); echo "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344405 Share on other sites More sharing options...
cyberRobot Posted May 10, 2012 Share Posted May 10, 2012 Friend "cyberRobort" I had not this question which u answered.Please focus on the question. I only changed the code to demonstrate that the foreach loop should work. But based on the next part of the response, it sounds like you don't want to use a foreach loop... When u execute the $result_arrry in foreach loop then it gets the result link1 , link2 , link3 and link4. But when it is execute out side the foreach loop then it gets the result link3 and link4. This is the problem, I want to get the result link1 , link2 , link3 and link4. when it is result_array execute outside the foreach loop. I think u can now understand this problem. please help me ................thanks......... If the foreach loop gives you the desired results, why use a different solution? When performing the same task on the various elements in an array, a loop tends to be the best option. Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344455 Share on other sites More sharing options...
cyberRobot Posted May 10, 2012 Share Posted May 10, 2012 Well it turns out that there is another option. Have you had a look at array_walk? http://php.net/manual/en/function.array-walk.php The function was mentioned by Barand in another post (Reply #11). http://www.phpfreaks.com/forums/index.php?topic=359090.0;all Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344465 Share on other sites More sharing options...
Xname Posted May 10, 2012 Author Share Posted May 10, 2012 Hi,Frnds....I hope u are all fine. "Drummin" Your answer is good but in your code here is a problem You can write this code only for seven links.Suppose if it is not seven it is seven hundred or may be seven thousand. And if I can not know what are the length of the links. Then I can not be able to write this code for all these links. Please tell me about this problem. ......................THANKS.............................................. Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344490 Share on other sites More sharing options...
Drummin Posted May 10, 2012 Share Posted May 10, 2012 What needs to be understood is how information is being gathered on this page. From what can see you have several instances of the array $something and all we're doing is adding these to one array called $result_array. Of course if the page reloads this all goes away. So what do you expect to happen and how is the information coming to the page. Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344572 Share on other sites More sharing options...
Xname Posted May 11, 2012 Author Share Posted May 11, 2012 Hi,Dear frnds...................I Hope u r all fine. I solved this problem by himself. How to convert the foreach loop in array using php? first of all make two array. sxecute the foreach loop and creat a if statment inside the foreach loop.After that merge these two arrays and get results. <?php $array1 = array(); $array2 = array(); $something = array('execute1','execute2'); foreach($something as $key=> $list){ // if execute1 get some result // if execute2 get some result if($key == 0){ $array1[] = $ur_result; } else{ $array2[] = $ur_result; } } $result_array = array_merge($array1,$array2); print_r($result_array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344725 Share on other sites More sharing options...
Mahngiel Posted May 11, 2012 Share Posted May 11, 2012 Any reason you're looping? Why not just use array_merge() ? Quote Link to comment https://forums.phpfreaks.com/topic/262298-how-to-convert-the-foreach-loop-in-array-using-php/#findComment-1344743 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.