1

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.

8
  • 2
    Why don't you want to keep this in an array? Commented Jan 6, 2016 at 15:57
  • 3
    You have a perfectly good array there, why do you need to move data to scalar variables and double your memory requirement for no good reason Commented Jan 6, 2016 at 15:59
  • Here I want to use these two group of values in two different places in my page. Thats why I am looking for such a solution. Commented Jan 6, 2016 at 16:01
  • Quesstion: Did you create that array or are you just dealing with an array you have no control over? Is it the result of a query on a database? Commented Jan 6, 2016 at 16:01
  • 1
    Then use a mysql function that returns the data bu name and not just with an index. Show us your query and the code that unloads the result set into the array Commented Jan 6, 2016 at 16:09

5 Answers 5

1

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++;
}
?>
Sign up to request clarification or add additional context in comments.

6 Comments

@Pamblam what about now?
should be $array[$i][$j].
@Pamblam it's an array of single array containing arrays, right?
oh, i didn't notice the [0] ..you're right. although a better answer would be to remove the [0] and change $array as i suggested, that way it will loop ALL the arrays an not just one of them..
@Pamblam well then I might as well delete it... it's no different from your answer which I didn't notice at first :D
|
1

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
}

Comments

0

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'];
}

3 Comments

Thank you for your answer, Can you show me how to use these values? for example, how to use veryfy_doc_type 1 and 2?
I tried it something like this foreach ($document[$doc_id] as $id => $doc ) { $id_number = $doc['id_number']; } echo "<br>$id_number";
I finally got a solution like this - pastebin.com/5SPJLzZK your idea would be welcome. Thank you.
0

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;
    }
}

Comments

-1

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
                      );

2 Comments

Not sure this is actually helping much
It's probably not quite what he wants, but it would depend on what he's doing with the array afterwards.

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.