DragonPrime - LoGD Resource Community
Welcome Guest
  • Good morning, Guest.
    Please log in, or register.
  • December 11, 2017, 05:26:37 AM
Home Forums News Downloads Login Register Advanced Search
* * *
DragonPrime Menu
Login
 
 
Resource Pages
Search

Pages: [1]   Go Down
  Print  
Author Topic: How To Convert Array Into String??  (Read 602 times)
0 Members and 1 Guest are viewing this topic.
TGTarheel
Captain of the Guard
***
Offline Offline

Posts: 179


View Profile
« on: October 30, 2017, 02:39:44 PM »

OK, I looked this up in a PHP manual and it is not terribly helpful.

I found the use of the implode function, but not sure how to express it in the code.

I am having trouble since my PHP version updated, in the riddles module.

So here is original coding
Code:
//split into array of words
$filtstr = explode(" ", trim($string));
foreach ($filtstr as $key => $filtstr1)
$filtstr[$key] = trim($filtstr1);

This is lines 71-74
The call stack happens at line 85
Code:
if (strlen($filtstr) > strlen($filterpost1))

So I need something between line 74 and line 85 that converts the array to a string.

The PHP manual shows this

Code:
$string_version = implode(',', $original_array)

but what do I replace the comma in the ' ' marks with, if anything...
and what do I replace $original_array with?

Am thinking $originalarray gets replaced with $filtstr


Looks to me like I may need to do it in more than one place to change $filtstr, $filtpost nd $fitpre into sttrings instead of arrays.

But not sure.

I have a very basic grasp of what I want to do.

here is a whole code block that is right now problematic. 

I just need to know exactly how to express this within the "implode" function and I am not sure how to do it.  Never used this before.



Code:
//Words to remove
$filterpre = array ( "a", "an", "and", "the", "my", "your", "someones",
"someone's", "someone", "his", "her", "s");
//Letters to take off the end
$filterpost = array ( "s", "ing", "ed");

//split into array of words
$filtstr = explode(" ", trim($string));
foreach ($filtstr as $key => $filtstr1)
$filtstr[$key] = trim($filtstr1);

//pop off word if found in $filterpre
foreach ($filtstr as $key => $filtstr1)
foreach ($filterpre as $filterpre1)
if (!strcasecmp($filtstr1, $filterpre1))
$filtstr[$key] = "";

//trim off common word endings
foreach ($filtstr as $key => $filtstr1)
foreach ($filterpost as $filterpost1)
if (strlen($filtstr) > strlen($filterpost1))
if (!strcasecmp(substr($filtstr1,
-1*strlen($filterpost1)), $filterpost1))
$filtstr[$key] =
substr($filtstr1, 0,
strlen($filterstr)-strlen($filterpost1));





Logged
Astarael|Jack
Codemeister
****
Offline Offline

Posts: 474


Lord Procrastinator and King of Cheap


View Profile
« Reply #1 on: November 02, 2017, 03:17:00 PM »

Little confused at what you're trying to go for here. Upgrading PHP versions shouldn't have broken things to do with arrays, but for your learning, I'll try to explain implode and explode. This probably wont be valid PHP, but it'll give you an idea on what each function does.

