0

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?

4
  • why not store at the time of looping?the value will be same for every data? Commented Oct 30, 2014 at 6:35
  • what user php version? Commented Oct 30, 2014 at 6:37
  • 2
    Firstly do not use mysql_* it has depreciated , its better to use mysqli or pdo. And secondly how do you map ID = 1 is for fName and similar for others. Commented Oct 30, 2014 at 6:38
  • @turtle I'm changing to mysqli. Thank you. Commented Oct 30, 2014 at 6:44

4 Answers 4

3

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']
Sign up to request clarification or add additional context in comments.

1 Comment

This worked exactly how I needed. Thank you. I can't assign the best answer yet.
1
<?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];

Comments

0

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.

Comments

0

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."; 

Comments

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.