Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组中div的PHP输出_Php_Arrays - Fatal编程技术网

数组中div的PHP输出

数组中div的PHP输出,php,arrays,Php,Arrays,我无法从数组中正确放置div,获得所需的输出。我无法理解如何以及在何处获取和放置变量的逻辑 我花了5个多小时在各种if…else循环上设置和取消设置变量等,但我似乎不理解获得所需输出的逻辑。。。希望有个善良的人能帮助我,因为我的脑子快沸腾了 Php代码: $A = array(1,1,2,3,3,3); $B = array(1,2,NULL,1,2,3); $C = array('Title1','lorem','Title2','Title3','dolor','sit'); $LEN =

我无法从数组中正确放置
div
,获得所需的输出。我无法理解如何以及在何处获取和放置变量的逻辑

我花了5个多小时在各种if…else循环上设置和取消设置变量等,但我似乎不理解获得所需输出的逻辑。。。希望有个善良的人能帮助我,因为我的脑子快沸腾了

Php代码:

$A = array(1,1,2,3,3,3);
$B = array(1,2,NULL,1,2,3);
$C = array('Title1','lorem','Title2','Title3','dolor','sit');

$LEN = count($A);
$i = 0;

while ($i < $LEN) {
$i++;

// Collecting the data here into a variable by looping through the arrays. 
// I have tried multiple things, like a bunch of IF statements, checking if
// $C is NULL etc. but it never works. Too complicated.
}
$A=数组(1,1,2,3,3,3);
$B=数组(1,2,NULL,1,2,3);
$C=数组('Title1'、'lorem'、'Title2'、'Title3'、'dolor'、'sit');
$LEN=计数($A);
$i=0;
而($i<$LEN){
$i++;
//通过数组循环将数据收集到变量中。
//我尝试了很多方法,比如一堆IF语句,检查IF
//$C是空的,等等。但它永远不会工作。太复杂了。
}
预期结果:

<div>
    <div>Title1</div>
    <div>lorem</div>
</div>
<div>
    <div>Title2</div>
</div>
<div>
    <div>Title3</div>
    <div>dolor</div>
    <div>sit</div>
</div>

标题1
洛勒姆
标题2
标题3
多洛
坐

试着理解php数组是如何工作的

这将解决您的问题并获得您想要的输出:

<?php

$A = array(1,1,2,3,3,3);
$B = array(1,2,NULL,1,2,3);
$C = array('Title1','lorem','Title2','ipsum','Title3','dolor','sit');

?>

<html>
<div>
    <div><?php echo $C[0]?></div>
    <div><?php echo $C[1]?></div>
</div>
<div>
<div><?php echo $C[3]?></div>
</div>
<div>
<div><?php echo $C[4]?></div>
<div><?php echo $C[5]?></div>
<div><?php echo $C[6]?></div>
</div>

</html>

我可能会考虑创建一个空存储阵列,将您的值保存到其中,然后将新阵列内爆:

# Pattern array
$pattern =  [1,1,2,3,3,3];
# Value array
$array   =  ['Title1','lorem','Title2','Title3','dolor','sit'];
# Create a storage array
$new     =   [];
# Loop the pattern array
foreach($pattern as $i => $placement) {
    # Ignore if there is an empty placement
    if(empty($placement))
        continue;
    # See if there is a matching value in the same place as the pattern array
    elseif(isset($array[$i]))
        # Store this value, make sure to keep track of the placement
        $new[$placement][]  =   $array[$i];
}
# Sort the new array so it's in order from low to high
ksort($new);
# Loop this new array and implode the placement arrays
foreach($new as $rows) {
    echo '<div>'.PHP_EOL."\t<div>".implode('</div>'.PHP_EOL."\t<div>", $rows).'</div>'.PHP_EOL.'</div>'.PHP_EOL;
}
您的数组
$B
将产生(理解为
NULL
没有位置,因此被忽略):


标题1
标题3
洛勒姆
多洛
坐
瞧:

$data = [
    ['Title1' => ['lorem']],
    ['Title2' => []],
    ['Title3' => ['dolor', 'sit']]
    ];

foreach ($data AS $value) {
    foreach($value AS $k => $v) {
        echo '<div>' . "\r\n";
        echo '<div>' . $k . '</div>' . "\r\n";
        if (count($v)) {
            foreach($v AS $item) {
                echo '<div>' . $item . '</div>' . "\r\n";
            }
        }
        echo '</div>' . "\r\n";
    }
}
$data=[
['Title1'=>['lorem']],
['Title2'=>[]],
['Title3'=>['dolor','sit']]
];
foreach(数据为$value){
foreach($k=>v的值){
回显“”。“\r\n”;
回显'.$k'.''。“\r\n”;
如果(计算($v)){
foreach($v作为$item){
回显'.$item'.''。“\r\n”;
}
}
回显“”。“\r\n”;
}
}

我不确定我是否正确理解了
$B
中的值的意义,或者您的数据结构的原因,但以下是我的解决方案,我认为您正在努力实现:

// These values define which parent div the text belongs in. 
// (first, second, or third here)
$A = array(1,1,2,3,3,3); 

// These values define which child div the text belongs in.
// The NULL value aligns with the element that is an only child of the second div, 
// it is the only one so no need to say or ask which one and thus the "which" is NULL
$B = array(1,2,NULL,1,2,3);

// These values are obvious
$C = array('Title1','lorem','Title2','Title3','dolor','sit');

$result = render($A,$B,$C);

function render(array $parentIndices, array $childIndices, array $values){
    $sorted = [];

    foreach($values as $i => $text){
        $sorted[$parentIndicies[$i]][$childIndicies[$i]] = "$text";
    }

    $output = "";
    foreach($sorted as $parent){
        $output .= "<div>\n";
        foreach($parent as $child){
            $output .= "\t<div>$child</div>\n";
        }
        $output .= "<\div>\n";
    }

    return $output;
}
只会产生这样的结果

<div>
    <div>second</div>
<div>

第二

您的数组长度不同。。。你是如何阅读它们的(即,你为什么忽略ipsum?)Doh,忽略impsum。我试图简化我的问题,并设法增加了一个太多。他脑子里有不同的逻辑,他不想那么容易地放置数组值。
// These values define which parent div the text belongs in. 
// (first, second, or third here)
$A = array(1,1,2,3,3,3); 

// These values define which child div the text belongs in.
// The NULL value aligns with the element that is an only child of the second div, 
// it is the only one so no need to say or ask which one and thus the "which" is NULL
$B = array(1,2,NULL,1,2,3);

// These values are obvious
$C = array('Title1','lorem','Title2','Title3','dolor','sit');

$result = render($A,$B,$C);

function render(array $parentIndices, array $childIndices, array $values){
    $sorted = [];

    foreach($values as $i => $text){
        $sorted[$parentIndicies[$i]][$childIndicies[$i]] = "$text";
    }

    $output = "";
    foreach($sorted as $parent){
        $output .= "<div>\n";
        foreach($parent as $child){
            $output .= "\t<div>$child</div>\n";
        }
        $output .= "<\div>\n";
    }

    return $output;
}
render([1,1],[1,1],['first','second']);
<div>
    <div>second</div>
<div>