Welcome to WebmasterWorld Guest from 54.167.29.212

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

foreach array inside array

     

jman11

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

5+ Year Member



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

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

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




foreach($outterArray[0] AS $inner)

:)

jman11

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

5+ Year Member



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

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

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



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

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

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



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

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

5+ Year Member



@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

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

5+ Year Member



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

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

5+ Year Member



@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:37 pm on Nov 10, 2009 (gmt 0)

10+ Year Member




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;

 

Featured Threads

Hot Threads This Week

Hot Threads This Month