News:

Wondering if this will always be free?  See why free is better.

Main Menu

list to array with defined keys

Started by Anguz, August 28, 2003, 12:39:50 PM

Previous topic - Next topic

Anguz

if I have this

$list = "
   k1, v1\n
   k2, v2\n
   k3, v3";


I know that exploding it I can save each line in an array, but how can I use the first value in each line as the key? like this

'k1' => "v1",
'k2' => "v2",
'k3' => "v3"
Cristián Lávaque http://cristianlavaque.com

Parham

rough thinking of what i came up with, but it keeps adding an empty first element.. i have no idea where it's coming from:


<?php

$list 
"
   k1, v1\n
   k2, v2\n
   k3, v3"
;

foreach (
explode("\n"$list) as $element) {
  
$element trim($element);
  list(
$key,$value) = explode(","$element);
  if (!isset(
$array)) {
    
$array = array($key => $value);
  } else {
    
$array += array($key => $value);
  }
}

//testing
foreach ($array as $a => $b) {
print 
"$a - $b\n";
}

?>


chris

Quote from: Parham on August 28, 2003, 01:39:45 PM
rough thinking of what i came up with, but it keeps adding an empty first element.. i have no idea where it's coming from

quite simple ;D

replace

$list = "
   k1, v1\n
   k2, v2\n
   k3, v3";


with

$list = "k1, v1\n
   k2, v2\n
   k3, v3";


and it doesn't happen ;D ;D ;D

Anguz

aaaa I see... exploding the exploded does the trick

why did you write this line like this?

$array += array($key => $value);

the "+=" adds to the existing array instead of overwritting it?
Cristián Lávaque http://cristianlavaque.com

Parham

Quote from: Christian Land on August 28, 2003, 02:49:41 PM
Quote from: Parham on August 28, 2003, 01:39:45 PM
rough thinking of what i came up with, but it keeps adding an empty first element.. i have no idea where it's coming from

quite simple ;D

replace

$list = "
   k1, v1\n
   k2, v2\n
   k3, v3";


with

$list = "k1, v1\n
   k2, v2\n
   k3, v3";


and it doesn't happen ;D ;D ;D

I'm a complete idiot, TOTALLY didn't see that first newline...

* Parham walks away with a moronic feel :P

Parham

Quote from: Anguz on August 28, 2003, 03:38:19 PM
aaaa I see... exploding the exploded does the trick

why did you write this line like this?

$array += array($key => $value);

the "+=" adds to the existing array instead of overwritting it?

It's a bit of a stretch from how the += operator actually works (or how it's actually supposed to be used), but yeah, it adds a new array element to the existing array.

Anguz

#6
Quote from: Parham on August 28, 2003, 03:51:48 PM
It's a bit of a stretch from how the += operator actually works (or how it's actually supposed to be used), but yeah, it adds a new array element to the existing array.

I see, that looks like a neat way of doing it... other ways are there to add an array element to an array?

about the code you gave me, could it also be written like this?

foreach (explode("\n", $list) as $element)
{
   list($key, $value) = explode(",", trim($element));
   $array = (!isset($array) && $key !== '' && $value !== '' ? array($key => $value) : $array += array($key => $value));
}


does it make a difference in how fast it's parsed to write it in less lines? just wondering...

I also added

&& $key !== '' && $value !== ''

to the condition to avoid empty array elements, but there may be a better way to do it...
Cristián Lávaque http://cristianlavaque.com

Parham

speed doesn't really depend on the number of lines really... it still has to go through the same kind of checks and processes.

Anguz

#8
Quote from: Parham on August 28, 2003, 05:14:07 PM
speed doesn't really depend on the number of lines really... it still has to go through the same kind of checks and processes.

I understand

about the code I wrote above, the check to not have empty array elements was wrong, it should be more like this

foreach (explode("\n", $list) as $element)
{
   list($key, $value) = explode(",", trim($element));
   if ($key !== '' && $value !== '')
      $array = (!isset($array) ? array($key => $value) : $array += array($key => $value));
}


I think...
Cristián Lávaque http://cristianlavaque.com

Advertisement: