0

Lets I have an array like this

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

<?php 
 $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
?>
    <div id="3">
          <!-----result of foreach loop---->
    </div>
    <div id="1">
         <?php 
            foreach ($age as $key => $value) {?>
                    <p><?php echo $value;?></p>
            <?php
            }
         ?>
    </div>
    <div id="2">
        <!-----result of foreach loop---->
    </div>
<?php
?>

I want to print the same result in <div id="2"> and in <div id="3"> , without loop through the array in , I just want to use the loop only for one time .

3
  • Why? What are you trying to achieve by not having multiple loops? You could concatenate your loop into a variable and echo it in multiple places. Commented Sep 25, 2014 at 5:07
  • If I have an array of million items , then surely my script will be slow . Commented Sep 25, 2014 at 5:09
  • Your browser will be worse ;) Commented Sep 25, 2014 at 5:11

6 Answers 6

3
 <?php 
            $pAge = "";
            foreach ($age as $key => $value) {
               $pAge .= "<p>" . $value . "</p>";
            }
 ?>

    <div id="3">
          <?= $pAge; ?>
    </div>
    <div id="1">
          <?= $pAge; ?>
    </div>
Sign up to request clarification or add additional context in comments.

Comments

2

You can try putting all of them in a variable and then echo them out.
Something like this -

<?php 
 $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
 $display_str = "";
 foreach ($age as $key => $value) {
    $display_str .= $value;
 }
?>
<div id="1">
  <?php echo $display_str;?>     
</div>
<div id="2">
    <!-----result of foreach loop---->
    <?php echo $display_str;?>
</div>

Comments

2

Just store the html before the div part and print it three times:

<?php 
    $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

    $htmlString = '';
    foreach ($age as $key => $value) 
    {
        $htmlString .= '<p>'.$value.'</p>';
    }
?>

<div id="3">
   <?=$htmlString?>
</div>
<div id="1">
   <?=$htmlString?>
</div>
<div id="2">
   <?=$htmlString?>
</div>  

Comments

1

Just store the result values in array in use in anywhere like this:

<?php 
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
?>
    <div id="3">
          <!-----result of foreach loop---->
    </div>
    <div id="1">
         <?php 
            $res = array();
            foreach ($age as $key => $value) {?>
                    <p><?php 
                    $res[] = $value;
                    echo $value;?></p>
            <?php
            }
         ?>
    </div>
    <div id="2">
        <!-----result of foreach loop---->
        <?php
            echo '<pre>';
            print_r($res); ?>
        <p><?php 
            echo $res[0];
            echo $res[1];
            echo $res[2];
        ?></p>
    </div>
<?php
?>

Comments

1

You can do this like as:

<div id="1">


 <?php 
        $html="";
        foreach ($age as $key => $value) {


                $html .= "<p>".$value."</p>";
                }
               echo $html;
                ?>

</div>
<div id="2">
    <?php echo $html; ?>
</div>

1 Comment

Why are you echoing $html in the loop?
1

You can also do it using :

<?php 
  function myfunction($value,$key)
  {
      echo "$value<br>";
  }

 $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
?>
    <div id="3">
         <?php array_walk($age,"myfunction"); ?>
    </div>
    <div id="1">
        <?php array_walk($age,"myfunction"); ?>
    </div>
    <div id="2">
        <?php array_walk($age,"myfunction"); ?>
    </div>

Comments

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.