Re: [PHP] array conversion

2010-02-20 Thread clancy_1
Or:

$a = array ('Cats', 'white', 'Dogs', 'black', 'Mice', 'grey', 'Camels', 
'brown');
$b = '';// Just in case it has some leftover 
value
$k = 2* (int) (count ($a)/2);   // ensure even no of terms
$i = 0; while ($i  $k)
{ 
$b[$a[$i++]] = $a[$i++];  // ***
}

And this works:
$i = 0; $k = array_keys($b); 
while ($i  count($b)) {echo 'h5'.$i.': '.$k[$i].' = '. 
$b[$k[$i++]].'/h5'; }

0: Cats = white
1: Dogs = black
2: Mice = grey
3: Camels = brown

( *** I have always been wary of using statements like this because I was 
unsure when the
incrementing would occur, so I tried it.)

Clancy

On Fri, 19 Feb 2010 02:26:49 -0500, simples...@gmail.com (Adam Richardson) 
wrote:

Or,

function new_arr(array $arr)
{
$count = count($arr);
if ($count % 2 != 0) throw new Exception('The new_arr() function
requires an even number of elements.');
for ($i = 0; $i  $count; $i += 2)
{
$new_arr[$arr[$i]] = $arr[$i + 1];
}
return $new_arr;
}

$test = new_arr(array('k1', 'v1', 'k2', 'v2', 'k3', 'v3'));

exit(var_dump($test));

On Fri, Feb 19, 2010 at 1:19 AM, Larry Garfield la...@garfieldtech.comwrote:

 On Thursday 18 February 2010 11:58:28 pm Paul M Foster wrote:
  On Fri, Feb 19, 2010 at 01:20:12PM +0800, Dasn wrote:
   Hi guys. How to convert an array like:
  
   Array
   (
   [0] = key1
   [1] = value1
   [2] = key2
   [3] = value2
   )
  
   to
  
  
   Array
   (
   [key1] = value1
   [key2] = value2
   )
  
   Is there a built-in function to do this?
   Please Cc me. :)
   Thank you in advance.
 
  I don't believe so, but rolling your own should not be too hard:
 
  $a = array($key1, $value1, $key2, $value2);
  $b = array();
  $numitems = count($a);
 
  for ($i = 0; $i  $numitems; $i++) {
if ($i % 2 == 0) {
$saved_key = $a[$i];
}
elseif ($i % 2 == 1) {
$b[$saved_key] = $a[$i];
}
  }
 
  Code is crude and untested, but you get the idea.
 
  Paul

 This would be even shorter, I think:

 foreach ($items as $i = $value) {
  $temp[$i % 2][] = $value;
 }
 $done = array_combine($temp[0], $temp[1]);

 (Also untested, just off the cuff...)

 --Larry Garfield

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array conversion

2010-02-19 Thread Richard Quadling
On 19 February 2010 07:26, Adam Richardson simples...@gmail.com wrote:
 Or,

 function new_arr(array $arr)
 {
    $count = count($arr);
    if ($count % 2 != 0) throw new Exception('The new_arr() function
 requires an even number of elements.');
    for ($i = 0; $i  $count; $i += 2)
    {
        $new_arr[$arr[$i]] = $arr[$i + 1];
    }
    return $new_arr;
 }

 $test = new_arr(array('k1', 'v1', 'k2', 'v2', 'k3', 'v3'));

 exit(var_dump($test));

 On Fri, Feb 19, 2010 at 1:19 AM, Larry Garfield la...@garfieldtech.comwrote:

 On Thursday 18 February 2010 11:58:28 pm Paul M Foster wrote:
  On Fri, Feb 19, 2010 at 01:20:12PM +0800, Dasn wrote:
   Hi guys. How to convert an array like:
  
   Array
   (
       [0] = key1
       [1] = value1
       [2] = key2
       [3] = value2
   )
  
   to
  
  
   Array
   (
       [key1] = value1
       [key2] = value2
   )
  
   Is there a built-in function to do this?
   Please Cc me. :)
   Thank you in advance.
 
  I don't believe so, but rolling your own should not be too hard:
 
  $a = array($key1, $value1, $key2, $value2);
  $b = array();
  $numitems = count($a);
 
  for ($i = 0; $i  $numitems; $i++) {
        if ($i % 2 == 0) {
                $saved_key = $a[$i];
        }
        elseif ($i % 2 == 1) {
                $b[$saved_key] = $a[$i];
        }
  }
 
  Code is crude and untested, but you get the idea.
 
  Paul

 This would be even shorter, I think:

 foreach ($items as $i = $value) {
  $temp[$i % 2][] = $value;
 }
 $done = array_combine($temp[0], $temp[1]);

 (Also untested, just off the cuff...)

 --Larry Garfield

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




