2

I have no idea How to convert a single quote array into array;

FOR Example:-

I have

$Array = '["ID" => 9, "Value" => [40,15,20,25,30]]';
print_r($Array);

Then it is showing like string not an array

 ["ID" => 9, "Value" => [40,15,20,25,30]]

so how to convert this into array

like the result will show like this

Array
(
    [ID] => 9
    [Value] => Array
        (
            [0] => 40
            [1] => 15
            [2] => 20
            [3] => 25
            [4] => 30
        )

)

May be you have why i am putting array in single quote but this is not i am putting.

I Getting an array from DB after group_concat in mysql

This is the array

Array
(
    [0] => Array
        (
            [GPN] => A
            [PKGID] => PKG01
            [Output] => ["ID" => 9, "Value" => [40,15,20,25,30]]
        )
)

Here the Output is element coming like it's a string

Nikhil Vaghela
  • 2,088
  • 2
  • 15
  • 30
User97798
  • 634
  • 1
  • 5
  • 24
  • yes you right but have a look on the `[Output] => ["ID" => 9, "Value" => [40,15,20,25,30]]` – User97798 Aug 04 '16 at 13:10
  • 1
    Maybe the correct solution to this is to look at the query as that is what is creating this issue. _Fix the problem, not the symptom_ – RiggsFolly Aug 04 '16 at 13:10
  • try to put at least a json in the database. – M. I. Aug 04 '16 at 14:21
  • if you need to use group_concat take a look at this: http://stackoverflow.com/questions/12511933/how-create-json-format-with-group-concat-mysql – M. I. Aug 04 '16 at 14:44

5 Answers5

2

You can do this but it might be dangerous:

$Array = '["ID" => 9, "Value" => [40,15,20,25,30]]';

eval('$Array = '.$Array.';'); // Could be dangerous

echo '<pre>';
print_r($Array);
echo '</pre>';

In my example above, $Array is assumed to be data coming from your database such as [Output] => ["ID" => 9, "Value" => [40,15,20,25,30]]. Since it is coming from the database then that means the possibility exists for the DB data to be malicious and eval() will gladly run anything you give it.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • $Array = '["ID" => 9, "Value" => "exec("rm -rf /")"]'; -> just an example of what could happen. – M. I. Aug 04 '16 at 14:09
  • @M.I. Close, but I think your exact example would just cause a fatal error. – MonkeyZeus Aug 04 '16 at 14:22
  • @M.I. The `group_concat` which OP mentioned would have to output `["ID" => 9, "Value" => [40,15,20,25,30]];exec("rm -rf /")` or just `exec("rm -rf /")` in order for the `eval()` injection to work. – MonkeyZeus Aug 04 '16 at 14:25
  • yes, that is correct, but will still throw an parse exception. eval is not ok to be used like this, he should try to get something else from the database, like a json, found another question on SO : http://stackoverflow.com/questions/12511933/how-create-json-format-with-group-concat-mysql – M. I. Aug 04 '16 at 14:43
  • @M.I. I agree but unfortunately OP is not revealing any other details about how their `group_concat` logic is even producing valid syntax for setting an array (assuming PHP >= 5.4); quite frankly, that's impressive. Also, I am sure that something is possible with a join or a PHP loop but without additional details I would be pulling 15 pounds of crap out of a 5 pound bag. – MonkeyZeus Aug 04 '16 at 15:12
1

You can use eval to parse the string as php code like this:

$s = '["ID" => 9, "Value" => [40,15,20,25,30]]';
eval('$a = ' . $s . ';');
print_r($a);

This will work only with php 5.4 and up

CAUTION

If this string contains data from user then it is not safe to use eval.

Alternative solution

If you are the one who populate the database records, i suggests to serialize Output values as json, using json_encode, before insert to database. Then you can use json_decode to access data as array.

Whiteulver
  • 828
  • 7
  • 12
-1
<?php

$str   = '["ID" => 9, "Value" => [40,15,20,25,30]]';
$array = eval('return ' . $str . ';');
var_export($array);

Output:

array (
  'ID' => 9,
  'Value' => 
  array (
    0 => 40,
    1 => 15,
    2 => 20,
    3 => 25,
    4 => 30,
  ),
)
Progrock
  • 7,373
  • 1
  • 19
  • 25
-1

So, your question is basically how to convert string representation of php array to php array. I don't know any tools for this, however you can use eval function.

$arr = []; eval('$arr=' . $Array . ';'); // $arr contains php array

However this is not recommendable, because you can execute arbitrary code which undermines security of your application. Before using it you should make sure that $Array does not contain any malicious code and all strings are escaped.

If you run your application as a root user then it is even more dangerous. For example: $Array = 'true?print_r($_SERVER['REQUEST_TIME']):0' The above code will print all the server data of your application.

-1

I think this solution.

$array = [
        "ID" => 9,
        "Value" => [40,15,20,25,30],
         ];

print_r($array);

Output :

 /* Array ( [ID] => 9 [Value] => Array ( [0] => 40 [1] => 15 [2] => 20
 [3]=> 25 [4] => 30 ) ) */

for other array :

 $array =[

         "0" => [
                "GPN" => A,
                "PKGID" => PKG01,
                "Output" => 
                           [
                            "ID" => 9,
                             "Value" => [40,15,20,25,30]
                           ]
                ]
         ];
print_r($array);

And output :

/*Array ( [0] => Array ( [GPN] => A [PKGID] => PKG01 [Output] => Array ( [ID] => 9 [Value] => Array ( [0] => 40 [1] => 15 [2] => 20 [3] => 25 [4] => 30 ) ) ) ) */

advise : the name "PKG01".

psr-1 : constants MUST be declared in all upper case

source : psr-1

CodeNoob
  • 345
  • 4
  • 17
  • he has a string to convert it in an array, how does this help? – M. I. Aug 04 '16 at 13:54
  • He wants to pass an array to an array of arrays – CodeNoob Aug 04 '16 at 14:01
  • 1
    Not really, he needs to transform a string to array and after that to pass it to another array. He is asking how to make this '["ID" => 9, "Value" => [40,15,20,25,30]]' into an array. – M. I. Aug 04 '16 at 14:19