Hello,
I have done some filtering in an array using unset. The problem is that if I do a var_dump on the array after the filtering, then some of the keys are missing. Eg, key 2 isn't showing.

So what I would like to do is to remap the keys so that Key 3 will turn into Key 2, Key 5 will turn into Key 4 etc. Basicly so I can do some loops later on with the array re-mapped later on.

Any ideas?

Cheers,

Member Avatar for LastMitch

@bsewell

I have done some filtering in an array using unset. The problem is that if I do a var_dump on the array after the filtering, then some of the keys are missing. Eg, key 2 isn't showing.

So what I would like to do is to remap the keys so that Key 3 will turn into Key 2, Key 5 will turn into Key 4 etc. Basicly so I can do some loops later on with the array re-mapped later on.

You know it would be nice if you provide the code that ran your array so it would be easier to see what is wrong than guessing what is wrong with it.

Member Avatar for diafol
$new_array = array_values($old_array);
print_r($new_array);

I had a go at that just now. So basicly I have an array of objects, where I use unset to delete some of the objects which have the same times. In all cases, where time duplication occurs I want to delete the first occrurance. It isn't liking it when using array_values.

$prevTime="";
for($i=0;$i<sizeOf($ipList);$i++)
{
  $ip=$ipList[$i];
  $prevTime="";
  for($j=0;$j<sizeOf($Details[$ip]);$j++)
  {
    if ($prevTime==$Details[$ip][$j]->getTime())
    {
      unset($Details[$ip][$j-1]);
    }
    $prevTime=$Details[$ip][$j]->getTime();

    echo $prevTime.'<br />';
  }

  echo "<h1>Next</h1>";
}

echo "<h2>Rehashed</h2>";
for($i=0;$i<sizeOf($Details);$i++)
{
$Details[$i]=array_values($Details[$i]);
}

Thanks

Member Avatar for diafol

Could you give an exaple of the contents of your arrays? This doesn't help too much.

Member Avatar for LastMitch

@bsewell

Like what diafol said what kind of array is this?

I'm bit confused why you have 2 $prevTime=""; ?

Is $Details[$i] is the array?

Just ran a var_dump on $Details. So Basicly Details contains a list of IP addresses, and within that I am pulling information for each IP. I hope that makes sense.

array(6) { ["149.26.491.94"]=> array(24) { [0]=> object(Record)#1 (5) { ["topic_title"]=> string(40) "Brain and spinal cord tumours: diagnosis" ["cat_name"]=> string(0) "" ["ip"]=> string(13) "149.26.491.94" ["search"]=> string(0) "" ["Ltime"]=> string(19) "2012-10-30 16:22:10" } [1]=> object(Record)#2 (5) { ["topic_title"]=> string(40) "Brain and spinal cord tumours: diagnosis" ["cat_name"]=> string(27) "Brain & spinal cord tumours" ["ip"]=> string(13) "149.26.491.94" ["search"]=> string(0) "" ["Ltime"]=> string(19) "2012-10-30 16:22:10" } [2]=> object(Record)#3 (5) { ["topic_title"]=> string(22) "Lung cancer: diagnosis" ["cat_name"]=> string(0) "" ["ip"]=> string(13) "149.26.491.94" ["search"]=> string(0) "" ["Ltime"]=> string(19) "2012-10-30 16:22:20" }

The var dump is massive, literally pulling 5000 records so I have pulled some records from one of the IPs.

Thanks,

Member Avatar for LastMitch

@bsewell

The problem is that if I do a var_dump on the array after the filtering, then some of the keys are missing. Eg, key 2 isn't showing.So what I would like to do is to remap the keys so that Key 3 will turn into Key 2, Key 5 will turn into Key 4 etc. Basicly so I can do some loops later on with the array re-mapped later on.

So you want key 2 to show.
Key 3 will turn into Key 2
Key 5 will turn into Key 4

OK, I be honest with you, can you put <pre> tags so it will echo out the array nicely right now the arrays are in a paragraph. It's a bit difficult to read it. If you don't how to put <pre> tags. It's goes like this:

<pre>
<?php 
You code
?>
</pre>

I just did that, I stripped it down to 1 IP and stripped some records.

The thing is that I want to call a method to get the value of one of values set for each object of Type record.

This is my code to try and retrieve the value:

$prevTime="";
foreach($Details as $detail=>$value)
{
  for($i=0;$i<sizeOf($detail);$i++)
  {
    if ($prevTime==$detail[$i]->getTime())
    {
      unset($detail[$i-1]);
    }
    $prevTime=$detail[$i]['Ltime'];
  }
}

This is the error which crops up when I use this code:

Fatal error: Call to a member function getTime() on a non-object

array(6) {
  ["201.23.226.83"]=>
  array(24) {
    [0]=>
    object(Record)#1 (5) {
      ["topic_title"]=>
      string(40) "Brain and spinal cord tumours: diagnosis"
      ["cat_name"]=>
      string(0) ""
      ["ip"]=>
      string(13) "201.23.226.83"
      ["search"]=>
      string(0) ""
      ["Ltime"]=>
      string(19) "2012-10-30 16:22:10"
    }
    [1]=>
    object(Record)#2 (5) {
      ["topic_title"]=>
      string(40) "Brain and spinal cord tumours: diagnosis"
      ["cat_name"]=>
      string(27) "Brain & spinal cord tumours"
      ["ip"]=>
      string(13) "201.23.226.83"
      ["search"]=>
      string(0) ""
      ["Ltime"]=>
      string(19) "2012-10-30 16:22:10"
    }
    [2]=>
    object(Record)#3 (5) {
      ["topic_title"]=>
      string(22) "Lung cancer: diagnosis"
      ["cat_name"]=>
      string(0) ""
      ["ip"]=>
      string(13) "201.23.226.83"
      ["search"]=>
      string(0) ""
      ["Ltime"]=>
      string(19) "2012-10-30 16:22:20"
    }
    [3]=>
    object(Record)#4 (5) {
      ["topic_title"]=>
      string(22) "Lung cancer: diagnosis"
      ["cat_name"]=>
      string(4) "Lung"
      ["ip"]=>
      string(13) "201.23.226.83"
      ["search"]=>
      string(0) ""
      ["Ltime"]=>
      string(19) "2012-10-30 16:22:20"
    }
  }