I'd say that this cat is well and truly skinned!

 --
 Nephtali:  PHP web framework that functions beautifully
 http://nephtaliproject.com




-- 
-
Richard Quadling
Standing on the shoulders of some very clever giants!
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array conversion

2010-02-19 Thread tedd

At 10:48 AM + 2/19/10, Richard Quadling wrote:

On 19 February 2010 07:26, Adam Richardson simples...@gmail.com wrote:
 Or,


Code fight!!!

http://www.webbytedd.com/ccc/array/

After reviewing the entries, mine does not provide any significant 
difference. I did it as a mental exercise after looking at several 
built-in array functions (array_flip(), array_combine(), etc. ) that 
I thought might solve the problem, but didn't.


tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array conversion

2010-02-19 Thread Richard Quadling
On 19 February 2010 15:52, tedd tedd.sperl...@gmail.com wrote:
 At 10:48 AM + 2/19/10, Richard Quadling wrote:

 On 19 February 2010 07:26, Adam Richardson simples...@gmail.com wrote:
  Or,

 Code fight!!!

 http://www.webbytedd.com/ccc/array/

 After reviewing the entries, mine does not provide any significant
 difference. I did it as a mental exercise after looking at several built-in
 array functions (array_flip(), array_combine(), etc. ) that I thought might
 solve the problem, but didn't.

 tedd
 --
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com


Just wanting to join in.

?php
$array = array
(
'key1',
'value1',
'key2',
'value2',
);

$result = array();
while(!is_null($result[array_shift($array)] = array_shift($array)));
array_pop($result);
print_r($result);
?

outputs ...

Array
(
[key1] = value1
[key2] = value2
)



-- 
-
Richard Quadling
Standing on the shoulders of some very clever giants!
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] array conversion

2010-02-18 Thread Dasn

Hi guys. How to convert an array like:

Array
(
[0] = key1
[1] = value1
[2] = key2
[3] = value2
)

to


Array
(
[key1] = value1
[key2] = value2
)

Is there a built-in function to do this?
Please Cc me. :)
Thank you in advance.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array conversion

2010-02-18 Thread Paul M Foster
On Fri, Feb 19, 2010 at 01:20:12PM +0800, Dasn wrote:

 Hi guys. How to convert an array like:

 Array
 (
 [0] = key1
 [1] = value1
 [2] = key2
 [3] = value2
 )

 to


 Array
 (
 [key1] = value1
 [key2] = value2
 )

 Is there a built-in function to do this?
 Please Cc me. :)
 Thank you in advance.

I don't believe so, but rolling your own should not be too hard:

$a = array($key1, $value1, $key2, $value2);
$b = array();
$numitems = count($a);

for ($i = 0; $i  $numitems; $i++) {
if ($i % 2 == 0) {
$saved_key = $a[$i];
}
elseif ($i % 2 == 1) {
$b[$saved_key] = $a[$i];
}
}

Code is crude and untested, but you get the idea.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array conversion

2010-02-18 Thread Larry Garfield
On Thursday 18 February 2010 11:58:28 pm Paul M Foster wrote:
 On Fri, Feb 19, 2010 at 01:20:12PM +0800, Dasn wrote:
  Hi guys. How to convert an array like:
 
  Array
  (
  [0] = key1
  [1] = value1
  [2] = key2
  [3] = value2
  )
 
  to
 
 
  Array
  (
  [key1] = value1
  [key2] = value2
  )
 
  Is there a built-in function to do this?
  Please Cc me. :)
  Thank you in advance.
 
 I don't believe so, but rolling your own should not be too hard:
 
 $a = array($key1, $value1, $key2, $value2);
 $b = array();
 $numitems = count($a);
 
 for ($i = 0; $i  $numitems; $i++) {
   if ($i % 2 == 0) {
   $saved_key = $a[$i];
   }
   elseif ($i % 2 == 1) {
   $b[$saved_key] = $a[$i];
   }
 }
 
 Code is crude and untested, but you get the idea.
 
 Paul

This would be even shorter, I think:

foreach ($items as $i = $value) {
  $temp[$i % 2][] = $value;
}
$done = array_combine($temp[0], $temp[1]);

