在php中通过foreach循环从数组中获取值的问题

时间:2015-08-25 12:06:38

标签: php arrays foreach associative-array

c.corridor_plan_id

我从这段代码得到的结果是

<?php
$v = array();
$text_attributes = array();
$i = 0;
foreach($input_text as $key => $value):
    $i++;
    $v[$i]=$value;
    $text_attributes[$key] = explode("\t", $v[$i]);
endforeach;
echo "<pre>";
print_r($text_attributes);
die();
?>

现在我想根据数组的索引值显示结果。 意味着如果0索引包含文本而不是它将显示文本框中最后一个索引的值,并且如果第一个索引包含图像的值,则它显示第二个索引上的图像值。但是这个循环总是显示我的最后一个值阵列。 E,G

Array
(
    [0] => Array
        (
            [0] => text
            [1] => Composition
            [2] => 1text1
            [3] => Test saf af saf
        )

[1] => Array
    (
        [0] => image
        [1] => c://xampp/htdocs/clickcue/application/app/webroot/files/ae_templates/rendered_templates/192/710/
        [2] => i1.jpg
        [3] => Chrysanthemum - Copy - Copy.jpg
    )

[2] => Array
    (
        [0] => text
        [1] => Composition
        [2] => 2text1
        [3] => Test blkjb
    )

[3] => Array
    (
        [0] => text
        [1] => Composition
        [2] => 2text2
        [3] => Test  jb
    )

[4] => Array
    (
        [0] => text
        [1] => Composition
        [2] => 2text3
        [3] => Test kjb
    )

[5] => Array
    (
        [0] => image
        [1] => c://xampp/htdocs/clickcue/application/app/webroot/files/ae_templates/rendered_templates/192/710/
        [2] => i2.jpg
        [3] => Desert.jpg
    )

[6] => Array
    (
        [0] => text
        [1] => image1
        [2] => i1text1
        [3] => Test lkjb
    )

[7] => Array
    (
        [0] => text
        [1] => image1
        [2] => i1text2
        [3] => Test k
    )

[8] => Array
    (
        [0] => text
        [1] => image1
        [2] => i1text3
        [3] => Test b
    )

[9] => Array
    (
        [0] => image
        [1] => c://xampp/htdocs/clickcue/application/app/webroot/files/ae_templates/rendered_templates/192/710/
        [2] => i3.jpg
        [3] => Hydrangeas - Copy - Copy.jpg
    )

[10] => Array
    (
        [0] => text
        [1] => image2
        [2] => i2text1
        [3] => Test kj
    )

[11] => Array
    (
        [0] => text
        [1] => image2
        [2] => i2text2
        [3] => Test b
    )

[12] => Array
    (
        [0] => text
        [1] => image2
        [2] => i2text3
        [3] => Test kjb
    )

[13] => Array
    (
        [0] => image
        [1] => c://xampp/htdocs/clickcue/application/app/webroot/files/ae_templates/rendered_templates/192/710/
        [2] => i4.jpg
        [3] => Jellyfish - Copy (2).jpg
    )

[14] => Array
    (
        [0] => text
        [1] => image3
        [2] => i3text1
        [3] => Test b
    )

[15] => Array
    (
        [0] => text
        [1] => image3
        [2] => i3text2
        [3] => Test kljb
    )

[16] => Array
    (
        [0] => text
        [1] => image3
        [2] => i3text3
        [3] => Test kj
    )

[17] => Array
    (
        [0] => text
        [1] => Composition
        [2] => 4text1
        [3] => Test b
    )

[18] => Array
    (
        [0] => text
        [1] => Composition
        [2] => 4text2
        [3] => Test kj
    )

[19] => Array
    (
        [0] => text
        [1] => Composition
        [2] => 5text1
        [3] => Test b
    )

[20] => Array
    (
        [0] => text
        [1] => Composition
        [2] => 5text2
        [3] => Test kjb
    )

[21] => Array
    (
        [0] => text
        [1] => Composition
        [2] => 5text3
        [3] => Test kj
    )

请有人帮我解决这个问题.. 谢谢高级

2 个答案:

答案 0 :(得分:0)

在$ text_attributes [$ key] = explode(&#34; \ t&#34;,$ v [$ i])之后,在你的foreach循环中

;线。试试这个

if($text_attributes[$key][0]=='images')
{
    echo $text_attributes[$key][3];

}
if($text_attributes[$key][0]=='text')
{
    echo $text_attributes[$key][2];
}

答案 1 :(得分:0)

我从你的结果中编写了一个代码,希望这对你有帮助。

$textAttributes = array(0 => array(0 => 'text',
        1 => 'Composition',
        2 => '1text1',
        3 => 'Test saf af saf'), 1 => array(0 => 'image',
        1 => 'c://xampp/htdocs/clickcue/application/app/webroot/files/ae_templates/rendered_templates/192/710/',
        2 => 'i1.jpg',
        3 => 'Chrysanthemum - Copy - Copy.jpg'));

foreach ($textAttributes as $textAttribute) {
    if ($textAttribute[0] == 'text') {
        echo end($textAttribute) . '<br/>';
    } else if ($textAttribute[0] == 'image') {
        echo $textAttribute[2] . '<br/>';
    }
}