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?

7
  • use there id as the array key, its usually helpful Commented Apr 23, 2015 at 0:44
  • don't need array push at all Commented Apr 23, 2015 at 0: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 :)?) Commented Apr 23, 2015 at 0:46
  • 1
    $user_display[$user->ID] = $name; @Rizier123 cloudy with a chance of a zombie apocalypse Commented Apr 23, 2015 at 0:46
  • 2
    over used by people who dont know how arrays work Commented Apr 23, 2015 at 0:52

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
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 Probably a different question but how would this be sorted by name in ascending order?
@user1048676 - added some examples for you
@scrowler Your second example was perfect. Thank you and Dagon for the help on this.
|
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).

7 Comments

or jsut $user_display[$user->ID] = $name;
Yes that is what I would do too, but the OP specifically asked for the output he wanted.
@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 whats more arbitrary? an id associated with the user, or something php picks incrementally
@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.
|
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.

2 Comments

From a logic stand point, this makes the most sense, as the element is more representative of an object, than an array.
@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.

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.