0

I have initialize an array with keys IDs of something. How can I get all this keys as a list-stack-array for example, after of that initiaze ?

this is my code:

        $sql = "SELECT id,name FROM Something WHERE id IN ( SELECT fk_Id FROM Something_Meta WHERE fk_pId=$data) ORDER BY name ASC";

        if($stmt = $mysqli->prepare($sql))
        {
            $stmt->execute();
            $stmt->bind_result($id,$name);
            while ($stmt->fetch())
            {
                echo "<hr>";
                $a_tmp_array[$id] = $name;
                echo "<hr>";
            }
        }
        print_r($a_tmp_array);

After of that I want something like this:

for (;;;)
$keyArray = key_from($a_tmp_array)

or

foreach (array_keys($a_tmp_array) as array_keys($a_tmp_array))

$keyArray[$i++] = array_keys($a_tmp_array);

the last one it throughs me a fatal error due to array_keys() function is a returning fuctions as foreach is going crazy :)

Also, print_r($a_tmp_array); returns the following: Array ( [2] => some1 [3] => some2 [4] => some99 [1] => etc [14] => foo )

1 Answer 1

1

You want to use something like array_keys. So your code would be:

$keyarray = array_keys($a_tmp_array);

You don't need a loop as array_keys creates the array for you.

Sign up to request clarification or add additional context in comments.

1 Comment

give me a sec to check it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.