2012-03-29 68 views
1

元件我有一個陣列,其看起來像PHP數組節省在可變

Array 
(
    [0] => Array 
     (
      [0] => <p>How can i get the firstName values from this array? its easy with 
    print_r, <strong>but I want individual values</strong></p> 
      [1] => How can i get the firstName values from this array? its easy with 
    print_r, <strong>but I want individual values</strong> 
     ) 

    [1] => Array 
     (
      [0] => <p>You can do:</p> 
      [1] => You can do: 
     ) 

    [2] => Array 
     (
      [0] => <p>Since your array contains objects eg <code>stdClass</code>, you need to use <code>-&gt;</code> like shown above.</p> 
      [1] => Since your array contains objects eg <code>stdClass</code>, you need to use <code>-&gt;</code> like shown above. 
     ) 

) 

如何可以保存在一個字符串變量的每個元素的[1]的值。我想要得到的是

How can i get the firstName values from this array? its easy with 
    print_r, <strong>but I want individual values</strong> You can do: Since your array contains objects eg <code>stdClass</code>, you need to use <code>-&gt;</code> like shown above. 

更新:嗯數組我在剛開始是:

Array 
(
    [0] => <p>try this</p> 

<pre><code>foreach($x as $val) 
{ 
echo $val-&gt;firstName; 
} 
</code></pre> 

    [1] => <p>Since you have an array of objects, you can either access each object by the array index or loop through the array to get each seperate object.</p> 

<p>Once you have the object it self, you can simply access the first name property of the object.</p> 

<p>Example of looping:</p> 

<pre><code>foreach ($array as $object) { 
echo $object-&gt;firstname; 
} 
</code></pre> 

<p>Where $array is the variable containing your array.</p> 

<p>Example of accessing via array index:</p> 

<pre><code>echo $array[0]-&gt;firstname; 
</code></pre> 

<p>OR </p> 

<pre><code>$obj = $array[0]; 
echo $obj-&gt;firstname; 
</code></pre> 

    [2] => <p>Try this (assume <code>$a</code> is your array):</p> 

<pre><code>echo $a[0]-&gt;firstname; 
</code></pre> 

    [3] => <blockquote> 
    <p>How can i get the firstName values from this array? its easy with 
    print_r, <strong>but I want individual values</strong></p> 
</blockquote> 

<p>You can do:</p> 

<pre><code>foreach($yourArray as $val){ 
    echo $val-&gt;firstName; 
} 
</code></pre> 

<p>Since your array contains objects eg <code>stdClass</code>, you need to use <code>-&gt;</code> like shown above.</p> 

) 

,它的answerstack命名然後我用:

for($j=0;$j<$answerscnt;$j++){ 

    preg_match_all("#<p>(.*?)</p>#is",$answerstack[$j],$matches,PREG_SET_ORDER); 
    foreach($matches as $item){ 
     $answerstack[$j]=$item[1]." "; 
    } 

} 

我想完成的是刪除<code>個標籤和從第一陣列它們之間的文本,但我得到的是

Array 
(
    [0] => try this 
    [1] => OR 
    [2] => Try this (assume <code>$a</code> is your array): 
    [3] => Since your array contains objects eg <code>stdClass</code>, you need to use <code>-&gt;</code> like shown above. 
) 
+1

你實際上是試圖從正則表達式的字符串中刪除的'

...

'所有出現? – Kaii 2012-03-29 10:46:22

+0

沒有這就是我想保持 – 2012-03-29 10:52:16

+1

也許你應該編輯你的問題,以反映你真正想要實現的。 – Kaii 2012-03-29 10:53:31

回答

1

看起來像一個preg_match_all('#<p>(.*)</p>#sU')的結果,您要刪除所有<p>段落。

下面是另一種方法:

$text = preg_replace('#<p>(.*)</p>#isU', '\1 ', $text); 

下面是所編輯的問題的解決方案。此代碼替換所有<code>標籤之間的一切與一個空字符串:

// iterate over each element in $answerstack 
foreach ($answerstack as &$text){ 
    // remove all '<code>' from $answerstack 
    $text = preg_replace('#<code>(.*)</code>#isU', '', $text); 
} 
+0

這是非常接近我其實只想要

並刪除所有TE 2012-03-29 10:46:14

+0

這只是刪除了代碼標籤不是他們之間的文本對不起,我是不是從一開始就明確 – 2012-03-29 11:02:14

+1

@ MpampinosHolmens再次編輯..現在快樂嗎? ;) – Kaii 2012-03-29 11:06:10

0
foreach($your_array as $item){ 
    echo $item[1]." "; 
} 
1
$var = ""; 
foreach($ext_array as $temp) 
    $var .= $temp[1] . ' ';