简体   繁体   English

将php数组值循环到html数据属性

[英]looping php array values to html data attribute

I have an array $myArray 我有一个数组$ myArray

Array ( 

[0] => Array (
        [Number] => 039
        [Fruit] => Apple
        [Level] => Low
        [Names] =>
            Array (
                [6] => Name 1
                [7] => Name 2
                [19] => Name 3
                [25] => Name 4
        )
        )
[0] => Array (
        [Number] => 007
        [Fruit] => Plum
        [Level] => High
        [Names] =>
            Array (
                [3] => Name 1
                [5] => Name 2
                [9] => Name 3
                [11] => Name 4
                [12] => Name 5
                [19] => Name 6
                [22] => Name 7
                [30] => Name 8
        )
        )
 [0] => Array (
        [Number] => 486
        [Fruit] => Grape
        [Level] => Medium
        [Names] =>
            Array (
                [6] => Name 1
                [15] => Name 2

        )
        )

I want to loop though this array for all the set of names, and then i will display them later in html though javascript 我想为所有名称集循环通过此数组,然后稍后通过javascript在html中显示它们

for($index=0; $index < count($myArray); $index++){


    $name = array_values(array_filter($myarray[$index]["Name"]));


    <button type="button" class="open-my-modal btn btn-primary" 
        data-names="'.$name.'"

The output of Names is as below 名称的输出如下

Array ( 
   [0] => Name 1
   [1] => Name 2
   [2] => Name 3
   [3] => Name 4
  )
Array ( 
   [0] => Name 1
   [1] => Name 2
   [2] => Name 3
   [3] => Name 4
   [4] => Name 5
   [5] => Name 6
   [6] => Name 7
   [7] => Name 8
   [8] => Name 9
  )
Array ( 
   [0] => Name 1
   [1] => Name 2
)

Later in my code i want to loop though each set of numbers and print them to the screen. 稍后在我的代码中,我想遍历每组数字并将其打印到屏幕上。 For example loop though the first array and print names 1-4. 例如,循环通过第一个数组并打印名称1-4。

I am getting an error of 我收到一个错误

Notice: Array to string conversion

on data-names="'.$name.'" 在data-names =“'。$ name。'”上

How can i pass the array of Names so i can later call them in html. 我如何传递名称数组,以便以后可以在html中调用它们。

Also: i am writing this in a huge php function so, php is written straightforward and html is in quotes. 另外:我在一个巨大的php函数中编写此代码,因此php编写简单明了,而html用引号引起来。 any content that is displayed on the screen is appended to a return variable. 屏幕上显示的所有内容都将附加到返回变量。

The error you get says that you are trying to concat array like you do with string. 您得到的错误表明您正在尝试像处理字符串一样连接数组。 You can't do it. 你做不到

you can use implode to join array to a string with deilimeter (for java script use) 您可以使用implode将数组连接到具有Deilimeter的字符串(供Java脚本使用)

echo '<button data-number="'.implode(',', $numbers).'">Button</button>'

It will results HTML like this: 它将产生如下所示的HTML:

<button data-number="1,2,3,4">Button</button>

Than you can split it with java script and loop over the numbers 比起您可以用Java脚本split它并遍历数字

implode Documentation 爆破文档

*EDIT If your array contain four arrays you will need another loop to merge the array to one array * EDIT如果您的数组包含四个数组,则需要另一个循环才能将该数组合并为一个数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM