Jump to content

How to convert the foreach loop in array using php?


Xname

Recommended Posts

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..........…

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);
?>

:( 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.

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 )

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

 

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>";
?>

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.

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

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

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.

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);

?>

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.