Consider this array: $toImplode = ['a, 'b', 'c'] and this string $toExplode = "a quick brown fox"

Now, explode converts a string to an array, whereas implode converts an array to a string. Horrible naming, in my opinion, these commands are better known as string.Split or array.join in many different languages.

If I called explode on $toExplode

Code:
<?php $exploded explode(" "$toExplode); ?>

then explode will look at $toExplode, then every time it sees a " " (a space) in the string, it'll split the string, and put each item into seperate indexes of an array. Thus:

Code:
$exploded == ["a", "quick", "brown", "fox"]

So, if explode 'explodes' a string into an array, what do you think implode does? If you answered turns an array into a string, you're the winner of a brand new caaaar! right!

So, lets take $toImplode, our array with three values, and use implode on it

Code:
<?php $imploded implode(", "$toImplode); ?>

Here, implode will join each element in our list up with whatever string we put, in this case ", ". Therefore the output will be:

Code:
$imploded == "a, b, c"

Hopefully that makes sense!
Logged

TGTarheel
Captain of the Guard
***
Offline Offline

Posts: 179


View Profile
« Reply #2 on: November 02, 2017, 06:11:07 PM »

Little confused at what you're trying to go for here. Upgrading PHP versions shouldn't have broken things to do with arrays, but for your learning, I'll try to explain implode and explode. This probably wont be valid PHP, but it'll give you an idea on what each function does.

Consider this array: $toImplode = ['a, 'b', 'c'] and this string $toExplode = "a quick brown fox"

Now, explode converts a string to an array, whereas implode converts an array to a string. Horrible naming, in my opinion, these commands are better known as string.Split or array.join in many different languages.

If I called explode on $toExplode

Code:
<?php $exploded explode(" "$toExplode); ?>

then explode will look at $toExplode, then every time it sees a " " (a space) in the string, it'll split the string, and put each item into seperate indexes of an array. Thus:

Code:
$exploded == ["a", "quick", "brown", "fox"]

So, if explode 'explodes' a string into an array, what do you think implode does? If you answered turns an array into a string, you're the winner of a brand new caaaar! right!

So, lets take $toImplode, our array with three values, and use implode on it

Code:
<?php $imploded implode(", "$toImplode); ?>

Here, implode will join each element in our list up with whatever string we put, in this case ", ". Therefore the output will be:

Code:
$imploded == "a, b, c"

Hopefully that makes sense!


Actually it does make sense.
My mind wraps better around the concept the way you stated it.

Now if only I can determine the correct parsing of implode and explode.  So I can make valid code.

I have two modules...one that needs an array and calls a string...and the other...vice versa...needs a string and calls an array.

See, i went all the way from PHP 5.3 to 5.7 not by my choice, my hosting company did it to me.

Now...in PHP 5.3 and lower...when an array was passed to something that called for a string, it quietly put a zero in there and just kept going...so you never knew there was a problem.

NOW...you actually get the unsightly call stack!

Anyway...one of the modules is creaturedrop.php (part of the item system)
the other is
riddles.php - the riddling gnome.

Without looking right at them...
I think riddles is the one that wants an array and is being passed a string.
But it could be the other way 'round.

At any rate...I need to convert the one into the other, before it is ever called for in the module, in order to get rid of the horrible call stack.
Logged
Aeolus
Mod God
*****
Offline Offline

Posts: 1794


You're welcome.


View Profile WWW
« Reply #3 on: November 02, 2017, 06:51:33 PM »

Now...in PHP 5.3 and lower...when an array was passed to something that called for a string, it quietly put a zero in there and just kept going...so you never knew there was a problem.

NOW...you actually get the unsightly call stack!

No, that's just bad coding. If it calls for a string, you give it a string. If it calls for an array, you give it an array. You don't pass something to it incorrectly and just hope for the best.

You can check if something's an array by using is_array in a if-statement. If it calls for an array, and what is passed is not an array, you need to exit the function and not continue with it. If it calls for a string, and what is passed is not a string, you need to exit the function and not continue with it.
Logged

TGTarheel
Captain of the Guard
***
Offline Offline

Posts: 179


View Profile
« Reply #4 on: November 02, 2017, 08:02:59 PM »

Now...in PHP 5.3 and lower...when an array was passed to something that called for a string, it quietly put a zero in there and just kept going...so you never knew there was a problem.

NOW...you actually get the unsightly call stack!

No, that's just bad coding. If it calls for a string, you give it a string. If it calls for an array, you give it an array. You don't pass something to it incorrectly and just hope for the best.

You can check if something's an array by using is_array in a if-statement. If it calls for an array, and what is passed is not an array, you need to exit the function and not continue with it. If it calls for a string, and what is passed is not a string, you need to exit the function and not continue with it.

OK...I am TRYIMG to correct;y re=code...what someone else coded...and which actually worked until the upgrade...

Or at least I thought it did since I never got the call stack, right?

The two modules in question....one was coded by Iori, the other, part of core distribution.

I am trying to FIX them...to get them right.

Seems to me the easiest fix...for the moment...would be to convert the existing string into an array and vice versa...just to get things working for now...

THEN...maybe go back an recode them so that the correct thing is called for in the first place,  I guess I am just looking for the quick fix for the moment, so that I can reactivate modules that are currently deactivated.

The one by Iori, for example, is using a debug function that seems to want an array but is getting a string.  I did not code it.
From Iori's module:

Code:
$drop = get_module_objpref("creatures",$args['creatureid'],"drop");

Code:
debug($drop['name']);



Seems the debug function wants an array...but $drop is expressed as a string here.

So I want to take and explode $drop into an array...which ought to clear up the call stack.

now...the older versions of PHP (5.3 and lower) would simply replace with a NULL when passed a string instead of an array.
Thus this was always a problem...but one which you never got alerted to.  The newer version of PHP does alert you by giving the call stack.

This is what I found i my own research on the matter.  (See, I do try to help myself!!)

SO...in the interim, I did a workaround like this:

Code:
if (is_array($drop)){
   
debug($drop['name']);

} else {}

I did that to see if it took care of the call stack and it did.
The thing now functions correctly in that it adds the item for you and does not give you the call stack...like older versions of PHP.

BUT...this is NOT the correct fix and I know it.
I am using a temporary workaround...and trying to understand the function better so that I can code properly...and explode the given string into an array....

Understand now?


Now the other module...I have the reverse problem...it is looking for a string, but being passed an array.
So I want to implode that one into a string.

But neother of them are my original coding...i am trying to fix someone else's improper coding in this case.
Logged
Anharat
Codemeister
****
Offline Offline

Posts: 267



View Profile WWW
« Reply #5 on: November 03, 2017, 10:03:29 AM »

The one by Iori, for example, is using a debug function that seems to want an array but is getting a string.  I did not code it.
From Iori's module:

Code:
$drop = get_module_objpref("creatures",$args['creatureid'],"drop");

Code:
debug($drop['name']);

Seems the debug function wants an array...but $drop is expressed as a string here.

So I want to take and explode $drop into an array...which ought to clear up the call stack.
Well, it probably won't. Not the parameter of debug is the problem, but the called key ('name') of the array. Using explode will not magically give you an array with a key called 'name'.

You should either just remove the call of debug, as it`s pretty unnecessary at that point or, better, check what ever is returned by the module object preference.
Logged

TGTarheel
Captain of the Guard
***
Offline Offline

Posts: 179


View Profile
« Reply #6 on: November 03, 2017, 01:16:24 PM »

OK.  But see why I ask questions?

In either case, my temp workaround basically does what you suggested, it basically causes it to skip that debug function...which I did not think was actually necessary anyway.

I really only know enough about coding to be dangerous.
Logged
Aeolus
Mod God
*****
Offline Offline

Posts: 1794


You're welcome.


View Profile WWW
« Reply #7 on: November 03, 2017, 03:26:44 PM »

I really only know enough about coding to be dangerous.

I'm sure that's meant to sound cool, but I recommend actually taking a full PHP course online before going further. How can you run a site when you don't know how to run it?
Logged

TGTarheel
Captain of the Guard
***
Offline Offline

Posts: 179


View Profile
« Reply #8 on: November 04, 2017, 06:27:03 AM »

I really only know enough about coding to be dangerous.

I'm sure that's meant to sound cool, but I recommend actually taking a full PHP course online before going further. How can you run a site when you don't know how to run it?

I have managed for over eleven years.

I didn't know squat about it when I got started with my site.

I self-taught myself what coding I know.

The problem here is...as soon as you learn how to do things, they change the damn rules.

Things need to STAY THE SAME.

And it is not meant to sound cool.  It means what it means.  I know enough to be truly dangerous...meaning that I could just as easily create something masterful...as royally screw something up.

I do know just enough to be truly dangerous in every sense of the word.  This is me recognizing my own limitations.

Sorry, but we can't all be Aeolus....and be born knowing how to do this stuff.

I am in the process, literally, of saving everything that is currently on my site.  I am TRYING to learn some OOP and I find it a very daunting prospect.  Quite frankly it scares the hell out of me, because it is SO DIFFERENT frm anything I have learned t ths point.

My plan here...is t take a module or two I have currently...and try to rework it in OOP.  See if I can make it work.

THEN I have some idea if I am actually doing it right!

THEN...I can rework all my stuff for it and be ready for it when it DOES happen...because unfortunately, yes, it will happen.


Now...in my day to day life, I am a medical biller/coder.  What this means is that I read the cryptic papers you get back from your insurance company to find out what sorry excuse they have for not paying your claim and try to get them to pay it.

A few years back, my industry went through a major overhaul from the ICD-9 nomenclature to the ICD-10 nomenclature.
This was, unlike previous upgrades...a major overhaul.  Previous permutations of upgrades had just been tweaks to what had already existed...the last, ICD-8 to ICD-9 had been 33 years previous...thus I never worked with ICD-8 and had always worked with ICD-9.

There were a lot of people fighting the change to ICD-10...and I was one who kept telling people that ICD-10 was the Borg...and that resistance was futile.  It was coming...and nothing was gonna stop it.  I was right, of course, though it did get delayed two years.

Seems to me PHP 7 and OOP is the same thing.  The Borg.  Resistance is futile.  But I have to take my time to learn it and learn it right.  And you harping on me and belittling me IS NOT HELPING.

Like I said we can't all be Mighty Aeolus, born knowing how to do this stuff.

I am proceeding about learning...in my own way and at my own pace.  You want to help or not?

It may make you feel good to knock me down and belittle me...which is all you seem to want to do...or you could actually help me in this process.  I'd really rather you helped me.  But it DOES NOT HELP...to have you constantly putting me down.

Could you just TRY to be a little more sensitive to other people's feelings...especially when they are NOT terribly confident of their abilities here?

You could try encouraging me and building me up instead of discouraging me, belittling me and knocking me down.  Try it, it isn't so hard, yaknow??
« Last Edit: November 04, 2017, 06:41:28 AM by TGTarheel » Logged
Aeolus
Mod God
*****
Offline Offline

Posts: 1794


You're welcome.


View Profile WWW
« Reply #9 on: November 04, 2017, 07:11:57 AM »

Do try to keep your drama to a minimum here, nobody here appreciates it. Nor did we ask for your life story.

I wasn't born into it, I self-taught myself at 16, started learning how to code properly in Uni at 18, and have only been decent at it for five years. Don't assume.

Shall we get back on topic?
Logged

TGTarheel
Captain of the Guard
***
Offline Offline

Posts: 179


View Profile
« Reply #10 on: November 04, 2017, 09:11:20 AM »

Do try to keep your drama to a minimum here, nobody here appreciates it. Nor did we ask for your life story.

I wasn't born into it, I self-taught myself at 16, started learning how to code properly in Uni at 18, and have only been decent at it for five years. Don't assume.

Shall we get back on topic?

Please.

The original topic was me asking for help.

You have chosen to belittle and shame me instead of HELPING.

I would love to get back to the original topic.
Logged
Boris735
Mod God
*****
Offline Offline

Posts: 569


View Profile
« Reply #11 on: November 06, 2017, 12:55:04 AM »

So here is original coding
Code:
//split into array of words
$filtstr = explode(" ", trim($string));
foreach ($filtstr as $key => $filtstr1)
$filtstr[$key] = trim($filtstr1);

This is lines 71-74
The call stack happens at line 85
Code:
if (strlen($filtstr) > strlen($filterpost1))

Somewhere along the line you have edited the code incorrectly.  The correct line 85 in riddles.php is
Code:
if (strlen($filtstr1) > strlen($filterpost1))
(note the '1' in 'filtstr1').
Logged
TGTarheel
Captain of the Guard
***
Offline Offline

Posts: 179


View Profile
« Reply #12 on: November 06, 2017, 09:37:17 AM »

So here is original coding
Code:
//split into array of words
$filtstr = explode(" ", trim($string));
foreach ($filtstr as $key => $filtstr1)
$filtstr[$key] = trim($filtstr1);

This is lines 71-74
The call stack happens at line 85
Code:
if (strlen($filtstr) > strlen($filterpost1))

Somewhere along the line you have edited the code incorrectly.  The correct line 85 in riddles.php is
Code:
if (strlen($filtstr1) > strlen($filterpost1))
(note the '1' in 'filtstr1').

Huh.
I never messed with the code on riddles.

I will try it.
Logged
TGTarheel
Captain of the Guard
***
Offline Offline

Posts: 179


View Profile
« Reply #13 on: November 09, 2017, 10:22:00 AM »

So here is original coding
Code:
//split into array of words
$filtstr = explode(" ", trim($string));
foreach ($filtstr as $key => $filtstr1)
$filtstr[$key] = trim($filtstr1);

This is lines 71-74
The call stack happens at line 85
Code:
if (strlen($filtstr) > strlen($filterpost1))

Somewhere along the line you have edited the code incorrectly.  The correct line 85 in riddles.php is
Code:
if (strlen($filtstr1) > strlen($filterpost1))
(note the '1' in 'filtstr1').

Thanks, Boris...this worked!
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  


*
DragonPrime Notices
Play LoGD on Dragonprime

Support Us
No funds raised yet this year
Your help is greatly appreciated!
Recent Topics
DragonPrime LoGD
Who's Online
20 Guests, 0 Users
Home Forums News Downloads Login Register Advanced Search