0

I'm trying to create an array dynamically with strings. Here is the code that I'm trying to use:

$users = get_users( $args_users );
$user_display = array(); 
$user_display_sorted = array();
foreach($users as $user){
    echo $user->ID;
    $first_name = get_user_meta($user->ID, 'first_name', true);
    $last_name = get_user_meta($user->ID, 'last_name', true);
    $name = $first_name.' '.$last_name;
    array_push($user_display, $user->ID);
    array_push($user_display, $name);
    $user_display_sorted = asort($user_display);
}
print_r($user_display);

When I print the array I get the following:
Array ( [0] => 5 [1] => Test Person[2] => 6 [3] => Adam Person )

I would like the output to be something like this:
Array ( [0] [ID] => 5 [name] => Test Person; [1] [ID] => 6 [name] => Adam Person)

So I'd like to do a foreach loop on the user_display_sorted array and access the variables like this:

foreach($user_display_sorted as $user_display_sort){
    echo $user_display_sort->ID;
}

How can I do this?

user1048676
  • 9,756
  • 26
  • 83
  • 120
  • use there id as the array key, its usually helpful –  Apr 23 '15 at 00:44
  • don't need array push at all –  Apr 23 '15 at 00:44
  • @Dagon I don't love `array_push()` since you can't define a key, don't understand why people are using it. (What's the future doing in New Zealand? Will the weather be good :)?) – Rizier123 Apr 23 '15 at 00:46
  • 1
    `$user_display[$user->ID] = $name;` @Rizier123 cloudy with a chance of a zombie apocalypse –  Apr 23 '15 at 00:46
  • Note that if you use the other solutions, you will have to access the data like $user_display_sort[ID] instead of $user_display_sort->ID – RisingSun Apr 23 '15 at 00:46
  • @Rizier123 array_push is useful when all you wanna do is add items to an array and you don't care about indexes. Plus, you can add multiple variable into a single array in one call. – RisingSun Apr 23 '15 at 00:49
  • 2
    over used by people who dont know how arrays work –  Apr 23 '15 at 00:52

3 Answers3

6

You just need to push an array of the data you want to append instead of the values separately:

foreach($users as $user){
    echo $user->ID;
    $first_name = get_user_meta($user->ID, 'first_name', true);
    $last_name = get_user_meta($user->ID, 'last_name', true);
    $name = $first_name.' '.$last_name;
    array_push($user_display, array('ID' => $user->ID, 'name' => $name));
    $user_display_sorted = asort($user_display);
}

Following on from your comment asking how to sort - since this is an multidimensional array, you could sort it using array_multisort() like this:

array_multisort($example, SORT_ASC, SORT_NATURAL, array_map(
    function($row) {
        return $row['name'];
    },
    $example
));

However, as Dagon has suggested in multiple places, if your objective is to sort by the value then using the ID as the array key would be much easier overall, i.e. from the start to end:

foreach ($users as $user) {
    // ...
    $user_display[$user->ID] = $name;
}

// sort, maintaining keys
asort($user_display);

// output again if you want to
foreach ($user_display as $id => $name) {
    echo 'User ID ' . $id . ' has name ' . $name, PHP_EOL;
}

Demo here.

For sorting references, the PHP manual has a great table explaining which function to suit.

scrowler
  • 24,273
  • 9
  • 60
  • 92
  • 1
    but why push at all? and why not use the user key as the array key making lookups simple? and why shouldn't i pull the girls pony tail? –  Apr 23 '15 at 00:50
  • I'm on your side regarding `array_push()` - I hate it. But providing an answer like this allows the OP to see the difference in data structure between what he started with and what he wants to end up with, without adding in changing `array_push()` to a simple `$user_display[]...` – scrowler Apr 23 '15 at 00:55
  • @scrowler Probably a different question but how would this be sorted by name in ascending order? – user1048676 Apr 23 '15 at 00:58
  • @user1048676 - added some examples for you – scrowler Apr 23 '15 at 01:08
  • 1
    @scrowler Your second example was perfect. Thank you and Dagon for the help on this. – user1048676 Apr 23 '15 at 01:11
  • 5 lines to sort vs 1, is that better ? –  Apr 23 '15 at 01:14
1

For the output you want to have, try the following:

$user_display[] = array('ID' => $user->ID, 'name' => $name);

instead of the two array_push lines. As for the sorted display, you may want to use usort and pass a callable function that compares the names (or the IDs, depends on what you want to do).

Wouter Thielen
  • 1,016
  • 9
  • 21
  • or jsut `$user_display[$user->ID] = $name;` –  Apr 23 '15 at 00:46
  • Yes that is what I would do too, but the OP specifically asked for the output he wanted. – Wouter Thielen Apr 23 '15 at 00:48
  • @Dagon Setting arbitrary keys is not **always** desirable. It can be handy to use `$user_display[0]` to get just the first item, for instance. – IMSoP Apr 23 '15 at 00:56
  • @IMSoP whats more **arbitrary**? an id associated with the user, or something php picks incrementally –  Apr 23 '15 at 00:57
  • @IMSoP agree with you, however `$user_display[0]` can be considered to be bad practice for selecting the first value of an array for this very reason - [there are better ways](http://stackoverflow.com/questions/1921421/get-the-first-element-of-an-array) – scrowler Apr 23 '15 at 01:21
  • `reset()` and `current()` is a good way to get the first element of the array. `array_shift` also takes the first element OUT of the array, which might not be what you want. And yes, some OPs don't know what is good for them, indeed, which is why it's good to suggest alternative ways, like @Dagon did. – Wouter Thielen Apr 23 '15 at 01:30
  • 1
    @scrowler Not sure I agree with that. If you know your array is a plain list, [0] is much easier and more efficient than any of those. But we're way off topic, and I do agree that considering non-consecutive indexes is a useful suggestion here. – IMSoP Apr 23 '15 at 07:51
0

You are almost there.

$userVar = new stdClass();
$userVar->ID = $user->ID;
$userVar->name = $name;
array_push($user_dispaly, $userVar);

If you replace the double array pushes with the above, you can access the items the way you want. All I'm doing is creating an object so you can access the data with an arrow and then adding that object to an array.

RisingSun
  • 1,693
  • 27
  • 45
  • From a logic stand point, this makes the most sense, as the element is more representative of an object, than an array. – Ohgodwhy Apr 23 '15 at 00:46
  • 1
    @Ohgodwhy Eh, I'm not a fan of stdClass objects. They do nothing an array can't, and can't do things an array can. If you want to use an object, define a class; if you want an ad hoc hash, an array works fine. – IMSoP Apr 23 '15 at 00:51