Jump to content

Some array questions


play_

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 =/
Link to comment
https://forums.phpfreaks.com/topic/14155-some-array-questions/
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 )
Link to comment
https://forums.phpfreaks.com/topic/14155-some-array-questions/#findComment-55478
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.
Link to comment
https://forums.phpfreaks.com/topic/14155-some-array-questions/#findComment-55627
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]
Link to comment
https://forums.phpfreaks.com/topic/14155-some-array-questions/#findComment-55740
Share on other sites

Archived

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

×
×
  • Create New...

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.