how to link array element to a string - php

The following instruction -
$this->menuTablesArr[$i]."_tablelink_attrs"
works correctly in older versions but in PHP5 it gives this error:
error type 8 Array to string conversion
Could anyone help ? many thanks

In your question isn't clear, what you mean by "works correctly". What is the outcome, when it works correctly?
I will assume, that this error is due to concatenating array and string. It can solve PHP function implode. This function can have one or two parameters.
In your case you can use it e.g. with only one parameter
implode($this->menuTablesArr[$i])."_tablelink_attrs"
If you want to use some glue between items of array, you can use two parameters. More documentation about this function is in the link above.

Related

Adding characters to str_replace in PHP

I have a php file with thread ($tid) and post ($pid) ids defined and I'm looking to use str_replace to combine them and create my desired output (below) with &p= added:
Desired output:
$tid&p=$pid
The closest I've got is doing this:
$tid=$t1;
$pid=$p1;
$tid=str_replace("$tid","$tid"."$pid",$tid);
The result is:
$tid$pid
I may need to use a function (not sure which if so?) in the str_replace to support the &p= being added, as trying to add it in the quotes directly doesn't seem to work resulting only in database errors.
Edit #1: I tried doing the following based on the comments thus far:
$tid="";
$tid.="$t1";
$tid.="$p1";
$tid=$tid;
That results in the same as my previous example above:
$tid$pid
As soon as I add another with the &p= I get a database error:
$tid.="&p=";
So my question now is how to add the &p= to my Edit #1 example above correctly?
I'm attempting to understand the question a little bit. You have a couple options that are alluded to in the comments above. You could, for instance (where '1', '2', and '3' are replaced by the desired values):
<?php
$tid=1;
$pid=2;
$amp=3;
$tid=str_replace("$tid","$tid".'$amp;p='."$pid",$tid);
echo $tid;
?>
Here you can just explicitly state in the 'replace' argument of str_replace that the missing string should be part of the replacement string.
or you could also simply use the a concatenation operator to generate the string you desire which is probably simpler given what (little) I know of the surrounding code/use-case:
<?php
$tid=1;
$pid=2;
$amp=3;
echo "$tid".'$amp;p='."$pid";
?>
The reason I'd recommend the latter, is due to the fact that it's less resource intensive to actually produce the desired result which appears to just be a string. str_replace() searches the "haystack" or subject string using the search string and then must replace each of the found instances with the replace string.

How to get first part of a URL

can anyone help me to piece together the puzzled I'm facing. Lets say I have url's
/some-work/
/store/bread/alloy/
and in both of these cases I wanna fetch the first part from it. i.e. some-work, store.
Now I've used parse_url(get_permalink()) to get the array of the url and then fetch the path index of the array to fetch the above string. Now I have also checked strstr PHP function, but I am unable to make it work. Can anyone help?
You can use explode, array_filter and current function like as
$url = "http://www.example.com/some-work/";
$extracted = array_filter(explode("/",parse_url($url,PHP_URL_PATH)));
echo current($extracted);//some-work
Demo

REALbasic array in an array

I am trying to write a soap parameter in REALbasic.
I need to add an array within another array similar to this in php:
$params = array(array(
'sku' => 'some sku'
));
so I can pass this:
$result = $client->call($session, 'catalog_product.list', $params);
I have
dim aArgs (0,1) as String
dim aParmas (0,1) as String
aArgs(0,0)="sku"
aArgs(0,1)="some sku"
aParmas(0,1)= aArgs
But receive a "Type mismatch error. Expected String, but got String(,)"
How can I do this.
Thanks
First off, the line
aParmas(0,1)= aArgs
is wrong because you assign an array (which is in aArgs) to a single element of aParmas. And since those single elements hold a String, you try to assign an array to a single string here, hence the error message.
But I think you're looking at this from the wrong end. You need to start with figuring out what parameters you need to send to the session function you want to call.
That means: You need to find the REALbasic function for $client->call. Once you know which function that is, look at the parameters that function expects. I doubt it expects a two-dimensional array for the "params". Once you know what to pass here, let us know if you still cannot figure out how to get it working.
An explanation of multidimensional arrays in REALbasic is here
The short answer is that you can't have a PHP-like array of arrays. You need to wrap your array in a class and make the class behave like an array.
Any reason you're using REALbasic? If it's cross-platform you're after, python is ALWAYS a better choice

Return Array Element without Creating Variable

I feel stupid for asking this cause it seems so basic but it's really bugging me.
I have a method that returns an array with a single element. I just want it to return the element not wrapped in an array. I tried this.
return $f->getValue()[0];
and it gives an error but if I save it to a variable, it works fine.
$v = $f->getValue();
return $v[0];
I can't figure it out....
It's available only since PHP 5.4: http://codepad.viper-7.com/VHOW0o
What you are trying to do, is called array dereferencing, and is only possible in PHP as of version 5.4 (if you scroll up a few lines in the documentation article I linked to, you'll see it mentioned).
Use reset().
<?php return reset( $f->getValue() ); ?>
Edit: reset() is probably superior to current() as it also makes sure that the internal pointer is reset, despite it not making much difference if the array only contains one element.
As far as I know since you are returning an array you only can get an array. You can instead save the array to a variable in the class (accessible by $f->myArray) and then return just the string portion. Or the other option is to do what your second example is and return the array and retrieve the string from it.
have you tried this
<?php
return array_shift(array_values($array));
?>
Get the first element of an array

How to Work with PostgreSQL Function Output in PHP

I have a postgresql (V 8.4) function that returns SETOF a custom type. An example of the string output from this function is the following 4 rows:
(2,"CD,100"," ","2010-09-08 14:07:59",New,0,,,,,,"2010-09-06 16:51:51","2010-09-07 16:51:57",)
(5,CD101,asdf,"2010-08-08 14:12:00",Suspended-Screen,1,10000,,,,,,,)
(4,DNR100,asdf,"2010-09-08 14:10:31",Suspended-Investgate,0,,,,,,"2010-09-06 16:51:51","2010-09-07 16:51:57",)
(3,MNSCU100," ","2010-09-08 14:09:07",Active,0,,,,,,,,)
I need to work with this data in PHP and I'm trying to figure out the best way to work with it. What I would love is if there was a way for postgresql to return this like a table where columns represent each value within a record rather than as a comma-separated string.
Is this possible? If not, what is the best way to work with this comma-separated string of values in PHP?
I've see this post (Convert PostgreSQL array to PHP array) and can use the function mentioned there but I wanted to ask if anyone has other ideas or suggestions.
Thanks,
Bart
There's str_getcsv() which'll parse a string as CSV data and return an array of the individual fields
Yep, its real easy, just change the way you are calling the function.
Instead of
SELECT my_srf(parm1);
Do either:
SELECT * FROM my_srf(parm1);
SELECT (my_srf(parm1)).*;
You'll even get the column names out this way.

Categories