Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda