Reputation: 5
I have a small problem that I cannot seem to wrap my head around and hoping someone here might be able to find the problem. I have an variable named $data which is an array of string values and when I use the following code everything works fine.
$data = array(1,2,3,4,5);
$users = implode("," , $data);
echo ($users);
This will produce something like 1,2,3,4,5 which is what I expected, but if I try to follow the same logic on the following piece of code then the result is an empty string.
$data = array(1,2,3,4,5);
$users = implode("," , $data);
$MyArray = array(
$user_ids => array($users)
)
My question then is how do I need to reference the $users variable in this array so it will produce the results I need. (ie 1,2,3,4,5 ...)
Thx, Amy
Upvotes: 0
Views: 92
Reputation: 12860
You don't need to make your data array into a string and convert it back into an array. You could just insert the array into another array and reference it that way. For example:
$data = array(1,2,3,4,5);
$MyArray[] = $data;
To recall this data:
foreach($MyArray as $array){
echo implode(',',$array);
}
Upvotes: 0
Reputation: 60413
Not sure exactly what you want the key to be but right now you are using a variable as a key:
$users = implode("," , $data);
$user_ids = 'users_id_keyname';
$MyArray = array(
$user_ids => $users
);
echo $MyArray['users_id_keyname']; // outputs 1,2,3,4,5
echo $MyArray[$user_ids]; // outputs 1,2,3,4,5
You probably want to just use the string as the key:
$users = implode("," , $data);
$MyArray = array(
'user_ids' => $users
);
echo $MyArray['user_ids']; // outputs 1,2,3,4,5
Upvotes: 2