homepage Welcome to WebmasterWorld Guest from 54.161.214.221
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
foreach array inside array
jman11




msg:4022169
 3:50 am on Nov 10, 2009 (gmt 0)

hi i want to use foreach on an array, except the array is inside another array and i have no clue why. anyways, is there a way to like take out the inner array and put it by iteself so i can use it with foreach();


Array ( [0] => Array ( [0] => 50 ) )

 

eelixduppy




msg:4022173
 4:08 am on Nov 10, 2009 (gmt 0)


foreach($outterArray[0] AS $inner)

:)

jman11




msg:4022184
 4:38 am on Nov 10, 2009 (gmt 0)

yeh i just got that thanks.

this is sort of off topic, but is it possible to do this:


foreach($new[0] as $key => $value) {
$replace = '<font style="font-size: '.$value.'">';
preg_replace("/abc/",$replace,$contents);
}

its just showing up blank for me, i have several spots where its "abc" in $contents, and i have the values i want to replace it with in an array ($new), how do i replace each one with the value from the array?

dreamcatcher




msg:4022245
 7:41 am on Nov 10, 2009 (gmt 0)

You need to assign the regex to a variable or echo it:

foreach($new[0] as $key => $value) {
$replace = '<font style="font-size: '.$value.'">';
$replace = preg_replace("/abc/",$replace,$contents);
echo $replace;
}

dc

rocknbil




msg:4022562
 6:57 pm on Nov 10, 2009 (gmt 0)

Also off topic, don't do this

$replace = '<font style="font-size: '.$value.'">';

<font> has been deprecated for years, instead do

$replace = '<span style="font-size: '.$value.'">';

or better yet,

$element_type='<p>';

$replace = '<' . $element_type . ' class="'.$value.'">';

It may require a little more work, but when it comes time to resolve cross browser issues (and others,) it will really help you.

jman11




msg:4022669
 9:53 pm on Nov 10, 2009 (gmt 0)

@dreamcatcher - when i echo inside the foreach, it echos the $contents 3 times, i only want it to echo once with the 3 sections replaced only.

PHPcoder




msg:4022708
 10:28 pm on Nov 10, 2009 (gmt 0)

It will echo it for as many values as the array holds.

EG: array(1, 2, 3) would echo 3 times array(1, 2, 3, 4) would echo 4 times and so on.

jman11




msg:4022709
 10:32 pm on Nov 10, 2009 (gmt 0)

@PHPcoder, obviously... thats what i just said. anyone know how to echo an array variable and have it go like, first time replace = $array[0], second replace = $array[1], etc.

JohnCanyon




msg:4022712
 10:37 pm on Nov 10, 2009 (gmt 0)


this is sort of off topic, but is it possible to do this:

foreach($new[0] as $key => $value) {
$replace = '<font style="font-size: '.$value.'">';
preg_replace("/abc/",$replace,$contents);
}

What benefit are you trying to get here? By looping through the way you are, the occurances of abc are going to be replaced by the first iteration of your foreach loop.

If you are trying to get the value of the first element within a multi-dimensional array, this would serve you better.

$replace = '<font style="font-size: '. current($new[0]).'">'; //we don't care what the key is.
$contents = str_replace("abc", $replace, $contents);
echo $contents;

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved