anujgarg Posted May 31, 2010 Share Posted May 31, 2010 Hi Everyone, I am having an array in this format: Array ( [0] => cassandra_ColumnOrSuperColumn Object ( [column] => cassandra_Column Object ( [name] => access [value] => 0 [timestamp] => 1275304054 ) [super_column] => ) [1] => cassandra_ColumnOrSuperColumn Object ( [column] => cassandra_Column Object ( [name] => activation [value] => [timestamp] => 1275299259 ) [super_column] => ) [2] => cassandra_ColumnOrSuperColumn Object ( [column] => cassandra_Column Object ( [name] => alias [value] => entertainment [timestamp] => 1275304054 ) [super_column] => ) ) I want to convert it in the following format: (Syntax) => name:value name:value (eg.) => access:0 activation: alias:entertainment Please suggest how can I do that? Thanks in advance Anuj Quote Link to comment https://forums.phpfreaks.com/topic/203418-php-array-re-structured/ Share on other sites More sharing options...
trq Posted May 31, 2010 Share Posted May 31, 2010 Explain again exactly what it is you want. Quote Link to comment https://forums.phpfreaks.com/topic/203418-php-array-re-structured/#findComment-1065655 Share on other sites More sharing options...
riwan Posted May 31, 2010 Share Posted May 31, 2010 yes, its a bit confusing the way you explain it. shouldn't your array just like this $arrvar["access"] = "0"; $arrvar["activation"] = ""; $arrvar["alias"] = "entertainment"; Quote Link to comment https://forums.phpfreaks.com/topic/203418-php-array-re-structured/#findComment-1065662 Share on other sites More sharing options...
anujgarg Posted May 31, 2010 Author Share Posted May 31, 2010 ok...so let me explain it: I have an array of objects that contains key-value combination. I have to display that key value pair in my result. For ex., in first element of array we have: [name] => access [value] => 0 [timestamp] => 1275304054 I need to fetch value of 'name' that is "access" and value of 'value' that is "0". Hope it is clear now... Quote Link to comment https://forums.phpfreaks.com/topic/203418-php-array-re-structured/#findComment-1065676 Share on other sites More sharing options...
trq Posted May 31, 2010 Share Posted May 31, 2010 foreach ($array as $a) { echo $a->column->name; echo $a->column->value; } Quote Link to comment https://forums.phpfreaks.com/topic/203418-php-array-re-structured/#findComment-1065681 Share on other sites More sharing options...
anujgarg Posted May 31, 2010 Author Share Posted May 31, 2010 Thanks for the reply, thorpe...but nothing is being displayed on page. Quote Link to comment https://forums.phpfreaks.com/topic/203418-php-array-re-structured/#findComment-1065691 Share on other sites More sharing options...
riwan Posted May 31, 2010 Share Posted May 31, 2010 well, if you could show your code, then you could get a better solution Quote Link to comment https://forums.phpfreaks.com/topic/203418-php-array-re-structured/#findComment-1065692 Share on other sites More sharing options...
anujgarg Posted May 31, 2010 Author Share Posted May 31, 2010 Riwan, I am using cassandra and trying to move MySQL data to it. This is the format that cassandra displays after inserting the records in its table. I have displayed the output in my post above...if you want to see the code, please let me know... Quote Link to comment https://forums.phpfreaks.com/topic/203418-php-array-re-structured/#findComment-1065697 Share on other sites More sharing options...
trq Posted May 31, 2010 Share Posted May 31, 2010 Again, post your code. Quote Link to comment https://forums.phpfreaks.com/topic/203418-php-array-re-structured/#findComment-1065699 Share on other sites More sharing options...
ignace Posted May 31, 2010 Share Posted May 31, 2010 cassandra_ColumnOrSuperColumn cassandra_Column Is that the composite pattern? If so then class cassandra_ColumnOrSuperColumn { public function findColumnByFieldValue($value) {} } class cassandra_Column extends cassandra_ColumnOrSuperColumn { public function findColumnByFieldValue($value) { return NULL; } } class cassandra_SuperColumn extends cassandra_ColumnOrSuperColumn { public function findColumnByFieldValue($value) { foreach ($columns as $column) { if (in_array($value, $column->toArray())) { return $column; } } return NULL; } } In your main program: $access = $columnOrSuperColumn->findColumnByFieldValue('access'); if (NULL !== $access) {//found Quote Link to comment https://forums.phpfreaks.com/topic/203418-php-array-re-structured/#findComment-1065705 Share on other sites More sharing options...
anujgarg Posted May 31, 2010 Author Share Posted May 31, 2010 For inserting record: $query = "SELECT * from jos_table"; $rslt = mysql_query($query) or die(mysql_error()); $total = mysql_num_rows($rslt); $row=0; while($content_var = mysql_fetch_assoc($rslt)) { foreach($content_var as $key => $val){ $columnPath->column = $key; $value = $val; $client->insert($keyspace, $row, $columnPath, $value, $timestamp, $consistency_level); } $row++; } For displaying record: for($i=0;$i<$total;$i++) { $keyUserId = $i; $result[$i] = $client->get_slice($keyspace, $keyUserId, $columnParent, $predicate, $consistency_level); } print_r($result); Quote Link to comment https://forums.phpfreaks.com/topic/203418-php-array-re-structured/#findComment-1065707 Share on other sites More sharing options...
riwan Posted May 31, 2010 Share Posted May 31, 2010 I'm not familiar with cassandra but your script is a bit confusing as it should be a multi dimensional array of objects. if its just single dimension array of objects, the display should be within the while loop you could just code it like this $query = "SELECT * from jos_table"; $rslt = mysql_query($query) or die(mysql_error()); $total = mysql_num_rows($rslt); $row=0; while($content_var = mysql_fetch_assoc($rslt)) { $total2 = 0; foreach($content_var as $key => $val){ $columnPath->column = $key; $value = $val; $client->insert($keyspace, $row, $columnPath, $value, $timestamp, $consistency_level); $col_arr[$row][$total2]->name = $key; $col_arr[$row][$total2]->value = $val; $total2++; } $row++; } and then for the display for($i=0;$i<$total;$i++) { $keyUserId = $i; $result[$i] = $client->get_slice($keyspace, $keyUserId, $columnParent, $predicate, $consistency_level); print "<br/>Row ".($i+1)."<br/>"; for($j=0;$j<$total2;$j++) { print $col_arr[$i][$j]->name.":".$col_arr[$i][$j]->value."<br/>"; } } print_r($result); Quote Link to comment https://forums.phpfreaks.com/topic/203418-php-array-re-structured/#findComment-1065722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.