echo key and value of an array without and with loop

前端 未结 11 1953
野的像风
野的像风 2020-12-29 20:47

This is an array i have


How do i get to echo something

相关标签:
11条回答
  • 2020-12-29 21:28
    foreach($page as $key => $value) {
      echo "$key is at $value";
    }
    

    For 'without loop' version I'll just ask "why?"

    0 讨论(0)
  • 2020-12-29 21:28

    Echo key and value of an array without and with loop

    $array = array(
                'kk6NFKK'=>'name',
                'nnbDDD'=>'claGg',
                'nnbDDD'=>'kaoOPOP',
                'nnbDDD'=>'JWIDE4',
                'nnbDDD'=>'lopO'
             );
    
    
    print_r(each($array));  
    

    Output

    Array
    (
        [1] => name
        [value] => name
        [0] => kk6NFKK
        [key] => kk6NFKK
    )
    
    0 讨论(0)
  • 2020-12-29 21:28

    How to echo key and value of an array without and with loop

    $keys = array_keys($page);
    implode(',',$keys);
    echo $keys[0].' is at '.$page['Home'];
    
    0 讨论(0)
  • 2020-12-29 21:33
    array_walk($v, function(&$value, $key) {
       echo $key . '--'. $value;
     });
    

    Learn more about array_walk

    0 讨论(0)
  • 2020-12-29 21:38

    for the first question

    $key = 'Home';
    echo $key." is at ".$page[$key];
    
    0 讨论(0)
  • 2020-12-29 21:44

    My version without a loop would be like this:

    echo implode(
        "\n", 
        array_map(
             function ($k, $v) { 
                 return "$k is at $v"; 
             }, 
             array_keys($page), 
             array_values($page)
        )
    );
    
    0 讨论(0)
提交回复
热议问题