Create nested associative array from foreach values

Aivaras

I'm trying to create associative nested array from foreach values, but not sure how to get it in desired format, as right now associative array is getting wrapped with numeric one.

I understand it's because shouldn't use array() to wrap values, but not sure how to do it right.

$arr=array();

foreach ($all_users as $val) {
   $arr[] = array( $val->data->user_nicename => array(
    'username'=> $val->data->display_name,
    'avatar_url' => get_avatar_url($val->ID)
    )
    );
}

print_f($arr);

Getting array result like this:

Array
(
   [0] => Array
       (
           [john_s] => Array
               (
                [username] => John Smith
                [avatar_url] => https://secure.gravatar.com
            )

    )

[1] => Array
    (
        [sarah_s] => Array
            (
                [username] => Sarah Smith
                [avatar_url] => https://secure.gravatar.com
            )

    )
)

While desired format is this:

Array
(
    [john_s] => Array
        (
            [username] => John Smith
            [avatar_url] => https://secure.gravatar.com
        )
    [sarah_s] => Array
        (
            [username] => Sarah Smith
            [avatar_url] => https://secure.gravatar.com
        )
)
MonkeyZeus

You are nesting one level too deep:

<?php
$arr=array();

foreach ($all_users as $val) {

    // Use $val->data->user_nicename as the index to build an associative array of the other data
    // This assumes that user_nicename is unique throughout the loop
    // If you have multiple users with the same user_nicename then some data can get "lost"
    $arr[$val->data->user_nicename] = array(
        'username'=> $val->data->display_name,
        'avatar_url' => get_avatar_url($val->ID)
    );
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Lodash forEach Associative Array

分類Dev

How to create php associative array from messy data

分類Dev

Display values from one associative array whose key exists as the value in another array

分類Dev

Create associative list with variables as values in common lisp

分類Dev

adding values in a 2d associative array

分類Dev

Display Associative Array with Empty Values in table Format?

分類Dev

How to create a comma-delimited string from associative array's key-value pairs

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

How to create instances from Array values

分類Dev

Create array with values that match from database

分類Dev

Check nested Array values, if a match: combine data from both

分類Dev

How to use reduce to retrieve values from deep nested array objects

分類Dev

Non-associative nested 2 level array PHP

分類Dev

Create nested hashes from an array of elements for JSON encoding

分類Dev

Kotlin return@forEach from nested forEach

分類Dev

Is there another way to create a JavaScript associative Array without JSON?

分類Dev

How to take a value from nested array(json format) without using multiple foreach in php

分類Dev

How to build a multidimensional array tree from an associative array in PHP?

分類Dev

How to create a html table from the values of mysql array

分類Dev

Getting one row from a nested array, push values into input then setTimeout to change to next array?

分類Dev

Efficient way to create new column based on nested if else conditions and comparing values from lists in R

分類Dev

How to get the first item from an associative PHP array?

分類Dev

Unzip list of files from bash associative array keys

分類Dev

How to build a multilevel menu from associative array.

分類Dev

associative php array from html data attributes and texts

分類Dev

Display 2 Array values in foreach() loop - PHP

分類Dev

How to efficiently create nested loops and append the output (as a multi-dimensional array) from all the loops?

分類Dev

Create an array using forEach that has all the usernames

分類Dev

Retrieve results from nested array

Related 関連記事

  1. 1

    Lodash forEach Associative Array

  2. 2

    How to create php associative array from messy data

  3. 3

    Display values from one associative array whose key exists as the value in another array

  4. 4

    Create associative list with variables as values in common lisp

  5. 5

    adding values in a 2d associative array

  6. 6

    Display Associative Array with Empty Values in table Format?

  7. 7

    How to create a comma-delimited string from associative array's key-value pairs

  8. 8

    How to get the combination of array values from nested arrays in an array of objects

  9. 9

    How to create instances from Array values

  10. 10

    Create array with values that match from database

  11. 11

    Check nested Array values, if a match: combine data from both

  12. 12

    How to use reduce to retrieve values from deep nested array objects

  13. 13

    Non-associative nested 2 level array PHP

  14. 14

    Create nested hashes from an array of elements for JSON encoding

  15. 15

    Kotlin return@forEach from nested forEach

  16. 16

    Is there another way to create a JavaScript associative Array without JSON?

  17. 17

    How to take a value from nested array(json format) without using multiple foreach in php

  18. 18

    How to build a multidimensional array tree from an associative array in PHP?

  19. 19

    How to create a html table from the values of mysql array

  20. 20

    Getting one row from a nested array, push values into input then setTimeout to change to next array?

  21. 21

    Efficient way to create new column based on nested if else conditions and comparing values from lists in R

  22. 22

    How to get the first item from an associative PHP array?

  23. 23

    Unzip list of files from bash associative array keys

  24. 24

    How to build a multilevel menu from associative array.

  25. 25

    associative php array from html data attributes and texts

  26. 26

    Display 2 Array values in foreach() loop - PHP

  27. 27

    How to efficiently create nested loops and append the output (as a multi-dimensional array) from all the loops?

  28. 28

    Create an array using forEach that has all the usernames

  29. 29

    Retrieve results from nested array

ホットタグ

アーカイブ