PHP Array with double values

578

I'm beginning with PHP and I would like to get your help in order to populate array with double value.

I have this little script :

$liststatus=array(
    '-1'=>$langs->trans("Draft"),
    '1'=>$langs->trans("Validated"),
    '0'=>$langs->trans("Resiliated")
);

I would like to add'10'=>$langs->trans("Validated"). In this way, validated search button should displays$liststatus with both values'1' and'10'

I tried :

$liststatus=array(
    '-1'=>$langs->trans("Draft"),
    '1'=>$langs->trans("Validated"),
    '10'=>$langs->trans("Validated"),
    '0'=>$langs->trans("Resiliated")
);

But it displays twice validated button.

So How I can write something like this :

'1' && '10'=>$langs->trans("Validated")

I tried this syntax but it doesn't work.

Thank you :)

385

Answer

Solution:

Array or hash need to have Key and Value.

What you did here is correct as both ket '1' and '10' pointing to value 'Validated'.

$liststatus=array(
   '-1'=>$langs->trans("Draft"),
   '1'=>$langs->trans("Validated"),
   '10'=>$langs->trans("Validated"),
   '0'=>$langs->trans("Resiliated")
);

Or if you really hate see 'Validated' repeated there, you can split it into two arrays.

$listStatusText = [$langs->trans("Draft"),
                   $langs->trans("Validated"),
                   $langs->trans("Resiliated")];
$listStatus=array(
    '-1'=> 0,
    '1' => 1,
    '10'=> 1,
    '0' => 2
);

echo $listStatusText[$listStatus[$x]]; // $x is your key value.

People are also looking for solutions to the problem: PHP deprecated code?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.