Jump to content

string to array!!!

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Hello,
I want to change an string to array but I get problem with it:
This my code to make array
    function parse_response($response) {

        if (is_array($response)) {

            $PARSED = array();
            $CURRENT = null;

            for ($i = 0, $imax = count($response); $i < $imax; $i++) {

                if (in_array($response[$i], array('!fatal', '!re', '!trap'))) {

                    if ($response[$i] == '!re')
                        $CURRENT = &$PARSED[];
                    else
                        $CURRENT = &$PARSED[$response[$i]][];

                }
                else
                if ($response[$i] != '!done') {

                    if (preg_match_all('/[^=]+/i', $response[$i], $MATCHES))
                        $CURRENT[$MATCHES[0][0]] = (isset($MATCHES[0][1]) ? $MATCHES[0][1] : '');

                }

            }

            return $PARSED;

        }
        else
            return array();

    }
but I get
Warning: preg_match_all() expects parameter 2 to be string, array given in /home/hamed4u/public_html/routeros_api.class.php on line 171

How can I fix it!!

output string

Quote

[20, 142] =primary-dns=4.2.2.4 >>> [22, 119] =secondary-dns=8.8.8.8 >>> [28, 90] =allow-remote-requests=false >>> [24, 65] =max-udp-packet-size=512 >>> [16, 48] =cache-size=2048 >>> [25, 22] =cache-max-ttl=1w00:00:00 >>> [13, 8] =cache-used=6 >>> [5, 1]


#2
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Well, it seems that one (possibly all) of your $response[$i] is an array as opposed to a string but since you didn't provide the code where you generate the $response object I can't really help you with that.

Try showing us the output of var_dump($response)

On a side note, that whole if/else structure would be much cleaner and more efficient if you did it as such:
if ($response[$i] == '!re') {

...

}

else if ($response[$i] == '!fatal' || $response[$i] == '!trap') {

...

}

else if ($response[$i] != '!done') {

...

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users