Please help me with this PHP array issue...
I have a function that outputs values like so: 1,2,5,15,21 etc...
When I use a simple array like this:
<?php
$fruits=array("Apples","Oranges","Bananas");
echo "I like " . $fruits[0] . ", " . $fruits[1] . " and " . $fruits[2] . ".";
?>
Which gives: "I like Apples, Oranges and Bananas."
And I put my function in it as such:
<?php
$my_func = my_function_output();
$fruits=array( $my_func );
echo "I like " . $fruits[0] . ", " . $fruits[1] . " and " . $fruits[2] . ".";
?>
The $my_func gives, say, 1,5,7 - then for $fruits[0] I get "1,5,7" instead of getting just the first "1", and for $fruits[1] and $fruits[2] I get nothing instead of 5 and 7 respectively...
Is there a way of getting each array position to be only one of the values from the function? - I can modify the function's output to a different format if needed.
The function basically fetches WordPress user id's;
function my_function_output($id)
{
global $wpdb;
$count = 0;
$site_user = get_user_name_by_id($id);
$tablename = $wpdb->prefix . 'site_users';
$user_list = array();
while($site_user) {
$site_user = $wpdb->get_var("SELECT user FROM $tablename WHERE site_user = '".$site_user."'");
if($site_user) {
$user_list[] = $site_user;
}
}
return $user_list;
}
Thank you very much!
Your function returns an array and you are assigning it to a variable inside ANOTHER array.
// here $my_func is an array, the return of $my_custom_func
$my_func = $my_custom_func();
// now my fruits is an array with just ONE element which is another array with your ids.
$fruits = array($my_func);
Try assigning the value returned by the function directly do the $fruits var. Like that:
$fruits = $my_custom_func();
or even
$my_func = $my_custom_func();
$fruits = $my_func; // without array()