E_Rider Posted May 10, 2014 Share Posted May 10, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288389-array_chunk/ Share on other sites More sharing options...
Ch0cu3r Posted May 10, 2014 Share Posted May 10, 2014 (edited) 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 May 10, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/288389-array_chunk/#findComment-1479003 Share on other sites More sharing options...
E_Rider Posted May 10, 2014 Author Share Posted May 10, 2014 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_count1row_count2Array ( [0] => Array ( [0] => Acton ) [1] => Array ( [0] => Acton ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Alfred ) [1] => Array ( [0] => Alfred ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Bar Mills ) [1] => Array ( [0] => Bar Mills ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Berwick ) [1] => Array ( [0] => Berwick ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Biddeford ) [1] => Array ( [0] => Biddeford ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Buxton ) [1] => Array ( [0] => Buxton ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Cape Neddick ) [1] => Array ( [0] => Cape Neddick ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Cape Porpoise ) [1] => Array ( [0] => Cape Porpoise ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Cornish ) [1] => Array ( [0] => Cornish ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => East Parsonsfield ) [1] => Array ( [0] => East Parsonsfield ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => East Waterboro ) [1] => Array ( [0] => East Waterboro ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Eliot ) [1] => Array ( [0] => Eliot ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Hollis Center ) [1] => Array ( [0] => Hollis Center ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Kennebunk ) [1] => Array ( [0] => Kennebunk ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Kennebunkport ) [1] => Array ( [0] => Kennebunkport ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Kittery ) [1] => Array ( [0] => Kittery ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Kittery Point ) [1] => Array ( [0] => Kittery Point ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Lebanon ) [1] => Array ( [0] => Lebanon ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Limerick ) [1] => Array ( [0] => Limerick ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Limington ) [1] => Array ( [0] => Limington ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Moody ) [1] => Array ( [0] => Moody ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Newfield ) [1] => Array ( [0] => Newfield ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => North Berwick ) [1] => Array ( [0] => North Berwick ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => North Waterboro ) [1] => Array ( [0] => North Waterboro ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Ogunquit ) [1] => Array ( [0] => Ogunquit ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Old Orchard Beach ) [1] => Array ( [0] => Old Orchard Beach ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Parsonsfield ) [1] => Array ( [0] => Parsonsfield ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Saco ) [1] => Array ( [0] => Saco ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Sanford ) [1] => Array ( [0] => Sanford ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Shapleigh ) [1] => Array ( [0] => Shapleigh ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => South Berwick ) [1] => Array ( [0] => South Berwick ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Springvale ) [1] => Array ( [0] => Springvale ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Waterboro ) [1] => Array ( [0] => Waterboro ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => Wells ) [1] => Array ( [0] => Wells ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => West Kennebunk ) [1] => Array ( [0] => West Kennebunk ) ) rowt_count2row_count2Array ( [0] => Array ( [0] => West Newfield ) [1] => Array ( [0] => West Newfield ) ) rowt_count2row_count2Array ( [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. Quote Link to comment https://forums.phpfreaks.com/topic/288389-array_chunk/#findComment-1479011 Share on other sites More sharing options...
E_Rider Posted May 10, 2014 Author Share Posted May 10, 2014 Is the fact that the $result is a multidimensional array causing this. I tested the base code from php.net and I am getting it to work. $input_array = array('a', 'b', 'c', 'd', 'e'); print_r(array_chunk($input_array, 1)); print_r(array_chunk($input_array, 2, true)); Quote Link to comment https://forums.phpfreaks.com/topic/288389-array_chunk/#findComment-1479012 Share on other sites More sharing options...
Ch0cu3r Posted May 10, 2014 Share Posted May 10, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/288389-array_chunk/#findComment-1479015 Share on other sites More sharing options...
E_Rider Posted May 10, 2014 Author Share Posted May 10, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288389-array_chunk/#findComment-1479016 Share on other sites More sharing options...
E_Rider Posted May 10, 2014 Author Share Posted May 10, 2014 I see why I was getting double arrays from the fetch and according to 3schools , wich I know to be wrong at times, fetch calls an associative array. Got to stop relying on 3schools to be correct and read the right manual. Quote Link to comment https://forums.phpfreaks.com/topic/288389-array_chunk/#findComment-1479017 Share on other sites More sharing options...
Solution E_Rider Posted May 10, 2014 Author Solution Share Posted May 10, 2014 Thanks for the help the final solution seems to be mysqli_fetch_all($result , MYSQLI_ASSOC) "is the type". I have not tested this fully yet but the print_r call seems fine. Quote Link to comment https://forums.phpfreaks.com/topic/288389-array_chunk/#findComment-1479019 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.