Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!