array to string my foot

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

array to string my foot

Post by shiznatix »

ok i have a array with a key and value (w00t) now im sorting this array into a tree for reasons unknown but its what i gotta do. now here is my code.

Code: Select all

foreach ($array as $key => $val)
	{
		$new[$key] = array('value' => $val);
		unset($array[$key]);

		if (count($new) > count($arrnum)-1)
			break;
	}

    $array = ashuffle($array);

    foreach ($new as $key => $val)
    {
        if (count($array) > 
            $num = rand (5, ;
        else
            $num = (count($array));

        if (count($array) == 1)
        {
            break;
        }

        $i = 1;

        foreach ($array as $key2 => $val2)
        {
            if ($i > $num)
                break;
            
            $i++;

            $found = '';

            if ($found == '')
            {
                if (false !== isset($array[$key2]))
                    $found = $array[$key2];
                else                
                    break;
                unset($array[$key2]);
            }

            $new[$key][$key2] = array('value' => $val2);
            unset($found);

            if (count($array) == 1)
            {
                if (count($new[$key]) == 1)
                {
                    foreach ($array as $arr)
                        $new[$key][$arr] = array();
                }
                else
                {
                    foreach ($array as $arr)
                        $new[$arr] = array();
                }
                $array = array();
                break;
            }

            $array = ashuffle($array);
        }

        if (count($array) < 1)
            break;
    }

    if (count($array) > 1)
    {
        foreach ($new as $key => $val)
        {
            foreach ($new[$key] as $key2 => $val2)
            {
                if (count($array) > 
                    $num = rand (5, ;
                else
                    $num = (count($array));

                $i = 0;

                foreach ($array as $key3 => $val3)
                {
                    if ($i > $num)
                        break;
                    $i++;

                    if ($i != 1)
                    {

                        $found = '';

                        if ($found == '')
                        {
                            if (false !== isset($array[$key3]))
                                $found = $array[$key3];
                            else
                                break;
                            unset($array[$key3]);
                        }

                        $new[$key][$key2][$key3] = array('value' => $val3);//death here AHHHH
                        unset($found);

                        if (count($array) == 1)
                        {
                            if (count($new[$key][$key2]) == 1)
                            {
                                $new[$key][$key2][$key3] = array('value' => $val3);
                            }
                            else
                            {
                                foreach ($array as $arr)
                                    $new[$key][$key3] = array('value' => $val3);
                            }
                            $array = array();
                            break;
                        }

                        $array = ashuffle($array);
                    }
                }                

                if (count($array) < 1)
                    break;
            }

            if (count($array) < 1)
                break;
        }
    }
but where i marked death here I get this error

Notice: Array to string conversion in /home/users/andrew/public_html/function_5b.php on line 237

if i turn error_reporting to regular that goes away and there appears to be no problem until this stuff starts happening...

it seams kinda random but i will get like massive blank spaces thrown into the parts of the new array and it will just remove some characters and it will throw in random ä and Ä here any there - but not all the time just on some values usually.

the end result is looking like this

Code: Select all

Array
(
    [rc] => Array
        (
            [value] => Ac
            [rc_wiley] => Array
                (
                    [value] => rc wiley
                    [hpi_rc_cars] => Array
                        (
                            [value] => hpi rc cars
                        )

                    [gas_powered_remote_control_airplanes] => Array
                        (
                            [value] => gas powered remote control airplanes
                        )

                    [remote_control_planes] => Array
                        (
                            [value] => Remote Control planes
                        )

                    [remote_control_gas_submarines] => Array
                        (
                            [value] => Remote Control Gas Submarines
                        )

                    [remote_control_boats_nitro] => Array
                        (
                            [value] => remote control boats nitro
                        )

                )

            [rc_jet_plane] => Array
                (
                    [value] => Rc Jet Plane
                    [remote_control_fire_boat] => Array
                        (
                            [value] => remote control fire boat
                        )

                    [discount_rc_nitro_car] => Array
                        (
                            [value] => Discount Rc Nitro Car
                        )

                    [ready_to_fly_remote_control_airplanes] => Array
                        (
                            [value] => ready to fly remote control airplanes
                        )

                    [rc_planes] => Array
                        (
                            [value] => RC PLANES
                        )

                    [remote_control_helicopter_scale_parts] => Array
                        (
                            [value] => remote control helicopter scale parts
                        )

                    [rc_jet_video_clips] => Array
                        (
                            [value] => Rc Jet Video Clips
                        )

                    [remote_control_boats_pool] => Array
                        (
                            [value] => Remote Control Boats Pool
                        )

                    [trinity_rc] => Array
                        (
                            [value] => trinity rc
                        )

                )
...and so on and that looks good and all but for some reason that first [value] => Ac is supposed to be Rc, if i echo out the val when i set that it says Rc but for some reason it gets changed somewhere somtime...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sounds like a giant load of debugging fun is in your future young padawan. :)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ok well i found the problem it was becuase of the [value] => 'some strange value' thing but i fixed that. now instead of making a new topic ill just post here

Code: Select all

$tmpkey = str_replace('_', ' ', $key);
                $tmpkey = strtolower($tmpkey);
                $tmpval = strtolower($val2);

                echo '--'.$tmpkey.'-'.$tmpval.'--<br>';

                if (false !== strpos($tmpkey, $tmpval))
                    die('YAY A MATCH!');
returns this

--rc-rc motorcycle--
--rc-remote control air planes--
--rc-grants passore remote control airplane club--
--rc-remote control airplane supplies in canada--

and so on... but why would the 1st one not catch and say yay a match and die!!!??? any sugestions would be great
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your arguments to strpos() are reversed ;)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

haha it was monday of course, i seriously spent like 3 hours on that :oops:
Post Reply