2

After json_decode I have the following Array :::

$json = Array ( [name] => Array ( [0] => Peter [1] => David ) [dep] => Array ( [0] => accounts [1] => sales ) [date] => Array ( [0] => 10/27/2015 [1] => 09/25/2015 ) );

How can I group the values by key so I get the following in a PHP foreach loop ? :::

<ul>
    <li>Peter, accounts, 10/27/2015</li>
    <li>David, sales, 09/25/2015</li>
</ul>

I've tried a similar foreach loop ( see below ) without the desired results, I'm able to print all the key & value's but I'm not able to group values by key eg. [1] :::

foreach($json as $row){
    foreach($row as $key=>$val){
        echo $key . ': ' . $val . '<br>';
    }
}

Any assistance would be appreciated :::

2 Answers 2

2

You could use the following code to sort the list:

<?php
    $json = [
        "name" => ["Peter","David"],
        "dep" => ["accounts", "sales"],
        "date" => ["10/27/2015","09/25/2015"]
    ];
    $final = [];
    foreach($json as $section)
    {
        foreach($section as $key=>$info)
        {
            if(!isset($final[$key])) $final[$key] = "";
            $final[$key] .= $info . ", ";
        }
    }

    echo "<ul>";
    foreach($final as $row)
    {
        echo "<li>" . substr($row, 0, strlen($row) - 2) . "</li>"; // -2 to remove the extra ", "
    }
    echo "</ul>";
?>

Result:

<ul>
    <li>Peter, accounts, 10/27/2015</li>
    <li>David, sales, 09/25/2015</li>
</ul>
Sign up to request clarification or add additional context in comments.

1 Comment

THX GGG, It works just fine ::: I'll adapt it for my final use :::
1

Perhaps try something like this:

$arrayCount = count($json['name']);

$output = '<ul>';

for($i = 0; $i < $arrayCount; $i++) {
    $output .= '<li>';
    $output .= $json['name'][$i] . ', ';
    $output .= $json['dep'][$i] . ', ';
    $output .= $json['date'][$i];
    $output .= '</li>';
}

$output .= '</ul>';

echo $output;

You would also be better off using an array format like this:

$json = 
Array( 
      Array( 
        'name' => 'Peter',
        'dep' => 'accounts',
        'date' => '10/27/2015'
      ),
      Array( 
        'name' => 'David',
        'dep' => 'accounts',
        'date' => '09/25/2015'
      )
    );

This is because you can create a easier to understand foreach loop like this:

$output = '<ul>';

foreach($json as $row) {
    $output .= '<li>';
    $output .= $row['name'] . ', ';
    $output .= $row['dep'] . ', ';
    $output .= $row['date'];
    $output .= '</li>';
}

$output .= '</ul>';

echo $output;

6 Comments

In my opinion this answer is more readeable and has cleaner code than mine, I suggest OP mark this one as the best answer.
I take that back, your code has syntax errors(using i instead of $i and using , instead of ; on the for loop) and also your for loop counts the elements inside $json, which is 3([name],[dep],[date]) therefore this code doesn't works at all.
Thanks for pointing that out, I've fixed the errors. It will work as required now.
THX for all the suggestions, however I can not change the array structure as suggested above, that's the reason I'm having difficulty extracting key related data :::
If you use the top code snippet on my response, it will print your array as it is.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.