user3733831
user3733831

Reputation: 2926

Get array values into variables

I am having an array looks like this,

$document[$doc_id][]= array( $user_id
                           , $doc_type
                           , $id_number
                           , $issuer
                           , $expiry_date
                           , $doc_name
                           , $doc_path
                           , $name
                          );

Its out put look like this:

Array ( 
    [15] => Array (

        [0] => Array (
                        [0] => 14
                        [1] => 1
                        [2] => 3242424
                        [3] => 1
                        [4] => 2016-01-26
                        [5] => 3242424_1452091784.jpg
                        [6] => ../documents/
                        [7] => USA Driving Licence
                    )   

        [1] => Array (
                        [0] => 15
                        [1] => 1
                        [2] => 3242424788
                        [3] => 1
                        [4] => 2016-01-26
                        [5] => 3242424_1452045645.jpg
                        [6] => ../documents/
                        [7] => US Driving Licence
                    )

    )
)

Using this array I need to get each array values into php variables. Like this:

------------------
$user_id1
$doc_type1
$id_number1
$issuer1
$expiry_date1
$document_name1
$document_path1
$name1
------------------
$user_id2
$doc_type2
$id_number2
$issuer2
$expiry_date2
$document_name2
$document_path2
$name2

Can anybody tell me how to do it in php? Hope somebody may help me out. Thank you.

Upvotes: 1

Views: 142

Answers (5)

Micke
Micke

Reputation: 609

Why you'd want to do this, I don't know. But in PHP, you can use variable variables. For instance, say you have a string 'my_var', then you can create the variable $my_var like this:

$my_string = 'my_var';
$$my_string = 'some value'; // same as: $my_var = 'some_value';

As for your particular request, here's how you'd extract the array values into variables (assuming you just want the ones at $doc_id):

// Replace the [...] with the proper variable names (obviously)...
$names = ['user_id', 'doc_type', 'id_number', [...], 'name'];

foreach ( $document[$doc_id] as $id => $values )
{
    $id++; // 0 becomes 1, 1 becomes 2, 2 becomes...
    foreach ( $values as $key => $value )
    {
        // Assume that this is the first value in your array above, 
        // then $key is 0, $id is 1 and $value is 14, so the following 
        // code is the equivalent of typing in: $user_id1 = 14;
        ${$names[$key].$id} = $value;
    }
}

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94642

Ok, so instead of amending the array after the fact, build it as you want it when you are processing the results of your query, like so. Of course you can call the entries in the array anything you want.

while($stmt->fetch()) {
    if(!isset($document[$doc_id])) {
        $document[$doc_id][]= array( 'userid'          => $user_id, 
                                     'veryfy_doc_type' => $veryfy_doc_type, 
                                     'id_number'       => $id_number, 
                                     'document_issue'  => $document_issuer, 
                                     'expiry_date'     => $expiry_date, 
                                     'document_name'   => $document_name, 
                                     'document_path'   => $document_path, 
                                     'doc_name'        => $doc_name);
            }          
        }

Then its just a case of runnig a foreach loop over your array, but now you can use sensible names for things

foreach ($document[$doc_id] as $id => $doc ) {
    echo "id = $id veryfy_doc_type = " . $doc['veryfy_doc_type'];
}

Upvotes: 0

Matei Zero
Matei Zero

Reputation: 56

i don't have enough reputation to comment on your first post but:

Yes I created that array by the result from mysql select – user3733831

you can use mysqli_fetch_assoc function which will return you an associative array (meaning that the result set keys will be names after your DB columns)

so assuming your DB table looks like this

user_id | doc_type | id_number | issuer | expiry_date | doc_path | name

your result set will look like this

Array (

    [0] => Array (
          [user_id] => 14
          [doc_type] => 1
          [id_number] => 3242424
          [issuer] => 1
          [expiry_date] => 2016-01-26
          [doc_name] => 3242424_1452091784.jpg
          [doc_path] => ../documents/
          [name] => USA Driving Licence
    )   

)

EDIT

Ok...looked over your code so instead of

$stmt->bind_param('i', $edit);  
$stmt->execute();    
$stmt->store_result(); 
$stmt->bind_result(
    $doc_id
    , $user_id
    , $veryfy_doc_type
    , $id_number
    , $document_issuer
    , $expiry_date
    , $document_name
    , $document_path
    , $doc_name
);
$document = array();
while($stmt->fetch())

you can use

$stmt->bind_param("i", $edit);
$stmt->execute();

/* instead of bind_result: */
$result = $stmt->get_result();

/* now you can fetch the results into an array - NICE */
while ($row = $result->fetch_assoc()) {
    //do something
    // here you can access each result as $row[<something>]
    // ex: echo $row['user_id'] will print 14
}

Upvotes: 1

Nijraj Gelani
Nijraj Gelani

Reputation: 1466

Here, this is exactly what you want. And also, what you shouldn't be doing.

<?php
$variables = array("user_id", "doc_type", "id_number", "issuer", "expiry_date", "doc_name", "doc_path", "name");
$i = 1;
foreach($your_array[0] as $array){
    for($j = 0; $j < count($variables); $j++){
        ${$variables[$j] . $i} = $array[$j];
    }
    $i++;
}
?>

Upvotes: 1

aynber
aynber

Reputation: 23011

Store the variable names as the array keys, then you can reference the named key instead of a number, or iterate through the array and print out the key itself.

$document[$doc_id][]= array( 'user_id' => $user_id
                       , 'doc_type' => $doc_type
                       , 'id_number' => $id_number
                       , 'issuer' => $issuer
                       , 'expiry_date' => $expiry_date
                       , 'doc_name' => $doc_name
                       , 'doc_path' => $doc_path
                       , 'name' => $name
                      );

Upvotes: -1

Related Questions