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

Sami

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?

Mark Miller

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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

getting array values without foreach loop

来自分类Dev

SQL query in Foreach loop using array variable

来自分类Dev

Perl : How to use constant tag value as key of associative array in?

来自分类Dev

Javascript array with associative array difference

来自分类Dev

Apply function to each pixel of the image

来自分类Dev

indexeddb: how to get request result and cursor value

来自分类Dev

How do I merge two associative arrays such that key collisions result in an array of both values?

来自分类Dev

Get function result instead of "<function1>"

来自分类Dev

How to get the value of a node using for each and if in xpath

来自分类Dev

How to set associative array?

来自分类Dev

PHP - Is array_map faster than foreach?

来自分类Dev

PHP-array_map是否比foreach快?

来自分类Dev

Merge PHP Associative Array

来自分类Dev

ajax GET - result from php not passing through

来自分类Dev

Get the first occurence of the result in each specified group

来自分类Dev

PHP get a value from array index

来自分类Dev

calling a synchronous function (with call back) in foreach with the help of async.each

来自分类Dev

PHP Foreach array to table display

来自分类Dev

How do I get a value from an associative array using Swift

来自分类Dev

Is there a built in Kotlin method to apply void function to value?

来自分类Dev

PHP JSON foreach Array Issue

来自分类Dev

How to use promise in forEach loop of array to populate an object

来自分类Dev

PHP foreach数组并与GET比较

来自分类Dev

php foreach echo打印“ Array”作为值

来自分类Dev

PHP loop and get lowest price

来自分类Dev

PHP-PDO echo $ array ['name']; 不管用

来自分类Dev

JQuery GET 请求不会从 php echo 中获取正确的数据

来自分类Dev

php:array sum没有foreach

来自分类Dev

PHP echo end($array) 不打印任何内容

Related 相关文章

热门标签

归档