1

I have the following array Array ( ['6'] => mobile ) while i printing my array with print_r function of php. I need to separate the index and value of the array in two different variables.

The actual code is

$ques=$_POST['selector'];
$count = count($ques);
for($i=0; $i < $count; $i++)
{
    print_r ($ques)."\n";
} 

How can i do this?

3 Answers 3

3

You could do this by following way:

foreach ($ques as $key => $value) {
    echo $key . ' = ' . $value;
} 
Sign up to request clarification or add additional context in comments.

4 Comments

I want to print just the id, but instead of 6 it is printing like '6' . How can i remove single quotations ? note that, I need to assign 6 in the integer form.
Try to use echo (int) $key . ' = ' . $value;.
every time its printing 0.
You can replace the quotes by using str_replace function like this: echo str_replace("'", '', $key);
2
<?php print_r( array_keys( $array ) ); ?>
<?php print_r( array_values( $array ) ); ?>

6 Comments

can i use it like this <?php $id = print_r(array_keys($array)); ?>
sure, just pass TRUE as a second argument for the print_r function
You couldn't know until you try running the code. If not working try debugging it.
after another guess, Are you sure you want the result of the print_r ? or you're just interested in the id of the first element of the array?
I want to print just the id, but instead of 6 it is printing like '6' . How can i remove single quotations ? note that, I need to assign 6 in the integer form.
|
0
foreach ($ques as $key => $value) {
    $variable1 = $variable1 . ',' . $key;
    $variable2 = $variable1 . ',' . $value;
} 

You can then separate the commas with substr if those two variables have more than 1 value.

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.