(Also untested, just off the cuff...)

--Larry Garfield

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array conversion

2010-02-18 Thread Adam Richardson
Or,

function new_arr(array $arr)
{
$count = count($arr);
if ($count % 2 != 0) throw new Exception('The new_arr() function
requires an even number of elements.');
for ($i = 0; $i  $count; $i += 2)
{
$new_arr[$arr[$i]] = $arr[$i + 1];
}
return $new_arr;
}

$test = new_arr(array('k1', 'v1', 'k2', 'v2', 'k3', 'v3'));

exit(var_dump($test));

On Fri, Feb 19, 2010 at 1:19 AM, Larry Garfield la...@garfieldtech.comwrote:

 On Thursday 18 February 2010 11:58:28 pm Paul M Foster wrote:
  On Fri, Feb 19, 2010 at 01:20:12PM +0800, Dasn wrote:
   Hi guys. How to convert an array like:
  
   Array
   (
   [0] = key1
   [1] = value1
   [2] = key2
   [3] = value2
   )
  
   to
  
  
   Array
   (
   [key1] = value1
   [key2] = value2
   )
  
   Is there a built-in function to do this?
   Please Cc me. :)
   Thank you in advance.
 
  I don't believe so, but rolling your own should not be too hard:
 
  $a = array($key1, $value1, $key2, $value2);
  $b = array();
  $numitems = count($a);
 
  for ($i = 0; $i  $numitems; $i++) {
if ($i % 2 == 0) {
$saved_key = $a[$i];
}
elseif ($i % 2 == 1) {
$b[$saved_key] = $a[$i];
}
  }
 
  Code is crude and untested, but you get the idea.
 
  Paul

 This would be even shorter, I think:

 foreach ($items as $i = $value) {
  $temp[$i % 2][] = $value;
 }
 $done = array_combine($temp[0], $temp[1]);

 (Also untested, just off the cuff...)

 --Larry Garfield

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com


[PHP] Array conversion

2003-05-30 Thread John Wulff
I'm new to working with complex arrays/objects and need a little help.
Below is the array I need to convert to a different type of array.  The
begin what i have section is data I've succesfully mined from an XML file.
The data labeled desired output is how I need the data to be formatted.
How do I do this?


//BEGIN WHAT I HAVE
Array
(
[0] = ssions Object
(
[name] = Apr 2003
[stocks] = 82024.52
[bonds] = 104896.27
[mutual] = 1171816.79
[insurance] = 2352039.80
[other] = 33644.55
)

[1] = ssions Object
(
[name] = Mar 2003
[stocks] = 77045.10
[bonds] = 119246.42
[mutual] = 806411.00
[insurance] = 1456819.41
[other] = 5727.44
)

[2] = ssions Object
(
[name] = Feb 2003
[stocks] = 82307.25
[bonds] = 77010.68
[mutual] = 800041.89
[insurance] = 2119164.09
[other] = 20440.45
)

)


//BEGIN DESIRED OUTPUT
Array
(
[0] = Array
(
[0] = Apr 2003
[1] = 82024.52
[2] = 104896.27
[3] = 1171816.79
[4] = 2352039.8
[5] = 33644.55
)

[1] = Array
(
[0] = Mar 2003
[1] = 77045.1
[2] = 119246.42
[3] = 806411
[4] = 1456819.41
[5] = 5727.44
)

[2] = Array
(
[0] = Feb 2003
[1] = 82307.25
[2] = 77010.68
[3] = 800041.89
[4] = 2119164.09
[5] = 20440.45
)

)





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array conversion

2003-05-30 Thread Ernest E Vogelsinger
At 01:38 30.05.2003, John Wulff said:
[snip]
I'm new to working with complex arrays/objects and need a little help.
Below is the array I need to convert to a different type of array.  The
begin what i have section is data I've succesfully mined from an XML file.
The data labeled desired output is how I need the data to be formatted.
How do I do this?
[snip]

$result = array();
while (list($key, $object) = each($whatihavelist)) {
 $result[] = array($object-name,
   $object-stocks,
   $object-bonds,
   $object-mutual,
   $object-insurance,
   $object-other);
}

But why would you want to skip your nicely named, easily readable objects
in favour of sequentially numbered arrays?

Just wondering...

-- 
   O Ernest E. Vogelsinger
   (\)ICQ #13394035
^ http://www.vogelsinger.at/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php