简体   繁体   中英

PHP- Get each value of an associative array in a foreach loop, then apply a function and echo the result

I have a piece of code similar to below:

    $list_links = array(
        [0] => Array // Here I get this error message Parse error: syntax error, unexpected '[', expecting ')'
            (
                "http://test.com/1/",
                "http://test.com/2/"
            ),
        [1] => Array
            (
                "http://test.com/3/",
                "http://test.com/4/"
            )
            );
    //Code stops to execute here        
    foreach($list_links as $link) {
        DoWork($link, $db_connect);
        if ($list_links[0]) {
            echo "this URL is from the 0 list";
        } else if ($list_links[1]) {
            echo "this URL is from the 1 list";
        }
    }
    //DoWork function works well and is already tested 
        function DoWork($link, $db_connect){
        ...
        ...
        ...
    }

The problem is that the code stops to execute the DoWork() function when it reaches the foreach() loop because it can not get each URL from the list. Below are my questions: 1. Could you please see if foreach($list_links as $link) syntax is correct? 2. Are the if formats of if ($list_links[0]) and if ($list_links[1]) correct to echo the messages?

I wouldn't suggest doing a DB call within a loop like this, but to answer your question:

Assign a variable to the function result

$list_links = DoWork($link, $db_connect);
if ($list_links[0]) {
    echo "this URL is from the 0 list";
 } else if ($list_links[2]) {
    echo "this URL is from the 1 list";
 }

Syntax error

$list_links = array(
        [0] => Array
            (
                "http://test.com/1/",
                "http://test.com/2/", <---- remove this ,
            ),
        [1] => Array
            (
                "http://test.com/3/",
                "http://test.com/4/",  <---- remove this ,
            )
            );

In this context, foreach($list_links as $link) , $link is not the URL, it is an array of URLs. Try a nested foreach instead:

foreach ($list_link as $link_array) {
    foreach ($link_array as $link) {
        DoWork($link, $db_connect);
    }
}

This will solve the first part of your problem. However, I'm not sure what you're trying to accomplish with if($list_links[0]) ... . This just checks if $list_links[0] is true or false, essentially.

Further, regardless of what you're trying to do, it doesn't seem like much of an error check, because, given your data, each URL will always be apart of either $list_link[0] or $list_link[1] . If you want to simply state from which list each URL comes from, try this:

foreach ($list_link as $key=>$link_array) {
    foreach ($link_array as $link) {
        DoWork($link, $db_connect);
        echo 'this url is from the '.$key.' list';
    }
}

In regard to Parse error: syntax error, unexpected '[', expecting ')' :

Remove the brackets from the keys. When declaring an array, they are not used.

$list_links = array(
    0 => array
        (
            "http://test.com/1/",
            "http://test.com/2/"
        ),
    1 => array
        (
            "http://test.com/3/",
            "http://test.com/4/"
        )
);

Note that if the key to an array element is a string, you must wrap it in quotes:

$array = array ('key' => 'value')

Further note that the default keys for an array are integers starting from 0, so in your case, you could simply remove the keys altogether to get the same result:

$list_links = array(
    array("http://test.com/1/", "http://test.com/2/"),
    array("http://test.com/3/", "http://test.com/4/")
);

This will automatically assign these two elements the keys 0 and 1, without the need to explicitly declare such. See a demo .

It could be this:

foreach($list_links as $link) {
    DoWork($link, $db_connect);
    if (in_array($links, $link_list[0])) {
        echo "this URL is from the 0 list";
    } elseif (in_array($links, $link_list[1])) {
        echo "this URL is from the 1 list";
    }
}

Notice that I use the recursive features of in_array() to check to see if the $links array has values in the 'zero' array $link_list[0] , or the 'one' array $link_list[1] .

PHP Manual: in_array()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM