2

I would like to store value from while loop but not in multidimensional way :

 $movies_id = array();

 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $id = $row['id'];
                $title_id = $row['title_id'];
                    $movies_id[] = [$id => $title_id];
     }

print_r($movies_id);

I am not getting the way, how to do it, if i just write $movies_id = [$id => $title_id]; it only shows the last value.

Thank you by advance.

2
  • $movies_id[] will add a element to array, it doesn't work? Commented Nov 26, 2014 at 8:26
  • which version of php are you using?? Commented Nov 26, 2014 at 8:29

4 Answers 4

8

You need to set value properly.

            $movies_id = array();

            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $id = $row['id'];
                $title_id = $row['title_id'];
                    $movies_id[$id] = $title_id; // The proper way
                    //$movies_id[] = [$id => $title_id];
            }

            print_r($movies_id);

The more readable code would be:

            $movies_id = array();

            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $movies_id[$row['id']] = $row['title_id'];
            }
            print_r($movies_id);

And would result in

array (
    1 => 'Title 1',
    2 => 'Title 2',
    4 => 'Title 4',
    8 => 'Title 8',
)

And key-less soulution would be:

            $movies_id = array();

            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $movies_id[] = $row['title_id'];
            }
            print_r($movies_id);

And would result in

array (
    0 => 'Title 1',
    1 => 'Title 2',
    2 => 'Title 4',
    3 => 'Title 8',
)
Sign up to request clarification or add additional context in comments.

4 Comments

Indeed are. But may be usable later if wanted to retrive ID of a movie.
there are a lot of ways but saying "you need" seems to be not optionals ;) that's for my downvote
Thanks, better be clear on what is optional and what it isn't =) so upvoted
Indeed. [$id => $title_id] led me to this mistake. By need i more meant he needs to do this that way if he wnats to store IDs as keys.
0

I may have misunderstood what you're aiming for, but this might do it:

replace

$movies_id[] = [$id => $title_id];

with

 $movies_id[$id] = $title_id;

Comments

0

Your code would not work in case of PHP version less than 5.4. In that case you can use the following.

        $movies_id = array();

        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $id = $row['id'];
            $title_id = $row['title_id'];
            $movies_id[] = array($id => $title_id);
        }

        print_r($movies_id);

Comments

0

Instead of this line

$movies_id[] = [$id => $title_id];

Use the following

$movies_id[$id] = $title_id;

6 Comments

It will work, but it will make multidimensional array. In the question user don't want multidimensional.
But the user does not want it to be multidimensional array. Have you read the question properly?
You have changed your comment just a minute ago.
See, SO is not for debating on a particular thing, It's about providing a beneficial solution to the question.
I changed the word "comment" to "question".
|

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.