1

I am struggling to find the solution for converting string type array into actual array. I have a database column where it stores plain-text exactly like below :

array ( 'mc_gross' => '100.00', 'auth_exp' => '18:35:24 Apr 08, 2022 PDT',  
 'protection_eligibility' => 'Eligible', 'payer_id' => 'J4RS8Q76ZEKZ2', 'payment_date' =>                        '17:35:24 Mar 10, 2022 PST', 'payment_status' => 'Pending', 'charset' => 'UTF-8',  
 'first_name' => 'John', 'transaction_entity' => 'auth', 'quantity' => '4',) 

I need to access 'quantity' value from this column. I have tried to fetch database and then tried to access it through the code below:

while($resp=$db->fetch($response)){
                foreach ($resp as $key => $value) {
                    $gateway[] = $resp[$key];
                    }
                }
            }
        echo $gateway[0]['quantity'];

I am getting 'a' after this command. it is the first character of string as you see.

The output of echo $gateway[0] is :

array ( 'mc_gross' => '100.00', 'auth_exp' => '18:35:24 Apr 08, 2022 PDT',  
 'protection_eligibility' => 'Eligible', 'payer_id' => 'J4RS8Q76ZEKZ2', 'payment_date' =>                        '17:35:24 Mar 10, 2022 PST', 'payment_status' => 'Pending', 'charset' => 'UTF-8',  
 'first_name' => 'John', 'transaction_entity' => 'auth', 'quantity' => '4',) 

I have tried to access by : $gateway[0][1] (just for example) but this only shows me : a

As you see it is saved as string, I don't know how to handle this. Any help would be highly appreciated.

3
  • How you printed this output (what you use to print ) from $gateway? Have you tried $gateway[0][7]? Commented Mar 11, 2022 at 4:22
  • Why you trying to access value of the key => ( $dateway[0][11] ) which obviously does not exist in this array? Commented Mar 11, 2022 at 4:25
  • I mean that I have tried everything but the only result I get is the first symbol of the value $gateway[0] . It looks like an array but it is only a string and therefore accessing as an array not works at all. it only returns me the symbols. Is there any way to convert it to an array ? Commented Mar 11, 2022 at 9:54

2 Answers 2

1
[
    'mc_gross' => '100.00',
    'auth_exp' => '18:35:24 Apr 08, 2022 PDT',
    'protection_eligibility' => 'Eligible',
    'payer_id' => 'J4RS8Q76ZEKZ2',
    'payment_date' => '17:35:24 Mar 10, 2022 PST',
    'payment_status' => 'Pending',
    'charset' => 'UTF-8',
    'first_name' => 'John',
    'transaction_entity' => 'auth',
    'quantity' => '4',
] // this is $gateway[0]

Simply use:

$gateway[0]['quantity']

to get the quantity value

Sign up to request clarification or add additional context in comments.

7 Comments

Unfortunately, that does not work also. Because the result of that command is : 'a' . As I said before $garteway[0] contains long string looks like array but it is not. It is a string and needs to be serialized in my opinion.
I answered according to your question. Please add required code to your question so we can help you better.
I have edited my first question, please have a look.
Did you try to run json_decode on your "string like array"
To be honest I have tried but that did not work also, but might be I am doing something wrong?
|
0

The 'string type arrays' are so-called associative arrays.

The result of $gateway[0] is such an associative array. More precise your array $gateway contains another associative array at its first index [0]. Therefore using

 $gateway[0]['quantity']

would be your way to go.


As a bonus tip you should check the array documentation of php: https://www.php.net/manual/en/language.types.array.php

2 Comments

Unfortunately, that does not work also. Because the result of that command is : 'a' . As I said before $garteway[0] contains long string looks like array but it is not. It is a string and needs to be serialized in my opinion.
What's the output you get of gettype($gateway[0]) and gettype($gateway[0]['quantity']) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.