munich
munich

Reputation: 530

How can I extract values from an array into a variable?

I'm using the following code to print my array:

//SQL Query
$query = "SELECT wp_arf_entry_values.entry_value, wp_arf_entry_values.id FROM wp_arf_entry_values WHERE entry_id=1";

//Execute the SQL query and return records
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result)){
     $results[] = $row;
}

//Print array in human readable form
print("<pre>".print_r($results,true)."</pre>");

This is the output:

Array
(
    [0] => Array
        (
            [entry_value] => John
            [id] => 1
        )

    [1] => Array
        (
            [entry_value] => Doe
            [id] => 2
        )

    [2] => Array
        (
            [entry_value] => 19
            [id] => 3
        )

    [3] => Array
        (
            [entry_value] => Male
            [id] => 4
        )
)

I'm having a hard time to figure out how I can extract the array's values into variables, like this (not actual code, just an example):

$fName = 'entry_value' that has ID = 1
$lName = 'entry_value' that has ID = 2
$age = 'entry_value' that has ID = 3
$genre = 'entry_value' that has ID = 4

I've researched here for the past hour but I can't find an answer that helps me. What's a feasible way to do this?

Upvotes: 0

Views: 82

Answers (4)

Akif Hussain Sayyed
Akif Hussain Sayyed

Reputation: 596

you have to get it through loop, the code is given below

$fName = $lName = $age = $genre = "";
foreach($results AS $result){
switch($result['id']){
case 1:
$fName = $result['entry_value'];
break;
case 2:
$lName = $result['entry_value'];
break;
case 3:
$age = $result['entry_value'];
break;
case 4:
$genre = $result['entry_value'];
break;
}
}
echo $fName." ".$lName." is ".$genre." and ".$age."years old."; 

Upvotes: 0

Eduardo Russo
Eduardo Russo

Reputation: 4219

It's not the most elegant solution, but works:

 foreach($results as $result){
    switch ($result['id']){
        case 1:
            $fname = $result['entry_value'];
            break;

        case 2:
            $lName  = $result['entry_value'];
            break;

        case 3:
            $age  = $result['entry_value'];
            break;
        case 4:
            $genre  = $result['entry_value'];
            break;
    }
    echo "fname = $fname | lName = $lName | age = $age | genre = $genre\n" ;
}

This way, it doesn't matter the result order... so, it the $results array doesn't come in the right order, it will work anyway.

Upvotes: 0

alu
alu

Reputation: 456

<?php
// set dummy data
$data = [
    ['entry_value' => 'John', 'id' => 1],
    ['entry_value' => 'Doe', 'id' => 2],
    ['entry_value' => '19', 'id' => 3],
    ['entry_value' => 'Male', 'id' => 4],
];

$data = array_column($data, 'entry_value', 'id');
$fName = $data[1];
$lName = $data[2];
$age = $data[3];
$genre = $data[4];

Upvotes: 1

Puneet
Puneet

Reputation: 603

Assumption:id values are unique in your db

change your code to

$results = array();
while($row = mysql_fetch_assoc($result)){
     $results[$row['id']] = $row['entry_value'];
}

//Print array in human readable form
print("<pre>".print_r($results,true)."</pre>");

And you can have your values as:

$fName = $results['1']
$lName = $results['2']
$age   = $results['3']
$genre = $results['4']

Upvotes: 3

Related Questions