Jump to content

PHP Array Re-structured


anujgarg

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/203418-php-array-re-structured/
Share on other sites

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...

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

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);

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);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.