4

im trying to print an array using implode, but i want to tweak it, so the "glue" of the implode show every two element, and not in every element.

$nombreNombre=array('josh','13','mike','44','dude','98','scott','450');
echo '<li>' . implode('</li><li>', $nombreNombre).'</li>

with that im getting:

  • josh
  • 13
  • mike
  • 44
  • dude
  • 98
  • scott
  • 450
  • and i want:

  • josh 13
  • mike 44
  • dude 98
  • scott 450

    1 Answer 1

    6

    You could run $nombreNombre through array_chunk, do an array_map to convert each pair to a string, then implode.

    $arr = array('josh','13','mike','44','dude','98','scott','450');
    $arr = array_chunk($arr, 2);
    function repr($pair) { list($a, $b) = $pair; return "$a $b"; }
    $arr = array_map("repr", $arr);
    echo '<li>' . implode('</li><li>', $arr) . '</li>';
    
    Sign up to request clarification or add additional context in comments.

    2 Comments

    Can you give the sample code? I am wondering how you tackle it.
    I like the way you used the built-in functions. Thanks

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.