smith.james0 Posted December 22, 2006 Share Posted December 22, 2006 How do cut an array in half to make two arrays?I have tried array_split() but i am not sure how you make two new arraysMany thanks James Quote Link to comment https://forums.phpfreaks.com/topic/31572-cutting-an-array-in-half/ Share on other sites More sharing options...
trq Posted December 22, 2006 Share Posted December 22, 2006 There is no such function as array_split. What exactly does your array look like, and where do you want it split?You could use array_slice.[code]<?php $a = array(1,2,3,4); $b = array_slice($a,0,1); $c = array_slice($a,2,3);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31572-cutting-an-array-in-half/#findComment-146318 Share on other sites More sharing options...
smith.james0 Posted December 22, 2006 Author Share Posted December 22, 2006 Sorry forgot to say I don't know the length of the array, I just need to cut the last two values of the end and use them else where.Thanks James Quote Link to comment https://forums.phpfreaks.com/topic/31572-cutting-an-array-in-half/#findComment-146321 Share on other sites More sharing options...
trq Posted December 22, 2006 Share Posted December 22, 2006 [code=php:0]$new = array_slice($old,-2,2);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31572-cutting-an-array-in-half/#findComment-146322 Share on other sites More sharing options...
smith.james0 Posted December 22, 2006 Author Share Posted December 22, 2006 How can a delete the values from the old array, as I don't the array keys?Thanks James Quote Link to comment https://forums.phpfreaks.com/topic/31572-cutting-an-array-in-half/#findComment-146323 Share on other sites More sharing options...
trq Posted December 22, 2006 Share Posted December 22, 2006 Ever heard of the [url=http://php.net/array]manual[/url]? You'll probably get a quicker response when you learn to use it.[code=php:0]array_splice($old,-2,2);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31572-cutting-an-array-in-half/#findComment-146328 Share on other sites More sharing options...
smith.james0 Posted December 22, 2006 Author Share Posted December 22, 2006 The manual is only good if you know what your looking for, if you don't know what it's called it hard to find.Thanks James Quote Link to comment https://forums.phpfreaks.com/topic/31572-cutting-an-array-in-half/#findComment-146329 Share on other sites More sharing options...
trq Posted December 22, 2006 Share Posted December 22, 2006 [quote]if you don't know what it's called it hard to find.[/quote]Ive never used array_slice() or array_split() in my life and I managed to find them.Sheesh. Quote Link to comment https://forums.phpfreaks.com/topic/31572-cutting-an-array-in-half/#findComment-146332 Share on other sites More sharing options...
Daniel0 Posted December 22, 2006 Share Posted December 22, 2006 Then I'll learn you how :)Ok... you have the [url=http://php.net/manual]manual[/url]. Then you ask yourself: What is my problem? And in this case the answer would be: I need to do something with arrays. Then you click on [url=http://php.net/ref.array]Array Functions[/url]. Now you read the function list. And you'll see two functions that might be useful: [quote=PHP Manual][url=http://php.net/array_slice]array_slice[/url] -- Extract a slice of the array[url=http://php.net/array_splice]array_splice[/url] -- Remove a portion of the array and replace it with something else[/quote]Then you go to the entry you think that solves your problem best, read the syntax and possibly examples and then you apply it to your code.If you already know the name of the function you need help with, you just go directly to it's manual entry by typing [tt]php.net/<function name here>[/tt] in the address bar.Always check the manual first. It's an invaluable resource ;) Quote Link to comment https://forums.phpfreaks.com/topic/31572-cutting-an-array-in-half/#findComment-146333 Share on other sites More sharing options...
trq Posted December 22, 2006 Share Posted December 22, 2006 Man... sorry if I seemed at all harsh but yes, as Daniel0 has pointed out, the manual is an invaluable resource. PHP's is one of the most organised manuals Ive seen even with PHP being as bloated as it is. Quote Link to comment https://forums.phpfreaks.com/topic/31572-cutting-an-array-in-half/#findComment-146335 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.