mardi 5 mai 2015

Decode json and foreach Shows key but not value

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.

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.

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.

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.

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 } ?>

Aucun commentaire:

Enregistrer un commentaire