Jump to content


Photo

Some array questions


  • Please log in to reply
7 replies to this topic

#1 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 10 July 2006 - 09:40 AM

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 =/
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 10 July 2006 - 09:46 AM

not an expert at this but couldn't you explode the string the resave the array?

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 10 July 2006 - 11:39 AM

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 put

Array ( [0] => my name [1] => my name is [2] => my name is redarrow [3] => i like php freaks )
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 10 July 2006 - 02:07 PM

not an expert at this but couldn't you explode the string the resave the array?


I dont think if i did that, they would be inserted at the end of of the array
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#5 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 10 July 2006 - 04:18 PM

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.

$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];
};


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.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 10 July 2006 - 07:24 PM

try

<?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>';
?>

gives -->[pre]Array
(
    0 => hi
    1 => really long string i need
    2 => to split into two parts
    3 => next element
    4 => etc
)

[/pre]

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 11 July 2006 - 04:55 AM

HI Barand.
Quick question.

"$tmp = explode('|', wordwrap($text[$i], ceil($l/2), '|'));"

Why explode on | ?
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 11 July 2006 - 08:03 PM

Because that's the character I used in the wordwrap function to split the text.

$tmp = explode('|', wordwrap($text[$i], ceil($l/2), '|'));
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users