Member Avatar for diafol

Are these objects initialised, e.g. with a new? I'm assuming not, but I could be wrong. So this is similar to a mysql dataobject but you've included a couple of methods?

Yes they are. Your right, basicly each object is a row of a mysql result.

I run this code to create the objects. IpList is just an array to hold distinct IP addresses.

$Details[$ipList[$i]][]=new Record($row['topic_title'],$row['cat_name'],$row['ip_remote'],$row['search'],$row['time']);

And this is my Records class

class Record implements ArrayAccess
{
    public $topic_title="";
    public $cat_name="";
    public $ip="";
    public $search="";
    public $Ltime="";

    function __construct($topic_title=null,$cat_name=null,$ip=null,$search=null,$time=null)
    {
    $this->topic_title=$topic_title;
        $this->cat_name=$cat_name;
        $this->ip=$ip;
        $this->search=$search;
        $this->Ltime=$time;
  }

    public function getTopic()
    {
        return $this->topic_title;
    }

    public function getCat()
    {
        return $this->cat_name;
    }

    public function getIp()
    {
        return $this->ip;
    }

  public function getSearch()

    {
        return $this->search;
    }

    public function getTime()
    {
    return $this->Ltime;
    }

    public function isSearch()
    {
        if ($this->search!="")
        {
      return true;
        }
        else
        {
      return false;
        }
    }

    public function isClick()
    {
        if (($this->topic_title=="search")&&($this->cat_name==false))
        {
            return false;
        }

        else
        {
      return true;
        }
  }

  public function offsetExists($offset)
  {
  }

  public function offsetGet($offset)
  {
  }

  public function offsetSet($offset,$value)
  {
  }

  public function offsetUnset($offset)
  {
  }
}

At this point I just need to access the properties.

Thanks,

Member Avatar for diafol

Again I may be wrong, but this looks really complicated for a resultset.

SO lets get this right. You're creating an object for each result row. You then provide a method for outputting each property, which AFAIK do not modify the underlying data. So I'm a bit lost as to why. Is it to attach methods to an array? e.g.

$detail[$i]->getTime()

I tried doing this without using objects and the script fails due to an out of memory error. Already, using this object approach seems to be better, RAM used is 0.8MB for approx 500 rows.

I just want to retrieve Ltime from each object. I will then do some more processing with Ltime, and output some stuff to another array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.