Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
PHP Get Array String Pointing to Another Array Object

I've got an array which points to a bunch of other arrays, structured like a JSON object. This is the structure of it:

$allRows= array(
    "Global task 1"=> array(
        "major-task"=> array(
            "points"=> 850,
        ),
        "minor-task"=> array(
            array(
                "points"=> 400,
                "task"=> "Minor task 1",
            ),
            array(
                "points"=> 200,
                "task"=> "Minor task 2",
            ),
            array(
                "points"=> 250,
                "task"=> "Minor task 3",
            ),
        )
    ),
    "Global task 2"=> array(
        "major-task"=> array(
            "points"=> 850,
        ),
        "minor-task"=> array(
            array(
                "points"=> 400,
                "task"=> "Minor task 1",
            ),
            array(
                "points"=> 200,
                "task"=> "Minor task 2",
            ),
            array(
                "points"=> 250,
                "task"=> "Minor task 3",
            ),
        )
    ),
);

I'm trying to be able to access each of these in the following way:

foreach ($allRows as $row) {
    // Print Global task 1, Global task 2, etc
    echo $row[0]; // unsure how to acquire this 'Global task x' text

    // I've already accessed this just fine
    echo $row["major-task"]["points"];
}

I've got the latter echo just fine, but I'm unable to print the Global task 1 and Global task 2. Any help printing this text would be highly appreciated!

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

The tasks are the keys for $allRows, you'll need to catch them in the foreach:

foreach ($allRows as $taskName => $row) {
    echo $taskName; // "Global task 1"

    echo $row["major-task"]["points"];
}
over 4 years ago · Santiago Trujillo Denunciar

0

Use it like this:

foreach ($allRows as $key => $row) {
    // Print Global task 1, Global task 2, etc
    echo $key;

    // I've already accessed this just fine
    echo $row["major-task"]["points"];
}

http://php.net/manual/en/control-structures.foreach.php

over 4 years ago · Santiago Trujillo Denunciar

0

What you are trying to do is get the key for the multidimensional array

To do this, define the a $key variable with the foreach() loop

Example:

foreach ($allRows as $key => $value) {
    echo $key; // Global task 1 or Global task 2
    echo $value["major-task"]["points"];
}

Also, does not matter, but when using strings in arrays, I like to use ''. Example:

echo $value['major-task']['points'];
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda