Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-25-2013, 06:23 PM
PM User |
#1
New Coder
Join Date: Nov 2012
Location: United Kingdom
Posts: 29
Thanks: 3
Thanked 0 Times in 0 Posts
foreach help
Hi,
I'm coding a template system for my site with multiple themes and i need to know how to echo things like this:
PHP Code:
$replace = "['data1','Hi there'],['data2','Hello there']" ;
I need to explode or something like that with [] as 1 array with 2 data sections.
It's like array but not.
I know how to replace in the site:
PHP Code:
str_ireplace ( "#data1#" , "value of data1" ); str_ireplace ( "#data2#" , "value of data2" );
Is just need to know how to make [] these in to 1 array.
Can anyone help.
Would be very helpful.
Thanks,
Clawed
01-25-2013, 06:36 PM
PM User |
#2
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,661
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
How'd you end up with a string like that?
If you have a newer version of PHP and the data comes from a completely trusted source, you can use eval:
PHP Code:
$replace = "['data1','Hi there'],['data2','Hello there']" ; eval( "\$a = [$replace];" ); print_r ( $a );
Otherwise you can explode it and then explode each item:
PHP Code:
$aParts = explode ( '],[' , trim ( $replace , '][' )); foreach ( $aParts AS & $part ) { $part = explode ( "','" , trim ( $part , "'" )); } print_r ( $aParts );
Both of which should result in:
Code:
Array
(
[0] => Array
(
[0] => data1
[1] => Hi there
)
[1] => Array
(
[0] => data2
[1] => Hello there
)
)
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer:
http://php.net/manual/en/mysqlinfo.api.choosing.php
01-25-2013, 07:11 PM
PM User |
#3
New Coder
Join Date: Nov 2012
Location: United Kingdom
Posts: 29
Thanks: 3
Thanked 0 Times in 0 Posts
Thanks, i'll try these.
Quote:
Originally Posted by
Fou-Lu
How'd you end up with a string like that?
I'm building a template system & it's all MySQL powered, all data comes from MySQL tables & it echos pages with:
Code:
<div>#data1#</div>
<div>#data2#</div>
And so on.
And after row content there's things that i want it to search and replace for example:
PHP Code:
$content = "<div>#data1#</div> <div>#data2#</div>" ; $string = str_ireplace ( "#data1#" , "Hi" , $content ); $string = str_ireplace ( "#data2#" , "Hi2" , $string ); echo $string ;
Edit;
------------------------------------------------------------------------------------
Nope not how i wanted it, i want so i can use foreach() to set a template param, example:
PHP Code:
$aParts = explode ( '],[' , trim ( $replace , '][' )); foreach ( $aParts AS & $part ) { $part = explode ( "','" , trim ( $part , "'" )); foreach ( $part as $info => $value ) { $template -> setParam ( $info , $value ); } }
So when i do:
PHP Code:
echo $template -> params [ 'data1' ];
It echos "Hi there"
And same with data2:
PHP Code:
echo $template -> params [ 'data2' ];
It echos "Hello there"
Last edited by Clawed; 01-25-2013 at 07:49 PM ..
01-25-2013, 08:03 PM
PM User |
#4
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,661
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Well no, this wouldn't work:
PHP Code:
foreach ( $part as $info => $value ) { $template -> setParam ( $info , $value ); }
That indicates that you have $info as something valid other than an offset. What that would do is set param0 and param1 to the value given. What you appear to want to do is this:
PHP Code:
foreach ( $aParts AS $part ) { $part = explode ( "','" , trim ( $part , "'" )); $template -> setParam ( $part [ 0 ], $part [ 1 ]); }
A quick example of what I think you are doing:
PHP Code:
class T { private $itms ; public function __construct () { $this -> itms = array(); } public function setParam ( $p , $v ) { $this -> itms [ $p ] = $v ; } public function getParam ( $p ) { return @ $this -> itms [ $p ]; } } $t = new T (); $replace = "['data1','Hi there'],['data2','Hello there']" ; $aParts = explode ( '],[' , trim ( $replace , '][' )); foreach ( $aParts AS & $part ) { $part = explode ( "','" , trim ( $part , "'" )); $t -> setParam ( $part [ 0 ], $part [ 1 ]); } print_r ( $t );
Which would give me:
Code:
T Object
(
[itms:private] => Array
(
[data1] => Hi there
[data2] => Hello there
)
)
To which I can access using:
Code:
print $t->getParam('data1');
Edit:
Oh, BTW, this is what makes sense:
PHP Code:
$content = "<div>#data1#</div> <div>#data2#</div>" ; $string = str_ireplace ( "#data1#" , "Hi" , $content ); $string = str_ireplace ( "#data2#" , "Hi2" , $string );
What I don't understand is how you ever ended up with such a bizarre string like:
PHP Code:
$replace = "['data1','Hi there'],['data2','Hello there']" ;
If that comes from a database or something else, it would be better to retain it in its array and use the str_[i]replace and give it the arrays instead.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer:
http://php.net/manual/en/mysqlinfo.api.choosing.php
Last edited by Fou-Lu; 01-25-2013 at 08:05 PM ..
Users who have thanked Fou-Lu for this post:
01-25-2013, 08:14 PM
PM User |
#5
New Coder
Join Date: Nov 2012
Location: United Kingdom
Posts: 29
Thanks: 3
Thanked 0 Times in 0 Posts
Thankyou!
Worked perfect!
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 02:04 AM .
Advertisement
Log in to turn off these ads.