![]() |
Having trouble imploding an array. - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Having trouble imploding an array. (/showthread.php?tid=24547) |
Having trouble imploding an array. - El Forum - 11-12-2009 [eluser]Samuurai[/eluser] Hi everyone, I think i'm going blonde.... I have this array: array(3) { [0]=> array(1) { ["date"]=> string(10) "2009-11-12" } [1]=> array(1) { ["date"]=> string(10) "2009-11-13" } [2]=> array(1) { ["date"]=> string(10) "2009-11-14" } } How can I implode it so that the strings are seperated by ","'s Code: implode(",",$myarray); Array,Array,Array and it whinges about an Array to string conversion. I think the problem lies in the fact that this is three arrays within one, but how do I easily bring those arrays to the surface, as it were? I'm using result_array(). Cheers! Beren Having trouble imploding an array. - El Forum - 11-12-2009 [eluser]jedd[/eluser] The phrase 'bring to the surface' means different things to different people. So in order for us to answer this: Quote:I think the problem lies in the fact that this is three arrays within one, but how do I easily bring those arrays to the surface, as it were? - you need to describe what you want this: Quote:array(3) { [0]=> array(1) { ["date"]=> string(10) "2009-11-12" } [1]=> array(1) { ["date"]=> string(10) "2009-11-13" } [2]=> array(1) { ["date"]=> string(10) "2009-11-14" } } ... to end up looking like. The act of describing what the output is that you're after will probably help you resolve the problem by yourself, anyway. Having trouble imploding an array. - El Forum - 11-12-2009 [eluser]Samuurai[/eluser] Hi Jedd - thanks for the reply! I want to be able to access the ['date'] value from the arrays contained in the array. I think this would get me what I want. Code: $result = "the array I pasted above"; Is this what you call a multidimensional array? BTW, what tags did you use to get the non-code boxes? Having trouble imploding an array. - El Forum - 11-12-2009 [eluser]jedd[/eluser] [quote author="Samuurai" date="1258070952"] I want to be able to access the ['date'] value from the arrays contained in the array. [/quote] I'd probably do a loop through the array first to generate a key-less 1D array and then implode that. Other more array-knowledgeable types may well know a much better / elegant approach. It seems a fairly trivial bit of code, so I wouldn't be terribly hung up about the performance here. Quote:BTW, what tags did you use to get the non-code boxes? Quotes. If you quote-reply to a message, you can see all the tags that were used. Having trouble imploding an array. - El Forum - 11-12-2009 [eluser]Samuurai[/eluser] Thanks Jedd. I got what I wanted in the end using a slightly different method. I'm sure there's a more elegant way to do it, but here's my code anyway: Code: $dates = ""; Having trouble imploding an array. - El Forum - 11-12-2009 [eluser]jedd[/eluser] Oh, okay. That's very similar to what I suggested: Code: $darray = array(); Having trouble imploding an array. - El Forum - 11-12-2009 [eluser]Samuurai[/eluser] Ooh, I like your way better. Except I don't get the $foo => $datedata part. What is that doing? Having trouble imploding an array. - El Forum - 11-13-2009 [eluser]jedd[/eluser] Mine's definitely untested - it was a way of getting the date string more easily. Basically $foo gets assigned to the key, which you aren't interested in. (0, 1, 2 ...) You may have to change: Code: $darray[] = $datedata; to this: Code: $darray[] = $datedata['date']; I'm sure you can experiment. Having trouble imploding an array. - El Forum - 11-13-2009 [eluser]Isern Palaus[/eluser] [quote author="jedd" date="1258076780"]Oh, okay. That's very similar to what I suggested: Code: $darray = array(); But this will create an object, no? Is not better to use $query->result_array()? Code: foreach($query->result_array() as $id => $data) Having trouble imploding an array. - El Forum - 11-13-2009 [eluser]jedd[/eluser] You're absolutely right. I was distracted by the definition of the input data originally provided - an array of arrays. |