Jump to content

Archived

This topic is now archived and is closed to further replies.

play_

Some array questions

Recommended Posts

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 =/

Share this post


Link to post
Share on other sites
not an expert at this but couldn't you explode the string the resave the array?

Share this post


Link to post
Share on other sites
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 )

Share this post


Link to post
Share on other sites
[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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
HI Barand.
Quick question.

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

Why explode on | ?

Share this post


Link to post
Share on other sites
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]'));

Share this post


Link to post
Share on other sites

×

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.