homepage Welcome to WebmasterWorld Guest from 54.145.172.149
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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

5+ Year Member



 
Msg#: 4022167 posted 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

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4022167 posted 4:08 am on Nov 10, 2009 (gmt 0)


foreach($outterArray[0] AS $inner)

:)

jman11

5+ Year Member



 
Msg#: 4022167 posted 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

WebmasterWorld Senior Member dreamcatcher us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4022167 posted 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

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4022167 posted 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

5+ Year Member



 
Msg#: 4022167 posted 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

5+ Year Member



 
Msg#: 4022167 posted 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

5+ Year Member



 
Msg#: 4022167 posted 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

10+ Year Member



 
Msg#: 4022167 posted 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