PHP string to array keys - php

I have a string that holds the property and key values to accessing a value on an object..
For Example, the string is "property_name[key1][key2][key3]", which relates to $obj->property_name[key1][key2][key3]
I've been trying to parse the string with a regular expression, but all of my attempts have ben in vain.
So far, my regular expression looks like this, but it won't get key2.
^(\w+)\[([^\]]+)\](?:(\[([^\]]+)\])+)
Am I on the right track or is there a better way to do this that I should try?
Thanks.

The regular expression could look like this:
^(\w+)(?:\[(\w+)\])+
Then your matches will contain the property name and the array keys. If the number of keys varies, use this to get the actual value: Using a path to an array item

You should use AJAX instead and send a useful array with objects inside to the server that you can parse with PHP.
var changed_values = [
{
'part_of_form': 'XX',
'field': 2,
'subfield': 3
},
.......
]
Then in PHP you can loop over it:
foreach($_POST['changed_values'] as $changed_value) {
........
}

Related

Laravel Backpack: coordinates field

I have a field named map_box which contains coordinates (see the example below) and cannot be serve as string.
Previously, before Backpack, I use to cast the field to an array 'map_box' => 'array' and the result was the following: note that the coordinates are not string
[
-73.661,
45.589
],
Now, I am trying to achieve the same result, but I keep getting an array of strings. I have tried using the repeatable field with numbers field in it, but the numbers are string. I have also tried to cast the field, then use the text field but this returns an error (as the form is expecting a string and not an array).
My goal is to be able to edit this field in a CRUD controller while being able to serve them with the correct format in my API. Any ideas on how I could achieve the same result as above? I can also use the following format, if this one is possible:
{
lon: -73.661,
lat: 45.589
}
Thanks!
i am not sure if i understand you correctly ... what backpack has to do with this casting?
however if you have something like:
$value='"max_box":{lon: -73.661, lat: 45.589}';
or any string and you want to get float number positive or negative you can use this:
preg_match_all('/-?\d+\.\d+/', $value, $matches);
the $matches[0] will be an array of your numbers

Getting value from string by index

I have a string of data formatted like so:
[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]
The string doesn't have any index/numbers like a regular array would and I'm finding it difficult to extract individual values e.g. with a regular array I could use:
$string[0]["pr_a_w"]
To get the first instance of "pr_a_w" and I could use:
$string[1]["pr_a_w"]
To get the second instance etc.
Is it possible to get single values from this string based on their number?
What you have there is valid JSON (serialized array of objects), so you could use json_decode to translate the serialized data into a native PHP array:
$array = json_decode('[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]',true);
$array will then allow you to do exactly what you stated you'd like to do above.
$array[0]["pr_a_w"]; // will give you 10
$array[1]["pr_a_w"]; // will give you 10
Try like this, No need to access with array index. You will get error if you access wrong index.
$json_arr= json_decode('[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]',true);
foreach($json_arr as $row){
echo $row['pr_a_w']."<br>";
}

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

PHP multidimensional array to JSON

So im trying to figure out the best way to get MySql table data into either a multidimensional PHP array or convert that multidimensional array into a json string.
Essentially what im trying to do is have a php include that returns the JSON string so i can iterate through it. I am needing a single key with multiple values, so im not 100% sure that im headed in the right direction.
I want to assign multiple values to the same key, for example:
[{"key1": "package1", "package2", "package3"}, {"key2": "package1", "package2", "package3", "package4"}]
I think that is not going to work right? Because i dont have any type of index's?
That is not valid JSON. The structure you are looking for would be something like:
[
{"key1": ["package1", "package2", "package3"]},
{"key2": ["package1", "package2", "package3", "package4"}]
^ An array as the value to the key "key1", "key2", etc..
]
At the PHP side, you would need something like:
For every row fetched from MySQL
$arr[$key] = <new array>
for each package:
append package to $arr[$key]
echo out json_encode($arr)
JS arrays have an implicit array keying, starting at index 0. What you've got is a perfectly valid JS array, the equivalent of having written:
var x = []; // create new empty array
x[0] = {"key1": .... }; // first object
x[1] = {"key2": ....} // second object
Note that the contents of your {} sub-objects is NOT valid.
You should never EVER built a JSON string by hand. It's too unreliable and easy to mess up. It's just easier to use a native data structure (php arrays/objects), then json_encode() them. Ditto on the other end of the process - don't decode the string manually. Convert to a native data structure (e.g. json_decode(), JSON.parse()) and then deal with the native structure directly.
essentially, JSON is a transmission format, not a manipulation format.

build SQL statement from json encoded string php

Welcome Stackoverflow nation ! I'm trying to decode json encoded string into SQL statement withing php.
Lets say I've such a json encoded string =>
$j = '{"groupOp":"AND","rules":[{"field":"id","op":"cn","data":"A"},{"field":"i_name","op":"cn","data":"B"}]}';
I want to build SQL WHERE clause (needed for filterToolbar search in jqGrid), something like this => "WHERE id LIKE %A% AND i_name LIKE %B% " and etc.
I've done this =>
$d = json_decode($j);
$filterArray = get_object_vars($d); // makes array
foreach($filterArray as $m_arr_name => $m_arr_key){
// here I can't made up my mind how to continue build SQL statement which I've mentioned above
}
Any ideas how to do that , thanks preliminarily :)
The first problem is you will want to pull out the groupOp operator.
Then, you have an object and inside that you have an array of objects, so you may want to look at the results of filterArray as that won't have the value you want.
Then, when you loop through, you will want to do it with an index so you can just pull the values out in order.
You may want to look at this question to see how you can get data out of the array:
json decode in php
And here is another question that may be helpful for you:
How to decode a JSON String with several objects in PHP?
There is an answer with an implementation for the server-side php code here
Correction: I had to unescape double-quotes in the 'filters' parameter to get it working:
$filters = str_replace('\"','"' ,$_POST['filters']);

Categories