Forum Moderators: coopster

Message Too Old, No Replies

turing associtiative array key/value pairs into variables

         

Cherewest

5:01 pm on Jan 14, 2007 (gmt 0)

10+ Year Member



I have been trying to figure out how to take the key/value pairs of an array and turn them into variables using foreach.

so for example
$myArray[0][title]="Cool title";
$myArray[1][title]="Another Cool title";
$myArray[2][name]="My name";
$myArray[3][name]="Your name";

then I want
$title[0]="Cool title";
$title[1]="Another Cool title";
$name[0]="My name";
$name[1]="Your name";

I've tried many things the simplest of which was using foreach something like
$i=0;
foreach ($myArray as $key => $value){
foreach ($key as $k =>$v){
$k[$i]=$v;
$i++;
}
}

I've even tried variable variables and declaring $k as an array.
I got some weird results and learned alot. But no luck.
I should also say that I really want to keep the name of the new array and it's numbering predictable.

so I do not want
$title[0]="Cool title";
$title[1]="Another Cool title";
$name[2]="My name";
$name[3]="Your name";

Any ideas?
Thanks

eelixduppy

6:06 pm on Jan 14, 2007 (gmt 0)



Hello!

This is what I've come up with real quick. I'm not sure if it's the most efficient, but it certainly does work :)


$myArray[0]['title']="Cool title";
$myArray[1]['title']="Another Cool title";
$myArray[2]['name']="My name";
$myArray[3]['name']="Your name";

$titles = array();
$names = array();

$count = [url=http://us3.php.net/manual/en/function.count.php]count[/url]($myArray);
for($i = 0; $i < $count; $i++) {
$info = [url=http://us3.php.net/manual/en/function.each.php]each[/url]($myArray[$i]);
if($info['key'] == 'title') {
$titles[] = $myArray[$i]['title'];
} else {
$names[] = $myArray[$i]['name'];
}
}

echo '<pre>';
print_r($titles);
print_r($names);
echo '</pre>';

Hope this helps ;)

Cherewest

9:35 pm on Jan 14, 2007 (gmt 0)

10+ Year Member



Hi!

Thanks. It does look useful. I am playing with it to see how it behaves with more complicated arrays.

Thanks again!

cmarshall

11:33 pm on Jan 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is how I do it:


$myarray = array ( "key 1" => "value 1", "key 2" => "value 2", "key 3" => "value 3", "key 4" => "value 4", "key 5" => "value 5", "key 6" => "value 6" );

while(list($key, $value) = each ( $myarray ))
{
echo "\$myarray['".htmlspecialchars($key)."'] = '".htmlspecialchars($value)."'<br />";
}

jatar_k

1:45 pm on Jan 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



to expand on eelix's so you don't have to use specific keys

<? 
$myArray[0]['title']="Cool title";
$myArray[1]['title']="Another Cool title";
$myArray[2]['name']="My name";
$myArray[3]['name']="Your name";
$myArray[4]['other']="something else";
$myArray[5]['other']="and another";
$thearrays = array();
$count = count($myArray);
for($i = 0; $i < $count; $i++) {
$info = each($myArray[$i]);
if(!isset(${$info['key']})) {
${$info['key']} = array();
$thearrays[] = $info['key'];
}
${$info['key']}[] = $info['value'];
}
foreach ($thearrays as $key => $value) {
echo '<p>',$value,'<pre>';
print_r(${$value});
echo '</pre>';
}
?>

cmarshall

1:49 pm on Jan 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jatar_k looks like the best answer. I was in a hurry, and didn't read your post thoroughly, so ignore my input. Sorry about the distraction.

Cherewest

3:04 pm on Jan 15, 2007 (gmt 0)

10+ Year Member



I haven't tried jatar_k's version yet. Though I am sure it's going to work. It does shorten the code considerably.

I tried eelixduppy's code on several kinds of arrays, including this one and another where the number of elements in each sub array were not the same. It worked every time :-D always producing an array with predictable key/value pairs. The predicatabilty of where the data would go is what is perhaps most useful.

The one big thing I learned is the use of "key". Life would have been much simpler if I had known that :-D.

Anyway, here is my last trial array. Perhaps it will be useful to someone.

Thanks all.
Glad you guys are here!


<?php

$myArray= Array (
0 => Array ( 'myField1' =>'value 1', 'myField2' => 'another value 1', 'myField3' =>'5', 'myField4' => '0' ),
1 => Array ( 'myField1' =>'value 2', 'myField2' => 'another value 2','myField3' =>' 4', 'myField4' => '0' ),
2 => Array ( 'myField1' => 'value 3', 'myField2' => 'another value 3', 'myField3' =>' 3', 'myField4' => '0' ),
3 => Array ( 'myField1' => 'value 4', 'myField2' => 'another value 4', 'myField3' =>'2', 'myField4' => '0' ),
4 => Array ( 'myField1' => 'value 5', 'myField2' => 'another value 5', 'myField3' => '1', 'myField4' => '1' )
);

$dataFound =array();
$keyFound=array();
$x=0;
$count= count($myArray, COUNT_RECURSIVE);
foreach($myArray as $key => $value){
foreach($value as $k => $v){
$dataFound[$k.$x]=$v;
$x++;
}
}

$myField1 = array();
$myField2 = array();
$myField3 = array();
$myField4 = array();
$info=array();

$count = (count($dataFound));
for($i = 0; $i < $count; ) {
$info = each($dataFound);
switch($info[key]){
case('myField1'.$i):
$myField1[] = $dataFound['myField1'.$i];
continue;
case('myField2'.$i):
$myField2[] = $dataFound['myField2'.$i];
continue;
case('myField3'.$i):
$myField3[] = $dataFound['myField3'.$i];
continue;
case('myField4'.$i):
$myField4[]=$dataFound['myField4'.$i];
continue;
break;
}
$i++;
}

echo '<pre>';
print_r($myField1);
echo "next <br/>";
print_r($myField2);
echo "next <br/>";
print_r($myField3);
echo "next <br/>";
print_r($myField4);
echo '</pre>';

?>