play_ Posted July 10, 2006 Share Posted July 10, 2006 Few questions.1) Is there a function to "flatten" a multidimensional array?2) is there a way to split an array element into two, giving each its own key?so for example, if i have:$a = array( 0 => "hi", 1 => "really long string i need to split into two parts", 2 => "next element", 3 => "etc");How could i make it so that it turns into:$a = array( 0 => "hi", 1 => "really long string i", 2 => "need to split into two parts", 3 => "next element", 4 => "etc");Can it be done?Using php4 here. so str_split() is not an option for #2 =/ Quote Link to comment https://forums.phpfreaks.com/topic/14155-some-array-questions/ Share on other sites More sharing options...
AV1611 Posted July 10, 2006 Share Posted July 10, 2006 not an expert at this but couldn't you explode the string the resave the array? Quote Link to comment https://forums.phpfreaks.com/topic/14155-some-array-questions/#findComment-55441 Share on other sites More sharing options...
redarrow Posted July 10, 2006 Share Posted July 10, 2006 Have a look at this might be ok dont know tried<?$a=array("my name","my name is","my name is redarrow and i like php freaks");$a[3]=substr($a[2],24);$a[2]=eregi_replace("and i like php freaks","",$a[2]);foreach($a AS $item[]);print_r($item);?>out putArray ( [0] => my name [1] => my name is [2] => my name is redarrow [3] => i like php freaks ) Quote Link to comment https://forums.phpfreaks.com/topic/14155-some-array-questions/#findComment-55478 Share on other sites More sharing options...
play_ Posted July 10, 2006 Author Share Posted July 10, 2006 [quote author=AV1611 link=topic=100029.msg394307#msg394307 date=1152524803]not an expert at this but couldn't you explode the string the resave the array?[/quote]I dont think if i did that, they would be inserted at the end of of the array Quote Link to comment https://forums.phpfreaks.com/topic/14155-some-array-questions/#findComment-55544 Share on other sites More sharing options...
play_ Posted July 10, 2006 Author Share Posted July 10, 2006 Thanks red. however, you lost the word "and" in there.the original array contanis "and i like php freaks"but "and" isn't in the final array.Anyhow, here' what im trying to do pretty much.[code] $all = array();// $text is an exploded array with tons of elements inside,for( $i = 0; $i < count($text); $i++) { if (strlen($text[$i]) >= 70) { $text[$i] = chunk_split($text[$i], 70, "\n"); } $all[] = $text[$i];};[/code]So, what it's doing is, it loops through each element in the $text array, and adds them to the $all array. In the middle of this, it checks every element's length. If an element is larger than 70 characters, i split it with a "\n".However, what i want to do is split it in 2 and add both halves to the $all array. Quote Link to comment https://forums.phpfreaks.com/topic/14155-some-array-questions/#findComment-55627 Share on other sites More sharing options...
Barand Posted July 10, 2006 Share Posted July 10, 2006 try[code]<?php$text = array( 0 => "hi", 1 => "really long string i need to split into two parts", 2 => "next element", 3 => "etc" );$all = array();// $text is an exploded array with tons of elements inside,for( $i = 0; $i < count($text); $i++) { if (($l = strlen($text[$i])) > 20) { $tmp = explode('|', wordwrap($text[$i], ceil($l/2), '|')); $all = array_merge($all, $tmp); } else $all[] = $text[$i];}echo '<pre>', print_r($all, true), '</pre>';?>[/code]gives -->[pre]Array( 0 => hi 1 => really long string i need 2 => to split into two parts 3 => next element 4 => etc)[/pre] Quote Link to comment https://forums.phpfreaks.com/topic/14155-some-array-questions/#findComment-55740 Share on other sites More sharing options...
play_ Posted July 11, 2006 Author Share Posted July 11, 2006 HI Barand.Quick question."$tmp = explode('|', wordwrap($text[$i], ceil($l/2), '|'));"Why explode on | ? Quote Link to comment https://forums.phpfreaks.com/topic/14155-some-array-questions/#findComment-56024 Share on other sites More sharing options...
Barand Posted July 11, 2006 Share Posted July 11, 2006 Because that's the character I used in the wordwrap function to split the text.$tmp = explode('[color=red]|[/color]', wordwrap($text[$i], ceil($l/2), '[color=red]|[/color]')); Quote Link to comment https://forums.phpfreaks.com/topic/14155-some-array-questions/#findComment-56430 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.