duanjia4817 2019-05-20 15:05
浏览 72
已采纳

如何用2个数组创建一个对象数组?

i'm trying to create an array of objects but its kinda hard for me to do it. Can you guys help me?

THis is what i have right now:

              $id_mercado = strip_tags($obj->idMercado);


                $sql    = "SELECT * FROM mercados WHERE id = $id_mercado";
                $stmt   = $pdo->prepare($sql);
                $stmt->execute();

               $row  = $stmt->fetch(PDO::FETCH_OBJ);
               $siglas = explode("
", $row->dicionario_familias_sigla);
               $significados = explode("
", $row->dicionario_familias_significado);

               class Dicionario{
                public $siglas;
                public $significados;
                }

                $listaFiltros = new Dicionario(); 
                        $listaFiltros->siglas = $siglas;
                        $listaFiltros->significado = $significados; 

So siglas is

["EX", "RO", "DI", "OR", "VE"]

Significado is

["Exoticos","Rosas","Diversos","Orquideas","Verdes"]

the output i want is:

0: {sigla: "EX", significado: "Exoticos"} 
1: {sigla: "RO", significado: "Rosas"} 
2: {sigla: "DI", significado: "Diversos"} 
3: {sigla: "OR", significado: "Orquedeas"}
4: {sigla: "VE", significado: "Verdes"}

UPDATE

for($i = 0; count($significados)> $i; $i++){
                $listaFiltros[] = (object) array('sigla' => $siglas[$i], 
                                                 'significado' => $significados[$i]);
 }
  • 写回答

1条回答 默认 最新

  • doudinghan8319 2019-05-20 15:13
    关注

    I don't use PDO, but you can use the fields returned from your SELECT statement, populate the objects, and then push them to the array you want like this...

    $myArr = []; 
    
    $object = (object)[
        "sigla": $row->dicionario_familias_sigla,
        "significado": $row->dicionario_familias_significado
    ];
    
    array_push($myArr, $object);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里