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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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