Welcome Guest, Not a member yet? Register   Sign In
Having trouble imploding an array.
#1

[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);
resultst in this:

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
#2

[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.
#3

[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";
$myarray = array($result[0][date], $result[1][date], $result[2][date]);

Is this what you call a multidimensional array?

BTW, what tags did you use to get the non-code boxes?
#4

[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.
#5

[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 = "";
foreach($query->result() as $row)
{
                $dates .= $row->date . ",";
}
$dates = substr($dates, 0, -1); //trim off trailing comma
return $dates;
#6

[eluser]jedd[/eluser]
Oh, okay. That's very similar to what I suggested:
Code:
$darray = array();

foreach($query->result() as $foo => $datedata)
    $darray[] = $datedata;

return (implode (', ', $darray));
#7

[eluser]Samuurai[/eluser]
Ooh, I like your way better.

Except I don't get the $foo => $datedata part. What is that doing?
#8

[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.
#9

[eluser]Isern Palaus[/eluser]
[quote author="jedd" date="1258076780"]Oh, okay. That's very similar to what I suggested:
Code:
$darray = array();

foreach($query->result() as $foo => $datedata)
    $darray[] = $datedata;

return (implode (', ', $darray));
[/quote]

But this will create an object, no? Is not better to use $query->result_array()?

Code:
foreach($query->result_array() as $id => $data)
     $dates = $data['date'];
#10

[eluser]jedd[/eluser]
You're absolutely right.

I was distracted by the definition of the input data originally provided - an array of arrays.




Theme © iAndrew 2016 - Forum software by © MyBB