在php中,通过foreach循环从数组中获取值

时间:2021-04-28 21:35:30
<?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();
?>

The result I get from this code is

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

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
    )

)

)

Now I want to show the result according to the index value of array.. means if 0 index contains text than it will show the value of last index in textbox,and if the first index contains the value of image than it shows the value of image on 2nd index.But this loop always show me the last value of the array. e,g

现在我想根据数组的索引值显示结果。意思是如果0索引包含文本,那么它将显示文本框中最后一个索引的值,如果第一个索引包含图像的值,那么它将显示第二个索引中的图像的值。但是这个循环总是显示数组的最后一个值。e,g

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

Kindly some one get me out of this problem.. Thanks in Advanced

请帮我解决这个问题。由于先进的

2 个解决方案

#1


0  

inside your foreach loop after $text_attributes[$key] = explode("\t", $v[$i]); line. try this

$text_attributes[$key] =爆裂(“\t”,$v[$i]);线。试试这个

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

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

#2


0  

I write a code from your result try this hope this will help you.

我从你的结果写了一段代码,试试这个,希望能对你有所帮助。

$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/>';
    }
}

#1


0  

inside your foreach loop after $text_attributes[$key] = explode("\t", $v[$i]); line. try this

$text_attributes[$key] =爆裂(“\t”,$v[$i]);线。试试这个

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

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

#2


0  

I write a code from your result try this hope this will help you.

我从你的结果写了一段代码,试试这个,希望能对你有所帮助。

$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/>';
    }
}