How to convert the foreach loop in array using php?

A

Anonymous

Guest
Hi...Dear frnds.
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..........…
 
I don't see where you are declaring the $something array. I also don't see how this is being executed twice.

Can you give us the full code segment?
 
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 google.com and facebook.com

// when it is executed second time it get the links from "url2"
// i.e .wikipedia.com and php.com.

$result_array[] = $urls_links
}
print_r($result_array);
// it can return only those links which can be taken from "url2"
// i.e wikipedia.com and php.com.
But I want to get the all links
i.e google.com and facebook.com...wikipedia.com and php.com.
 
You seem to have left out the most important portion of the code.

If "get_url_links" is a real function that you have coded, I need to see the contents of that function, other wise I need to see what is going inside of your foreach loop.

This tells me nothing:
Code:
//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 google.com and facebook.com

// when it is executed second time it get the links from "url2"
// i.e .wikipedia.com and php.com.


From what I see if your "get_url_links" function works correctly and does retrieve the correct information on execution then your array should be populating correctly.

You aren't giving me anything to work with though; you are in essence giving me really bad metaphors for what you are actually trying to accomplish.
 
Hi,Frnd......I hope u r all fine.
My question is that when foreach loop execute two times
and add the result in any array.It can add only those result
in array which is gets from when foreach loop execute second time.It can ignore those
result which gets from when foreach loop execute first time.
I want that for each loop can not ignore any result.
This is a question that i want to ask.
 
Yes but you aren't giving me your full code segment so, like I said before, from what I see everything should be "Working as Intended".

You have however left out a big chunk of your code, almost everything inside of the foreach loop. Without seeing that code and the code for the non-standard functions that are being called it seems I will not be able to find the source of your error.

Essentially you have given me:
Code:
foreach($something as $anything) {
      //Pay no attention to the man behind the curtain even this controls everything else.

     $any_array[] = $anything;
}
 //unexpected output.


I need to know what you are doing inside the loop that is causing the error. Are you re-initializing a variable? Are you failing to execute a code segment that you are expecting to execute?

More than likely you are experiencing a scope hole, but I can't figure out the source of that error without seeing your real code. You are obviously leaving out important details.
 
Forget all above codes.Just focus on it.
My question is that when foreach loop execute two times
and add the result in any array.It can add only those result
in array which is gets from when foreach loop execute second time.It can ignore those
result which gets from when foreach loop execute first time.
I want that for each loop can not ignore any result.
This is a question that i want to ask.
ok.just focus it.
 
Besides the obvious language barrier, you seem to lack the understanding that I, and no one else can help you if you don't supply the code. There is no way to fully figure out what your problem is without seeing the code. The computer will not except my best guess. My best guess will be completely unfounded since I do not have the information I need find a conclusion.

My recommendation is that you find someone who you trust enough to show the code and have them debug it.
 
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.
Code:
<?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);

?>
 
It's a solution, but not a good one. You can't scale this infinitely since you now have to initialize additional variables for every loop and then merge them all together.

If you had just not been so impossible to deal with I could have designed a much better solution for you.
 
Back
Top