这里使用的最佳类型的数组/循环是什么?

时间:2022-09-10 23:39:35

Let's say I have a simple markup.

假设我有一个简单的标记。

<div class="wrapper" id=" <?php echo $id; ?> ">
  <div class="title"> <?php echo $title; ?> </div>
  <div class="inner-text"> <?php echo $inner-text; ?> </div>
</div>

I have a long list of titles,id's and inner text's that need to be inserted where the variables are and this block of code will need to be repeated at least 50-100 times.(There is no set amount as of right now) Each $id,$title and $inner-text will not be random.

我有一长串标题,id和内部文本需要在变量所在的地方插入,这段代码需要重复至少50-100次。(目前没有设定金额) $ id,$ title和$ inner-text不会是随机的。

Example:

例:

car

汽车

car

汽车

this is a fast car

这是一辆快车


This is how they should be represented

这就是它们应该如何表现的方式

$id = car

$ id = car

$title = car

$ title = car

$inner-text = this is a fast car

$ inner-text =这是一辆快车

How can I assign each set of three peices of information to individual variables, then put them into a <div> and loop through them until there is no more left to loop from the list.

如何将每组三个信息分配给单个变量,然后将它们放入

并循环遍历它们,直到从列表中不再循环。

If you don't understand what I mean then think of while loop with an associative array from a mysql database

如果您不明白我的意思,那么请考虑使用mysql数据库中的关联数组进行while循环

I wish to do the same thing but just without the database part

我希望做同样的事情,但只是没有数据库部分

5 个解决方案

#1


1  

Make an array with arrays:

使用数组创建一个数组:

$array = array(

array('id' => 1,
        'title' => 'somename'),
        'innertext' => 'sometext'),

array('id' => 1,
        'title' => 'somename'),
        'innertext' => 'sometext')
);

Then you can easily look through it afterwards like

然后,您可以轻松地查看它

foreach($array as $row) {

    $id = $row['id'];
    $title = $row['title'];
    $innertext = $row['innertext'];
}

#2


0  

Let's say you retrieve your data with a query.

假设您使用查询检索数据。

$query = "SELECT id, title, inner_text FROM table";
$result = mysqli_query($link,$query); //Be sure to establish the mysqli instance in your header or configuration file.

while($row = mysqli_fetch_assoc($result))
{
    echo '<div class="wrapper" id="'. $row['id'] .'">';
        echo '<div class="title">'. $row['title']; .'</div>';
        echo '<div class="inner-text">'. $row['inner_text'] .'</div>';
    echo '</div>';
}

This is one of the basic methods of generating html code with php. The other way is with a nested array with your prefilled data.

这是用php生成html代码的基本方法之一。另一种方法是使用带有预填充数据的嵌套数组。

You could use an foreach loop on the array and generate it in the same fashion. I don't know if you have a prebuilt array but if you show it to me I can edit my answer and give an example.

您可以在阵列上使用foreach循环并以相同的方式生成它。我不知道你是否有一个预制的阵列,但如果你向我展示,我可以编辑我的答案并举例。

#3


0  

First you should put those variables in array. For example: $data = array ('id' => someid, 'title' => sometitle,.... and so on )

首先,您应该将这些变量放在数组中。例如:$ data = array('id'=> someid,'title'=> sometitle,....等等)

Next you should make foreach loop as folows:

接下来你应该将foreach循环作为folows:

<?php foreach($data as $rows_of_data_array) : ?>

 <div class="wrapper" id=" <?=$rows_of_data_array['id']; ?> ">
   <div class="title"> <?=$rows_of_data_array['title']; ?> </div>
   <div class="inner-text"> <?=$rows_of_data_array['inner-text']; ?> </div>
 </div>

<?php endforeach?>

#4


0  

If you have one array of objects :

如果您有一个对象数组:

foreach($myArrayOfObjects as $object){
?>
<div class="wrapper" id="<?php echo $object->id; ?> ">
        <div class="title"><?php echo $object->title; ?> </div>
        <div class="inner-text"><?php echo $object->innerText; ?> </div>
</div>
<?php
}

If you have one array of arrays (multidimensional array) :

如果你有一个数组数组(多维数组):

foreach($myArrayOfcontentArrays as $contentArray){
?>
<div class="wrapper" id="<?php echo $contentArray['id']; ?> ">
        <div class="title"><?php echo $contentArray['title']; ?> </div>
        <div class="inner-text"><?php echo $contentArray['inner-text']; ?> </div>
</div>
<?php
}

If you have 3 correctly indexed and same sized arrays ($ids, $titles and $innerTexts) :

如果你有3个正确索引和相同大小的数组($ ids,$ titles和$ innerTexts):

