Jump to content

php issue with array_splice()

I keep getting illegal string offset errors and I'm not quite sure why it's happening. I'm assuming it has something to do with how I'm getting the index, but I'm not sure what the right way to get the index of the element is. 

 

$comment_array = array();
if($result->num_rows > 0) {
   while($row = $result->fetch_assoc()){
       $new_sql = "SELECT * FROM comments WHERE reply_to = " . $row["comment_id"] . " AND reply_to != comment_id";
       $new_result = $conn->query($new_sql);
       //array_push($comment_array, $row);
       if ($new_result->num_rows > 0) {
          if($row["reply_to"] >= $row["comment_id"]){
              array_push($comment_array, $row);
          }
                                              
          while($new_row = $new_result->fetch_assoc()) {
              $index = -1; 
              foreach($comment_array as $key => $val){
                  if ($val['comment_id'] == $new_row["reply_to"]) {
                    $index = $key;
                    break;
                  }
              }
              if($index > -1){
                  array_splice($comment_array, $index, 0, $new_row);
              } else {
                  array_push($comment_array, $new_row);
              }
		   }
       } else if($row["reply_to"] == $row["comment_id"]){ 
           array_push($comment_array, $row);
       }
   }
}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/678195-php-issue-with-array_splice/
Share on other sites

Link to post
Share on other sites

Try to debug the code by printing out some information, if error occurred at line with array_splice, try to print out current length of array you trying to insert row into and index which you try insert the row at.

 

I could help you with that but I would need you to give me some example data for $row and $new_result. I guess $row is a comment and $new_result is set of replies to comments and you want to order it so the replies are under a comment that they are reply to. If it is the case I would just need an example of comments table, but if I'm wrong yo may explain me some more.

 

P.S.: Are you sure that it is necessary to check if comment is not a reply to itself? I guess it would be difficult to make by mistake, as when you insert a comment you don't know it's id yet. So for comment to be reply to itself it would involve an insert and then update with last inserted id.

 

Link to post
Share on other sites

1 hour ago, Mr_KoKa said:

Try to debug the code by printing out some information, if error occurred at line with array_splice, try to print out current length of array you trying to insert row into and index which you try insert the row at.

 

I could help you with that but I would need you to give me some example data for $row and $new_result. I guess $row is a comment and $new_result is set of replies to comments and you want to order it so the replies are under a comment that they are reply to. If it is the case I would just need an example of comments table, but if I'm wrong yo may explain me some more.

 

P.S.: Are you sure that it is necessary to check if comment is not a reply to itself? I guess it would be difficult to make by mistake, as when you insert a comment you don't know it's id yet. So for comment to be reply to itself it would involve an insert and then update with last inserted id.

 

It's a comment tree, so reply_to refers to the parent, and rather than setting the parent to 0 and starting to index the comments at 1, I made any original comment (i.e. not replying to anyone) replying to itself. 

 

If I print_r array[i ], I get:

 


Array ( [comment_id] => 0 [reply_to] => 0 [username] => user0 [comment] => comment0 )

 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

So data like:

array(
	array('comment_id' => 1, 'reply_to' => 0),
	array('comment_id' => 2, 'reply_to' => 0),
	array('comment_id' => 3, 'reply_to' => 1),	
	array('comment_id' => 4, 'reply_to' => 2),
	array('comment_id' => 5, 'reply_to' => 1),	
	array('comment_id' => 6, 'reply_to' => 2),	
	array('comment_id' => 7, 'reply_to' => 4),	
);

Should be put in such order (I will reference by comment_id)?:

array(1, 3, 5, 2, 4, 7, 6);

/*
	1
		3
		5
	2
		4
			7
		6
*/

Have you tried to debug yours?

Link to post
Share on other sites

3 minutes ago, Mr_KoKa said:

So data like:


array(
	array('comment_id' => 1, 'reply_to' => 0),
	array('comment_id' => 2, 'reply_to' => 0),
	array('comment_id' => 3, 'reply_to' => 1),	
	array('comment_id' => 4, 'reply_to' => 2),
	array('comment_id' => 5, 'reply_to' => 1),	
	array('comment_id' => 6, 'reply_to' => 2),	
	array('comment_id' => 7, 'reply_to' => 4),	
);

Should be put in such order (I will reference by comment_id)?:


array(1, 3, 5, 2, 4, 7, 6);

/*
	1
		3
		5
	2
		4
			7
		6
*/

Have you tried to debug yours?

I have a print statement at the end that prints 2 of the 3 attributes of each record in the array, and it prints everything as expected (when I just push new records onto the array rather than trying to splice them in somewhere, when I try to splice them in I get the error). 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

16 minutes ago, Mr_KoKa said:

Just try to print out the index you are trying to insert row at, and the length of comment array at this point.

If I put a  print_r($comment_array[$key]) inside the if statement where I'm setting $index=$key, then it prints the array element that the new record should be inserted after. So the first $index will be 2, and the length of the array is 3. So I want to insert the new record at index=3. Is splicing onto the end of an array illegal?

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

Hm, now I noticed that. You see, when you array_splice, the 4th argument to array_splice is array of elements you want to insert, so when you pass there an array which $new_row is, then every element of array will be inserted as new element, you would need to surround your $new_row with array().

 

To make a simple example:

 

$a = array(1, 2);
$row = array('a' => 1, 'b' => 2, 'c' => 3);

array_splice($a, 2, 0, $row);
// Will result in $a array being:
// array(1, 2, 1, 2, 3)
// not as you expect:
// array(1, 2, array(1, 2, 3));

// in this case valid array_splice would look like
array_splice($a, 2, 0, array($row));

It may not fix it though. Please affirm that my example of data and result order is valid so I can try to accomplish it myself.

Link to post
Share on other sites

44 minutes ago, Mr_KoKa said:

Hm, now I noticed that. You see, when you array_splice, the 4th argument to array_splice is array of elements you want to insert, so when you pass there an array which $new_row is, then every element of array will be inserted as new element, you would need to surround your $new_row with array().

 

To make a simple example:

 


$a = array(1, 2);
$row = array('a' => 1, 'b' => 2, 'c' => 3);

array_splice($a, 2, 0, $row);
// Will result in $a array being:
// array(1, 2, 1, 2, 3)
// not as you expect:
// array(1, 2, array(1, 2, 3));

// in this case valid array_splice would look like
array_splice($a, 2, 0, array($row));

It may not fix it though. Please affirm that my example of data and result order is valid so I can try to accomplish it myself.

I'll test it later. I decided to just scrap the idea of a tree'd comment system and just go with a forum style comment system. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×