PHP - 从数组

时间:2017-03-08 20:58:04

标签: php arrays

嗨,我有这个庞大的阵列:

array(14) {
  ["imagen-profesor"]=>
  array(8) {
    ["id"]=>
    string(15) "imagen-profesor"
    ["slug"]=>
    string(15) "imagen-profesor"
    ["type"]=>
    string(5) "image"
    ["name"]=>
    string(15) "Imagen profesor"
    ["description"]=>
    string(17) "Foto del profesor"
    ["data"]=>
    array(8) {
      ["slug-pre-save"]=>
      string(15) "imagen-profesor"
      ["placeholder"]=>
      string(0) ""
      ["user_default_value"]=>
      string(0) ""
      ["repetitive"]=>
      string(1) "0"
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
      ["validate"]=>
      array(1) {
        ["url2"]=>
        array(3) {
          ["active"]=>
          string(1) "1"
          ["message"]=>
          string(60) "Please enter a valid URL address pointing to the image file."
          ["suppress_for_cred"]=>
          bool(true)
        }
      }
    }
    ["meta_key"]=>
    string(20) "wpcf-imagen-profesor"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["grados-academicos"]=>
  array(8) {
    ["id"]=>
    string(17) "grados-academicos"
    ["slug"]=>
    string(17) "grados-academicos"
    ["type"]=>
    string(7) "wysiwyg"
    ["name"]=>
    string(18) "Grados académicos"
    ["description"]=>
    string(0) ""
    ["data"]=>
    array(5) {
      ["slug-pre-save"]=>
      string(17) "grados-academicos"
      ["user_default_value"]=>
      string(0) ""
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(22) "wpcf-grados-academicos"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["experiencia-profesional"]=>
  array(8) {
    ["id"]=>
    string(23) "experiencia-profesional"
    ["slug"]=>
    string(23) "experiencia-profesional"
    ["type"]=>
    string(7) "wysiwyg"
    ["name"]=>
    string(23) "Experiencia profesional"
    ["description"]=>
    string(46) "Por favor coloca aquí experiencia profesional"
    ["data"]=>
    array(5) {
      ["slug-pre-save"]=>
      string(23) "experiencia-profesional"
      ["user_default_value"]=>
      string(0) ""
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(28) "wpcf-experiencia-profesional"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["areas-de-interes"]=>
  array(8) {
    ["id"]=>
    string(16) "areas-de-interes"
    ["slug"]=>
    string(16) "areas-de-interes"
    ["type"]=>
    string(7) "wysiwyg"
    ["name"]=>
    string(18) "Áreas de interés"
    ["description"]=>
    string(22) "Intereses del profesor"
    ["data"]=>
    array(5) {
      ["slug-pre-save"]=>
      string(16) "areas-de-interes"
      ["user_default_value"]=>
      string(0) ""
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(21) "wpcf-areas-de-interes"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["cursos-relevantes"]=>
  array(8) {
    ["id"]=>
    string(17) "cursos-relevantes"
    ["slug"]=>
    string(17) "cursos-relevantes"
    ["type"]=>
    string(7) "wysiwyg"
    ["name"]=>
    string(17) "Cursos relevantes"
    ["description"]=>
    string(7) "Cursos "
    ["data"]=>
    array(5) {
      ["slug-pre-save"]=>
      string(17) "cursos-relevantes"
      ["user_default_value"]=>
      string(0) ""
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(22) "wpcf-cursos-relevantes"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["articulos"]=>
  array(8) {
    ["id"]=>
    string(9) "articulos"
    ["slug"]=>
    string(9) "articulos"
    ["type"]=>
    string(7) "wysiwyg"
    ["name"]=>
    string(10) "Artículos"
    ["description"]=>
    string(0) ""
    ["data"]=>
    array(5) {
      ["slug-pre-save"]=>
      string(9) "articulos"
      ["user_default_value"]=>
      string(0) ""
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(14) "wpcf-articulos"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["areas-academicas"]=>
  array(8) {
    ["id"]=>
    string(16) "areas-academicas"
    ["slug"]=>
    string(16) "areas-academicas"
    ["type"]=>
    string(6) "select"
    ["name"]=>
    string(18) "Áreas académicas"
    ["description"]=>
    string(0) ""
    ["data"]=>
    array(6) {
      ["slug-pre-save"]=>
      string(16) "areas-academicas"
      ["options"]=>
      array(12) {
        ["wpcf-fields-select-option-96129381b0cc289dd28e00d98f8c15b0-1"]=>
        array(2) {
          ["title"]=>
          string(23) "Análisis de decisiones"
          ["value"]=>
          string(1) "1"
        }
        ["wpcf-fields-select-option-f320b41329b42d462bd08eb7961f2c65-1"]=>
        array(2) {
          ["title"]=>
          string(17) "Comercialización"
          ["value"]=>
          string(1) "2"
        }
        ["wpcf-fields-select-option-f6b63eb3926b7de2462225998e09cc13-1"]=>
        array(2) {
          ["title"]=>
          string(32) "Control e información directiva"
          ["value"]=>
          string(1) "3"
        }
        ["wpcf-fields-select-option-b2610684e6f824fd687a74d3be7bcabe-1"]=>
        array(2) {
          ["title"]=>
          string(21) "Dirección financiera"
          ["value"]=>
          string(1) "4"
        }
        ["wpcf-fields-select-option-d2c3f7b635105db769c4772a15d5cec2-1"]=>
        array(2) {
          ["title"]=>
          string(25) "Dirección de operaciones"
          ["value"]=>
          string(1) "5"
        }
        ["wpcf-fields-select-option-f14bad56bc815fa8060ae231ccb8a217-1"]=>
        array(2) {
          ["title"]=>
          string(22) "Dirección de personal"
          ["value"]=>
          string(1) "6"
        }
        ["wpcf-fields-select-option-c6114cc9883ef2a4c8d23d9985fd41fc-1"]=>
        array(2) {
          ["title"]=>
          string(15) "Empresa-familia"
          ["value"]=>
          string(1) "7"
        }
        ["wpcf-fields-select-option-6378fcaa75be017d5cddec1f4c837bba-1"]=>
        array(2) {
          ["title"]=>
          string(18) "Entorno económico"
          ["value"]=>
          string(1) "8"
        }
        ["wpcf-fields-select-option-48713712cd807bd0920369faaea72630-1"]=>
        array(2) {
          ["title"]=>
          string(26) "Entorno político y social"
          ["value"]=>
          string(1) "9"
        }
        ["wpcf-fields-select-option-35c50a5e264b2951ccbd9a83f0d52185-1"]=>
        array(2) {
          ["title"]=>
          string(13) "Factor humano"
          ["value"]=>
          string(2) "10"
        }
        ["wpcf-fields-select-option-bdac1a39ab699e74fbffd3b6a9aa85be-1"]=>
        array(2) {
          ["title"]=>
          string(20) "Política de empresa"
          ["value"]=>
          string(2) "11"
        }
        ["default"]=>
        string(10) "no-default"
      }
      ["validate"]=>
      array(1) {
        ["required"]=>
        array(3) {
          ["active"]=>
          string(1) "1"
          ["value"]=>
          string(4) "true"
          ["message"]=>
          string(25) "Por favor, elige el área"
        }
      }
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(21) "wpcf-areas-academicas"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["calendario"]=>
  array(8) {
    ["id"]=>
    string(10) "calendario"
    ["slug"]=>
    string(10) "calendario"
    ["type"]=>
    string(4) "date"
    ["name"]=>
    string(10) "Calendario"
    ["description"]=>
    string(0) ""
    ["data"]=>
    array(7) {
      ["slug-pre-save"]=>
      string(10) "calendario"
      ["placeholder"]=>
      string(0) ""
      ["date_and_time"]=>
      string(4) "date"
      ["repetitive"]=>
      string(1) "0"
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(15) "wpcf-calendario"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["duracion"]=>
  array(8) {
    ["id"]=>
    string(8) "duracion"
    ["slug"]=>
    string(8) "duracion"
    ["type"]=>
    string(8) "textarea"
    ["name"]=>
    string(9) "Duración"
    ["description"]=>
    string(0) ""
    ["data"]=>
    array(7) {
      ["slug-pre-save"]=>
      string(8) "duracion"
      ["placeholder"]=>
      string(0) ""
      ["user_default_value"]=>
      string(0) ""
      ["repetitive"]=>
      string(1) "0"
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(13) "wpcf-duracion"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["fechas"]=>
  array(8) {
    ["id"]=>
    string(6) "fechas"
    ["slug"]=>
    string(6) "fechas"
    ["type"]=>
    string(8) "textarea"
    ["name"]=>
    string(6) "Fechas"
    ["description"]=>
    string(0) ""
    ["data"]=>
    array(7) {
      ["slug-pre-save"]=>
      string(6) "fechas"
      ["placeholder"]=>
      string(0) ""
      ["user_default_value"]=>
      string(0) ""
      ["repetitive"]=>
      string(1) "0"
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(11) "wpcf-fechas"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["contacto"]=>
  array(8) {
    ["id"]=>
    string(8) "contacto"
    ["slug"]=>
    string(8) "contacto"
    ["type"]=>
    string(7) "wysiwyg"
    ["name"]=>
    string(8) "Contacto"
    ["description"]=>
    string(0) ""
    ["data"]=>
    array(5) {
      ["slug-pre-save"]=>
      string(8) "contacto"
      ["user_default_value"]=>
      string(0) ""
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(13) "wpcf-contacto"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["areas-academicas-2"]=>
  array(8) {
    ["id"]=>
    string(18) "areas-academicas-2"
    ["slug"]=>
    string(18) "areas-academicas-2"
    ["type"]=>
    string(6) "select"
    ["name"]=>
    string(24) "Segunda área académica"
    ["description"]=>
    string(0) ""
    ["data"]=>
    array(5) {
      ["slug-pre-save"]=>
      string(18) "areas-academicas-2"
      ["options"]=>
      array(12) {
        ["wpcf-fields-select-option-a84588db3f03c2837345f94adfa18588-1"]=>
        array(2) {
          ["title"]=>
          string(23) "Análisis de decisiones"
          ["value"]=>
          string(1) "1"
        }
        ["wpcf-fields-select-option-5a20ef6e60591caaf5bec848cf43cec0-1"]=>
        array(2) {
          ["title"]=>
          string(17) "Comercialización"
          ["value"]=>
          string(1) "2"
        }
        ["wpcf-fields-select-option-3b00b7d1d1040c63c3512a7e52446443-1"]=>
        array(2) {
          ["title"]=>
          string(32) "Control e información directiva"
          ["value"]=>
          string(1) "3"
        }
        ["wpcf-fields-select-option-da90a64b9b2bf7becfbec27c9c4025da-1"]=>
        array(2) {
          ["title"]=>
          string(21) "Dirección financiera"
          ["value"]=>
          string(1) "4"
        }
        ["wpcf-fields-select-option-65c40a4ea4cde81d6fb7b5bbe1c73282-1"]=>
        array(2) {
          ["title"]=>
          string(25) "Dirección de operaciones"
          ["value"]=>
          string(1) "5"
        }
        ["wpcf-fields-select-option-3b7a56cdb15cef8028daf43082269339-1"]=>
        array(2) {
          ["title"]=>
          string(22) "Dirección de personal"
          ["value"]=>
          string(1) "6"
        }
        ["wpcf-fields-select-option-6eded9521556a27a4cf4465c82f0a203-1"]=>
        array(2) {
          ["title"]=>
          string(15) "Empresa-familia"
          ["value"]=>
          string(1) "7"
        }
        ["wpcf-fields-select-option-5102092609e13386bfadc0ed367591b7-1"]=>
        array(2) {
          ["title"]=>
          string(18) "Entorno económico"
          ["value"]=>
          string(1) "8"
        }
        ["wpcf-fields-select-option-e1aadcfe82cc3389a57288a85ed45ff6-1"]=>
        array(2) {
          ["title"]=>
          string(26) "Entorno político y social"
          ["value"]=>
          string(1) "9"
        }
        ["wpcf-fields-select-option-6440b6d9a60739b88dacaafae2bccaa9-1"]=>
        array(2) {
          ["title"]=>
          string(13) "Factor humano"
          ["value"]=>
          string(2) "10"
        }
        ["wpcf-fields-select-option-2d081231584de301011755851f93b246-1"]=>
        array(2) {
          ["title"]=>
          string(20) "Política de empresa"
          ["value"]=>
          string(2) "11"
        }
        ["default"]=>
        string(10) "no-default"
      }
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(23) "wpcf-areas-academicas-2"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["cargo-profesor"]=>
  array(8) {
    ["id"]=>
    string(14) "cargo-profesor"
    ["slug"]=>
    string(14) "cargo-profesor"
    ["type"]=>
    string(6) "select"
    ["name"]=>
    string(14) "Cargo Profesor"
    ["description"]=>
    string(36) "Selecciona un cargo para el profesor"
    ["data"]=>
    array(6) {
      ["slug-pre-save"]=>
      string(14) "cargo-profesor"
      ["options"]=>
      array(5) {
        ["wpcf-fields-select-option-2a9da69d4d39817ac2a4b687ac49dc27-1"]=>
        array(2) {
          ["title"]=>
          string(18) "Director del área"
          ["value"]=>
          string(1) "4"
        }
        ["wpcf-fields-select-option-b7ae4c59b346a60a6680338c9c44c456-1"]=>
        array(2) {
          ["title"]=>
          string(8) "Profesor"
          ["value"]=>
          string(1) "1"
        }
        ["wpcf-fields-select-option-51a473bf339e2669511accaa90983fb0-1"]=>
        array(2) {
          ["title"]=>
          string(17) "Profesor invitado"
          ["value"]=>
          string(1) "2"
        }
        ["wpcf-fields-select-option-734e5707d3253ded0bf0d972f1c56afe-1"]=>
        array(2) {
          ["title"]=>
          string(6) "Decano"
          ["value"]=>
          string(1) "3"
        }
        ["default"]=>
        string(10) "no-default"
      }
      ["validate"]=>
      array(1) {
        ["required"]=>
        array(3) {
          ["active"]=>
          string(1) "1"
          ["value"]=>
          string(4) "true"
          ["message"]=>
          string(23) "This field is required."
        }
      }
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(19) "wpcf-cargo-profesor"
    ["meta_type"]=>
    string(8) "usermeta"
  }
  ["segundo-cargo"]=>
  array(8) {
    ["id"]=>
    string(13) "segundo-cargo"
    ["slug"]=>
    string(13) "segundo-cargo"
    ["type"]=>
    string(6) "select"
    ["name"]=>
    string(13) "Segundo cargo"
    ["description"]=>
    string(0) ""
    ["data"]=>
    array(5) {
      ["slug-pre-save"]=>
      string(13) "segundo-cargo"
      ["options"]=>
      array(5) {
        ["wpcf-fields-select-option-30ef69a1ba17b84f26edcee1404746f4-1"]=>
        array(2) {
          ["title"]=>
          string(18) "Director del área"
          ["value"]=>
          string(1) "1"
        }
        ["wpcf-fields-select-option-8b8dbd4c42d2e4d61053f442e82a88be-1"]=>
        array(2) {
          ["title"]=>
          string(8) "Profesor"
          ["value"]=>
          string(1) "2"
        }
        ["wpcf-fields-select-option-862f1fbf0e317265cf544b754d6fa3f0-1"]=>
        array(2) {
          ["title"]=>
          string(17) "Profesor invitado"
          ["value"]=>
          string(1) "3"
        }
        ["wpcf-fields-select-option-d49829cdb8f8aaae8af93f30f220ca86-1"]=>
        array(2) {
          ["title"]=>
          string(6) "Decano"
          ["value"]=>
          string(1) "4"
        }
        ["default"]=>
        string(10) "no-default"
      }
      ["custom_use"]=>
      string(0) ""
      ["conditional_display"]=>
      array(0) {
      }
      ["disabled_by_type"]=>
      int(0)
    }
    ["meta_key"]=>
    string(18) "wpcf-segundo-cargo"
    ["meta_type"]=>
    string(8) "usermeta"
  }
}

我只想得到这个价值:Análisisdedecisiones,Comercialización,Controleinformacióndirectiva,Direcciónfinanciera,“Direccióndeoperaciones,Direccióndepersonal,Empresa-familia,Entornoeconómico,Entornotolíticoysocial,Factor humano,Política de empresa。

我的意思是areas-academicas

内的内容

我正在使用嵌套的foreach,但没有:

foreach ($un as $key => $value)
   {

      if ( $key === "areas-academicas" )
      {
        echo "Llave: " . $key . " => ";
        //echo $value;
        foreach ( $value as $key2 => $value2 )
        {
          echo "Llave2: " . $key2 . "<br />" . "valor2: " . $value2;

          foreach ($value2 as $key3 => $value3)
          {
            echo "Llave3: " . $key3 . "<br />" . "valor3: " . $value3;
          }
        }
      }

   }

获得这些价值观的简单方法是什么?感谢。

2 个答案:

答案 0 :(得分:1)

如果我没有弄乱你的筑巢,这应该有效。如果它没有修复foreach内的路径。

$data = array(...); // the array you posted

foreach ($data["areas-academicas"]["data"]["options"] as $area) {
  echo $area["title"];
}

底线是你可以从任何级别开始迭代,你不必从顶部开始。

答案 1 :(得分:1)

看看数组,你似乎只对顶层的两个数组感兴趣:

$un["areas-academicas"]
$un["areas-academicas-2"]

然后,您正在寻找这两个下面的嵌套结构中的特定值,数据选项的标题:

$un["areas-academicas"]['data']['options'][....]['title']

您可以通过使用这些数组的统一结构来限制循环的迭代。但是我假设您希望在包含“areas-academicas”的键的顶层数组中循环:

$titles = array();
foreach ($un as $key => $value){
    if(strpos($key, 'areas-academicas') == false){ continue; }
    foreach($value['data']['options'] as $val){
        $titles[] = $val['title'];
    }
}

这个循环应该收集来自area-academicas数组的所有选项标题,但是依赖于嵌套结构对于顶层数组中的每个区域实例都是统一的。希望这有帮助