php在数组中找到带有类的元素并打印出来

时间:2022-06-13 17:27:25

How do I find the div with specific class under li element in array and take it if it exists but with childs? I try in_array, array_search but nothing works. I only need those elements that have a class " cf_sel_opt".

如何在数组中的li元素下找到具有特定类的div,如果它存在但是带有子元素,则将其取出?我尝试in_array,array_search但没有任何作用。我只需要那些有“cf_sel_opt”类的元素。

This PHP code

这个PHP代码

<div>
<?php
foreach ($filters_render_array['html'] as $key => $value) {
   if (strpos($key, 'custom_f_') !== false) {
      $selected[$key] = $value;
      print_r($selected[$key]);
   }
}
 ?>
</div>

Returns this HTML

返回此HTML

<div>

    <ul>
        <li>
            <a href="/" class=" cf_sel_opt">
                <span></span>
            </a>
            <input name="custom_f_35[]" value="313038" type="hidden">
        </li>
        <li>
            <a href="/" class=" cf_sel_opt">
                <span></span>
            </a>
            <input name="custom_f_35[]" value="313034" type="hidden">
        </li>
        <li>
            <a href="/" class="">
                <span></span>
            </a>
            <input name="custom_f_35[]" value="313049" type="hidden">
        </li>
    </ul>

    <ul>
        <li>
            <label>
                <input type="checkbox">
                <a href="/">
                    7
                </a>
            </label>
        </li>
        <li>
            <label>
                <input type="checkbox">
                <a href="/">
                    6
                </a>
            </label>
        </li>
        <li>
            <label>
                <input type="checkbox">
                <a href="/">
                    5
                </a>
            </label>
        </li>
        <li>
            <label>
                <input type="checkbox">
                <a href="/">
                    4
                </a>
            </label>
        </li>
        <li>
            <label>
                <input type="checkbox">
                <a href="/">
                    3
                </a>
            </label>
        </li>
        <li>
            <label>
                <input type="checkbox">
                <a href="/">
                    2
                </a>
            </label>
        </li>
        <li>
            <label>
                <input type="checkbox">
                <a href="/">
                    2A
                </a>
            </label>
        </li>
        <li>
            <label class=" cf_sel_opt">
                <input type="checkbox">
                <a href="/">
                    1
                </a>
            </label>
        </li>
        <li>
            <label class=" cf_sel_opt">
                <input type="checkbox">
                <a href="/">
                    00
                </a>
            </label>
        </li>
        <li>
            <label class=" cf_sel_opt">
                <input type="checkbox">
                <a href="/">
                    0
                </a>
            </label>
        </li>
    </ul>

    <ul>
        <li>
            <label class=" cf_sel_opt">
                <input type="checkbox">
                <a href="/">
                    Light/Mesh lycra
                </a>
            </label>
        </li>
    </ul>
</div>

2 个解决方案

#1


2  

Since you are storing strings in the array, you can use a regex or string operation. I assume this will work for you:

由于您在数组中存储字符串,因此可以使用正则表达式或字符串操作。我认为这对你有用:

<?php 
$array = array('<ul>
<li><div class="MY ELEMENT">aaa</div></li>
<li><div class="">bbb</div></li>
<li><div class="">ccc</div></li>
</ul>'); 

$array[] = '<ul>
<li><div class="MY ELEMENT">111</div></li>
<li><div class="">222</div></li>
<li><div class="">333</div></li>
</ul>';

$search_class = "MY ELEMENT";

foreach($array as $e)
{
  $list = explode('<li>', $e);
  foreach($list as $element)
  {
    if(strstr($element,$search_class))
     {
       $results[] = $element;
     }
  }
}

var_dump($results);
?>

Here is the live output https://eval.in/836611 ;)

这是实时输出https://eval.in/836611;)

#2


0  

You should overthink your method imho. Seems like it is a typical DOM-Issue. ( https://en.wikipedia.org/wiki/Document_Object_Model )

你应该过度思考你的方法imho。似乎它是一个典型的DOM问题。 (https://en.wikipedia.org/wiki/Document_Object_Model)

Anyways.. you have an Array and you can get the affected Array-Elements with preg_grep().

无论如何..你有一个数组,你可以使用preg_grep()获得受影响的数组元素。

$results = preg_grep('/class="MY ELEMENT"/is', $input);

If you access this Results with DOM it will be easy to get the desired Scriptparts.

如果您使用DOM访问此结果,将很容易获得所需的Scriptparts。

#1


2  

Since you are storing strings in the array, you can use a regex or string operation. I assume this will work for you:

由于您在数组中存储字符串,因此可以使用正则表达式或字符串操作。我认为这对你有用:

<?php 
$array = array('<ul>
<li><div class="MY ELEMENT">aaa</div></li>
<li><div class="">bbb</div></li>
<li><div class="">ccc</div></li>
</ul>'); 

$array[] = '<ul>
<li><div class="MY ELEMENT">111</div></li>
<li><div class="">222</div></li>
<li><div class="">333</div></li>
</ul>';

$search_class = "MY ELEMENT";

foreach($array as $e)
{
  $list = explode('<li>', $e);
  foreach($list as $element)
  {
    if(strstr($element,$search_class))
     {
       $results[] = $element;
     }
  }
}

var_dump($results);
?>

Here is the live output https://eval.in/836611 ;)

这是实时输出https://eval.in/836611;)

#2


0  

You should overthink your method imho. Seems like it is a typical DOM-Issue. ( https://en.wikipedia.org/wiki/Document_Object_Model )

你应该过度思考你的方法imho。似乎它是一个典型的DOM问题。 (https://en.wikipedia.org/wiki/Document_Object_Model)

Anyways.. you have an Array and you can get the affected Array-Elements with preg_grep().

无论如何..你有一个数组,你可以使用preg_grep()获得受影响的数组元素。

$results = preg_grep('/class="MY ELEMENT"/is', $input);

If you access this Results with DOM it will be easy to get the desired Scriptparts.

如果您使用DOM访问此结果,将很容易获得所需的Scriptparts。