0

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

0

2 Answers 2

2

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
14
  • Sorry, I don't understand what you mean by Key and all that but I tried your 2nd example and I received a parse error saying: Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in C:\wamp\www\forum\practicepm.php on line 58
    – Amy
    Aug 14, 2011 at 6:42
  • the you have an error somewhere thats not directly part of the code i gave you. You do need to understand what a key is when dealing with arrays, though... its kind of essential. Aug 14, 2011 at 6:47
  • I updated the code in the original post. I forgot to add the word array in front of ($users) so the old code just said user_ids => ($users) but it now says user_ids => array($users) that may clear up some of the confusion sorry about that.
    – Amy
    Aug 14, 2011 at 6:53
  • @Amy: if you do it that you will actually have the following if you were to var_dump $MyArray: Array('user_ids' => Array( 0 => "1,2,3,4,5")) is that what you actually want? Aug 14, 2011 at 6:57
  • As long as it works I don't care but all I know is I need the word array in front of the ($users) part or I get tons of error messages about missing this and that.
    – Amy
    Aug 14, 2011 at 7:04
0

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);
}
3
  • I need the word array infront of the word users because if I omit it then I get alot of errors.
    – Amy
    Aug 14, 2011 at 6:55
  • I rewrote my answer, and tested this, and it works. Hope it works for you. Any questions, let me know.
    – bozdoz
    Aug 14, 2011 at 7:05
  • You all decide who wins the pot, this is getting to confusing to me. I already wasted 2 days on this I think I will let someone else work on this project who knows what they are doing.
    – Amy
    Aug 14, 2011 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.