Jump to content

Looping through and updating an Array


xProteuSx

Recommended Posts

I've got a data array that looks like this:

 

$arraylist = ('00101001','10001010','00010100','01100101');

 

And another that looks like this:

 

$arrayposition = (0,3);

 

The idea is to loop through $arraylist and when the loop gets to the indexes/positions listed in the $arrayposition array, to change all of the 1's to 0's, then put the values back into a string variable.

 

I have something like this at the moment:

 

$chores = 1;

$arraylist_new = '';

 

foreach ($arrayposition as $value)

        {

        if ($chores != '')

            {

            $oldvalnum = 0;

           

            foreach($arraylist as $string)

                {

                if ($value == $oldvalnum)

                {

                        $string = str_replace("1", "0", $string);

                        if ($arraylist_new == '')

                        {$arraylist_new = $string;}

                        else

                        {$arraylist_new .= ', ' . $string;}

                        }

                else

                {

                        if ($arraylist_new == '')

                        {$arraylist_new = $string;}

                        else

                        {$arraylist_new .= ', ' . $string;}

                        }

                $oldvalnum++;

                }

            }

        }

echo $arraylist_new;

 

The idea is for the output to be:  00000000, 10001010, 00000000, 01100101.

 

I know the code is hinda hectic, but could someone please help??  Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/250834-looping-through-and-updating-an-array/
Share on other sites

Yeah, you've got that way over complicated. If it's just as simple as you've described, it can be done with one foreach() loop using str_replace, strlen and implode. There are probably other ways to do it as well, but this is what I came up with of the top of my head. Note that this only works if the values in $arraylist are quoted strings.

 

<?php
$arraylist = array( '00101001', '10001010', '00010100', '01100101' );
$arrayposition = array( 0,3 );

foreach( $arrayposition as $v ) {
   $arraylist[$v] = str_repeat( '0', strlen($arraylist[$v]) );
}

$arraylist_new = implode( ', ', $arraylist );
echo $arraylist_new;

Pikachu2000 (lol),

 

What would you say if I told you that, instead of 0's and 1's  ...

 

$arraylist = ( [0] => 26|7|0|10|0|0 [1] => 3|1|3|5|0|0 [2] => 3|2|3|5|0|0 [3] => 3|3|3|5|0|0 )

 

And that I was trying to switch the 0 to a 1 at index 4 ??

 

So, for example, if

 

$arrayposition = array( 0,3 );

 

The I would like to update $array list from

 

$arraylist = ( [0] => 26|7|0|10|0|0 [1] => 3|1|3|5|0|0 [2] => 3|2|3|5|0|0 [3] => 3|3|3|5|0|0 )

 

to

 

$arraylist = ( [0] => 26|7|0|10|1|0 [1] => 3|1|3|5|1|0 [2] => 3|2|3|5|0|0 [3] => 3|3|3|5|0|0 )

 

????

 

I'm sorry, but I simplified the code that I posted (insert rant about over-complication here) so yeah, values in $arraylist are not in quoted strings.

 

Forgive me??  ;)  ....  thank you by the way.

 

And there I was thinking I had come up with a nice, elegant solution. :(

 

Can you post the output of this, using your actual data, or at least part of it?

echo '<pre>';
print_r($arraylist);
echo '</pre>';

 

I see what you're trying to do, but what determines the array element that needs to be changed? Is it simply one higher than the highest value in $arrayposition?

Pikachu2000 (lol, again!)

 

You did come up with an elegant solution!  I figured it out, BASED ALMOST ENTIRELY ON YOUR ELEGANT SOLUTION!  Here's what I've got:

 

<?php

$arraylist = array('26|7|0|10|0|0','3|1|3|5|0|0','3|2|3|5|0|0', '3|3|3|5|0|0') ;

$arrayposition = array( 0,1 );

 

//foreach( $arrayposition as $v )

// {

//  $arraylist[$v] = str_repeat( '0', strlen($arraylist[$v]) );

// }

 

foreach ($arrayposition as $v)

{

    $onechore = explode("|", $arraylist[$v]);

$onechore[4] = 1;

$chore = implode("|", $onechore);

    $arraylist[$v] = $chore;

    }

 

$arraylist_new = implode( ', ', $arraylist );

echo $arraylist_new;

 

?>

 

Output is:

 

26|7|0|10|1|0, 3|1|3|5|1|0, 3|2|3|5|0|0, 3|3|3|5|0|0

 

Magic!

 

Thank you dear sir.  Thank you, thank you, thank you.

 

I've spent about 5 hours on this and I've come up with variations that go up to 100+ lines of code.  Now I've got a beauty in about 15 lines.

 

PS - you said you hate everything, yet you appear to like candy ...

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.