Jump to content

Recommended Posts

Hi,

I am trying to split an array with array_chunk and having no luck.

I can call the array, display the array, but not as two or more just not working as I understand it .

I am pulling the array from MYSQL and have used count() to count arrays to debug this problem but get strange results.

mysqli_query returns a value of one as expected, and this is where things get strange as mysqli_fetch_array variable counts as two before I even change or try to chunk, and chunk always returns two and will echo the whole array not chunks $array[0] echos but $array[1] is null.

I am either trying to do something that cannot be done or my understanding is wrong. I have read the documentation over and over and anything else I could find with no luck.

Any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/288389-array_chunk/
Share on other sites

 

 

mysqli_query returns a value of one as expected

mysql_query returns a result set

 

 

 

nd this is where things get strange as mysqli_fetch_array variable counts as two

the mysql_fetch_* functions is used to get the next row from the result set, eg the first time it is called the first row is returned from the results, the second call will return the second row and so on.

 

I'm confused where you're using array_chunk. Can you post your code and the output you are trying to achieve

Edited by Ch0cu3r
Link to comment
https://forums.phpfreaks.com/topic/288389-array_chunk/#findComment-1479003
Share on other sites

 I have cut this down and get each row but no new keys.

if  (!$result=mysqli_query($con,$sql))
{
die('Could not get data: ' . mysqli_error($sql));
}



echo "result_count".count($result)."<br/>";
while( $row=mysqli_fetch_array($result) ) 
{ 
echo "row_count".count($row)."<br/>";
   $rowt = array_chunk( $row, 1); 
	
    print_r($rowt);
		echo "rowt_count".count($rowt)."<br/>";
		echo "<br/><br/>";
} 

 and this is output

 

 

result_count1
row_count2
Array ( [0] => Array ( [0] => Acton ) [1] => Array ( [0] => Acton ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Alfred ) [1] => Array ( [0] => Alfred ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Bar Mills ) [1] => Array ( [0] => Bar Mills ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Berwick ) [1] => Array ( [0] => Berwick ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Biddeford ) [1] => Array ( [0] => Biddeford ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Buxton ) [1] => Array ( [0] => Buxton ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Cape Neddick ) [1] => Array ( [0] => Cape Neddick ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Cape Porpoise ) [1] => Array ( [0] => Cape Porpoise ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Cornish ) [1] => Array ( [0] => Cornish ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => East Parsonsfield ) [1] => Array ( [0] => East Parsonsfield ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => East Waterboro ) [1] => Array ( [0] => East Waterboro ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Eliot ) [1] => Array ( [0] => Eliot ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Hollis Center ) [1] => Array ( [0] => Hollis Center ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Kennebunk ) [1] => Array ( [0] => Kennebunk ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Kennebunkport ) [1] => Array ( [0] => Kennebunkport ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Kittery ) [1] => Array ( [0] => Kittery ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Kittery Point ) [1] => Array ( [0] => Kittery Point ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Lebanon ) [1] => Array ( [0] => Lebanon ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Limerick ) [1] => Array ( [0] => Limerick ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Limington ) [1] => Array ( [0] => Limington ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Moody ) [1] => Array ( [0] => Moody ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Newfield ) [1] => Array ( [0] => Newfield ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => North Berwick ) [1] => Array ( [0] => North Berwick ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => North Waterboro ) [1] => Array ( [0] => North Waterboro ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Ogunquit ) [1] => Array ( [0] => Ogunquit ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Old Orchard Beach ) [1] => Array ( [0] => Old Orchard Beach ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Parsonsfield ) [1] => Array ( [0] => Parsonsfield ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Saco ) [1] => Array ( [0] => Saco ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Sanford ) [1] => Array ( [0] => Sanford ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Shapleigh ) [1] => Array ( [0] => Shapleigh ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => South Berwick ) [1] => Array ( [0] => South Berwick ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Springvale ) [1] => Array ( [0] => Springvale ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Waterboro ) [1] => Array ( [0] => Waterboro ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => Wells ) [1] => Array ( [0] => Wells ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => West Kennebunk ) [1] => Array ( [0] => West Kennebunk ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => West Newfield ) [1] => Array ( [0] => West Newfield ) ) rowt_count2


row_count2
Array ( [0] => Array ( [0] => York ) [1] => Array ( [0] => York ) ) rowt_count2
 

and is expected but with new keys and less base arrays for lack of a better term.

Link to comment
https://forums.phpfreaks.com/topic/288389-array_chunk/#findComment-1479011
Share on other sites

mysqli_fetch_array returns an numerically indexed and associative indexed array for each field returned from your query. Because you are using array chunk it is removing the associative keys from the associative array. if you set the 3rd argument for array_chunk to true the keys will be preserved.

 

What I do not understand is why you are using array_chunk? if you dont want your data to be listed in different arrays then use either mysqli_fetch_row or mysqli_fetch_assoc

Link to comment
https://forums.phpfreaks.com/topic/288389-array_chunk/#findComment-1479015
Share on other sites

I am begining to see this and what I am trying to do is insert a delimiter in the middle of the mysqli_fech_array($result) so I can split it between two sections of a page. ie: split the $result so it can be called like $result[0] and $result[1]. I am hesitant to sat it cannot be done , but see I am doing it the wrong way.

Link to comment
https://forums.phpfreaks.com/topic/288389-array_chunk/#findComment-1479016
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.