解码json和foreach显示键但不是值

时间:2022-06-01 20:02:06

I cant seem to get the value from the foreach below. I need to basically create a loop where i can then create html buttons based on selections.

我似乎无法从下面的foreach中获得价值。我需要基本上创建一个循环,然后我可以根据选择创建html按钮。

I have also added a snippet example only below this text to show what im trying to achieve within the foreach. I just need to work out how to extract the values so i can do that.

我还在此文本下方添加了一个片段示例,以显示我想在foreach中实现的内容。我只需要弄清楚如何提取值,以便我可以做到这一点。

I am basically wanting to create a foreach loop that checks how many buttons the user has added and then display each button within the loop with a link in href and a custom button name. I will also have to check of they chose 1,2,3,4 from the showBtn value to determine what type of html to output.

我基本上想要创建一个foreach循环来检查用户添加了多少按钮,然后使用href中的链接和自定义按钮名称显示循环中的每个按钮。我还要检查他们从showBtn值中选择1,2,3,4以确定要输出的html类型。

if showBtn==1 { <a herf="btnMenuLink">btnName</a> }

if showBtn==3 { <a herf="btnPhone">btnName</a> }

I have the following code of which i have provided the outputs of the database content and also a var_dump just so you can see how the information is being stored.

我有以下代码,我提供了数据库内容的输出,还有一个var_dump,这样你就可以看到信息的存储方式。

The following code does output the key for me but it wont output the values. And i suspect its because my values are an array as well. How on earth would i create a loop within a loop within a loop and still achieve what i explained above?

以下代码确实为我输出了密钥,但它不会输出值。我怀疑它是因为我的价值观也是一个阵列。我怎么能在循环内的循环中创建一个循环,仍然实现我上面解释的内容?

<?php

$jsonresult =  $column->links;
$array = json_decode($jsonresult,true);

// The databse TEXT field    
/*{
"showBtn":["3","3"],
"btnMenuLink":["101","101"],
"btnArticleLink":["2","2"],
"btnPhone":["036244789","0404256478"],
"btnURL":["",""],
"btnName":["Office","Mobile"]
}*/

// The Var dump $array    
/*  array(6) {
    ["showBtn"] => array(2) {
        [0] => string(1)
        "3" [1] => string(1)
        "3"
    }["btnMenuLink"] => array(2) {
        [0] => string(3)
        "101" [1] => string(3)
        "101"
    }["btnArticleLink"] => array(2) {
        [0] => string(1)
        "2" [1] => string(1)
        "2"
    }["btnPhone"] => array(2) {
        [0] => string(9)
        "036244789" [1] => string(10)
        "0404256478"
    }["btnURL"] => array(2) {
        [0] => string(0)
        "" [1] => string(0)
        ""
    }["btnName"] => array(2) {
        [0] => string(6)
        "Office" [1] => string(6)
        "Mobile"
    }
} */

foreach($array as $key => $value) { ?>    
<?php echo $key;?>:<?php echo $value;?><hr/>    
<?php } ?>

Im still kind of stuck on this,

我还是有点坚持这个,

please find below what im after:

请在下面找到以下内容:

$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';

$array          =   json_decode($jsonresult,true);

foreach ($array as $key => $value) {
    foreach ($value as $next_key => $next_value) { 
        echo $key.":".$next_key.":".$next_value."\n";
    } }

// I want this

// if(showBtn==3) {

// echo '<a herf='tel:btnPhone'>btnName</a>';

// }

// the result would be

// <a href="tel:036244789">Office</a> <a href="tel:0404256478">Mobile</a>

2 个解决方案

#1


That would be because your $value is an array, not a set value. You'll need to loop it one more time:

那是因为你的$ value是一个数组,而不是一个设定值。你需要再次循环它:

foreach($array as $key => $values) {
    foreach($values as $item) {
         echo $key . ":" . $item;
    }
    echo "<hr />";
}

Example

#2


foreach ($array1 as $key1 => $value1) {
    foreach ($value as $key2 => $value2) {
        echo "this is a nested loop";
    }
}

So $value2 will contain the elements in the array.

所以$ value2将包含数组中的元素。

Also, if you are not seeing any error or warning messages when you do echo $value; you should turn on error_reporting as it is extremely useful when developing.

此外,如果您在回显$ value时没有看到任何错误或警告消息;你应该打开error_reporting,因为它在开发时非常有用。

#1


That would be because your $value is an array, not a set value. You'll need to loop it one more time:

那是因为你的$ value是一个数组,而不是一个设定值。你需要再次循环它:

foreach($array as $key => $values) {
    foreach($values as $item) {
         echo $key . ":" . $item;
    }
    echo "<hr />";
}

Example

#2


foreach ($array1 as $key1 => $value1) {
    foreach ($value as $key2 => $value2) {
        echo "this is a nested loop";
    }
}

So $value2 will contain the elements in the array.

所以$ value2将包含数组中的元素。

Also, if you are not seeing any error or warning messages when you do echo $value; you should turn on error_reporting as it is extremely useful when developing.

此外,如果您在回显$ value时没有看到任何错误或警告消息;你应该打开error_reporting,因为它在开发时非常有用。