Jump to content

Recommended Posts

Hello Everyone,

I got stuck. I have a headache already, please help.

Problem is. Array example:


$part[0][0] = "value1";
$part[0][1] = "1";

$part[1][0] = "value2";
$part[1][1] = "1";

$part[2][0] = "value3";
$part[2][1] = "2";

$part[2][0] = "value4";
$part[2][1] = "2";

$part[3][0] = "value5";
$part[3][1] = "3";

 

I am looping this array with for loop. In loop i need to call a function when $part[$i][1] changes. I tried to do something like this:

 

$ch ="";
for ($i=0;$i<count($part);$i++)
{ 
            //making an array here based on $part[$i][0], let's call it $data.
    
    if($ch != $part[$i][1])
    {
         function insert($data);
         $ch = $part[$i][1];
    }
}

 

But then the function is inserting $data gathered for $part[0].

 

The question is: What should i to make that insert function to be called when $part[$i][1] changes ??

 

 

Link to comment
https://forums.phpfreaks.com/topic/206706-looping-array-issue/
Share on other sites

How you have setup your for loop is correct. However it can be cleaned up with a foreach loop

function insert($data)
{
echo "inserting... $data<br />";
}

$prev = '';
foreach($part as $data)
{	
if($prev != $data[1])
	insert($data[0]);

$prev = $data[1];
}

 

What are you planning on doing with the insert() function?

Link to comment
https://forums.phpfreaks.com/topic/206706-looping-array-issue/#findComment-1081038
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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