for ($i= 0; $i < count($ids), $i++){
?>
<div class="wrapper" id="<?php echo $ids[$i]; ?> ">
        <div class="title"><?php echo $titles[$i]; ?></div>
        <div class="inner-text"><?php echo $innerTexts[$i]; ?> </div>
</div>
<?php   
}

#5


0  

Use multi-dimensional array.

使用多维数组。

$array=array(
  array(0 =>'id', 1 =>'title', 2=>'inner-text'),
  array(0 =>'id1', 1 =>'title1', 2=>'inner-text1'),
  array(0 =>'id2', 1 =>'title2', 2=>'inner-text2')
);

$c=count($array); //the value of this is 3

for($i=0; $i<$c; $i++){
  foreach($array as $key => $value){
    echo $array[$i][$key]. "<br />";
  }
}

#1


1  

Make an array with arrays:

使用数组创建一个数组:

$array = array(

array('id' => 1,
        'title' => 'somename'),
        'innertext' => 'sometext'),

array('id' => 1,
        'title' => 'somename'),
        'innertext' => 'sometext')
);

Then you can easily look through it afterwards like

然后,您可以轻松地查看它

foreach($array as $row) {

    $id = $row['id'];
    $title = $row['title'];
    $innertext = $row['innertext'];
}

#2


0  

Let's say you retrieve your data with a query.

假设您使用查询检索数据。

$query = "SELECT id, title, inner_text FROM table";
$result = mysqli_query($link,$query); //Be sure to establish the mysqli instance in your header or configuration file.

while($row = mysqli_fetch_assoc($result))
{
    echo '<div class="wrapper" id="'. $row['id'] .'">';
        echo '<div class="title">'. $row['title']; .'</div>';
        echo '<div class="inner-text">'. $row['inner_text'] .'</div>';
    echo '</div>';
}

This is one of the basic methods of generating html code with php. The other way is with a nested array with your prefilled data.

这是用php生成html代码的基本方法之一。另一种方法是使用带有预填充数据的嵌套数组。

You could use an foreach loop on the array and generate it in the same fashion. I don't know if you have a prebuilt array but if you show it to me I can edit my answer and give an example.

您可以在阵列上使用foreach循环并以相同的方式生成它。我不知道你是否有一个预制的阵列,但如果你向我展示,我可以编辑我的答案并举例。

#3


0  

First you should put those variables in array. For example: $data = array ('id' => someid, 'title' => sometitle,.... and so on )

首先,您应该将这些变量放在数组中。例如:$ data = array('id'=> someid,'title'=> sometitle,....等等)

Next you should make foreach loop as folows:

接下来你应该将foreach循环作为folows:

<?php foreach($data as $rows_of_data_array) : ?>

 <div class="wrapper" id=" <?=$rows_of_data_array['id']; ?> ">
   <div class="title"> <?=$rows_of_data_array['title']; ?> </div>
   <div class="inner-text"> <?=$rows_of_data_array['inner-text']; ?> </div>
 </div>

<?php endforeach?>

#4


0  

If you have one array of objects :

如果您有一个对象数组:

foreach($myArrayOfObjects as $object){
?>
<div class="wrapper" id="<?php echo $object->id; ?> ">
        <div class="title"><?php echo $object->title; ?> </div>
        <div class="inner-text"><?php echo $object->innerText; ?> </div>
</div>
<?php
}

If you have one array of arrays (multidimensional array) :

如果你有一个数组数组(多维数组):

foreach($myArrayOfcontentArrays as $contentArray){
?>
<div class="wrapper" id="<?php echo $contentArray['id']; ?> ">
        <div class="title"><?php echo $contentArray['title']; ?> </div>
        <div class="inner-text"><?php echo $contentArray['inner-text']; ?> </div>
</div>
<?php
}

If you have 3 correctly indexed and same sized arrays ($ids, $titles and $innerTexts) :

如果你有3个正确索引和相同大小的数组($ ids,$ titles和$ innerTexts):

for ($i= 0; $i < count($ids), $i++){
?>
<div class="wrapper" id="<?php echo $ids[$i]; ?> ">
        <div class="title"><?php echo $titles[$i]; ?></div>
        <div class="inner-text"><?php echo $innerTexts[$i]; ?> </div>
</div>
<?php   
}

#5


0  

Use multi-dimensional array.

使用多维数组。

$array=array(
  array(0 =>'id', 1 =>'title', 2=>'inner-text'),
  array(0 =>'id1', 1 =>'title1', 2=>'inner-text1'),
  array(0 =>'id2', 1 =>'title2', 2=>'inner-text2')
);

$c=count($array); //the value of this is 3

for($i=0; $i<$c; $i++){
  foreach($array as $key => $value){
    echo $array[$i][$key]. "<br />";
  }
}