Welcome to WebmasterWorld Guest from 54.196.153.46

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

foreach array inside array

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

Junior Member

5+ Year Member

joined:Aug 21, 2009
posts: 50
votes: 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 ) )
4:08 am on Nov 10, 2009 (gmt 0)

Senior Member

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

joined:Nov 12, 2005
posts:5966
votes: 0



foreach($outterArray[0] AS $inner)

:)

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

Junior Member

5+ Year Member

joined:Aug 21, 2009
posts: 50
votes: 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?

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

Senior Member

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

joined:Mar 30, 2003
posts:3719
votes: 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

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

Senior Member

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

joined:Nov 28, 2004
posts:7999
votes: 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.

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

Junior Member

5+ Year Member

joined:Aug 21, 2009
posts:50
votes: 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.
10:28 pm on Nov 10, 2009 (gmt 0)

New User

5+ Year Member

joined:Nov 9, 2009
posts: 5
votes: 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.

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

Junior Member

5+ Year Member

joined:Aug 21, 2009
posts:50
votes: 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.
10:37 pm on Nov 10, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 10, 2004
posts:126
votes: 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;

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members