【问题标题】:PHP array save elements in variablePHP数组将元素保存在变量中
【发布时间】:2012-03-29 10:32:01
【问题描述】:

我有一个看起来像这样的数组

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]." ";
    }

}

我想要完成的是从第一个数组中删除 &lt;code&gt; 标签和它们之间的文本,但我得到的是

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. 
)

【问题讨论】:

  • 您是否真的要使用正则表达式从字符串中删除所有出现的&lt;p&gt;...&lt;/p&gt;
  • 不,这就是我想要保留的
  • 也许您应该编辑您的问题以反映您真正想要实现的目标。
  • 谢谢,我想我的问题不够清楚,抱歉我再次编辑。我也想删除代码标签之间的文字

标签: php arrays string element


【解决方案1】:

看起来像 preg_match_all('#&lt;p&gt;(.*)&lt;/p&gt;#sU') 的结果,您正在尝试删除所有 &lt;p&gt; 段落。

这是另一种方法:

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

这是已编辑问题的解决方案。此代码用空字符串替换所有 &lt;code&gt; 标记及其之间的所有内容:

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

【讨论】:

  • 那真的很接近我实际上只想要

    并删除所有 te
  • 这只是删除代码选项卡而不是它们之间的文本对不起我从一开始就不清楚
  • 非常感谢,这真是很棒的东西。感谢您的救命帮助
【解决方案2】:
 $var = "";
 foreach($ext_array as $temp)
     $var .= $temp[1] . ' ';

【讨论】:

    【解决方案3】:
    foreach($your_array as $item){
        echo $item[1]." ";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-22
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      • 2020-04-29
      相关资源
      最近更新 更多