0

My query looks like this:

SELECT * FROM tblName WHERE btn_group_id = 1;

Also I have array of languages $langs = array(5,7,19) the same in btn_lang_id

$btn = Array
(
    [0] => Array
        (
            [btn_id] => 1
            [btn_group_id] => 1
            [btn_lang_id] => 5
            [btn_text] => aaa1
        )

    [1] => Array
        (
            [btn_id] => 2
            [btn_group_id] => 1
            [btn_lang_id] => 7
            [btn_text] => bbb2
        )

    [2] => Array
        (
            [btn_id] => 3
            [btn_group_id] => 1
            [btn_lang_id] => 19
            [btn_text] => ccc3
        )
)

My question is how I can use this array to echo data by using btn_lang_id

foreach ($langs as $lang){
echo $btn[$lang['lang_id']]['btn_text'];
}

I want the above 3 arrays of $btn accessed by language id not by index 0,1,2. I there any way?

1
  • You can store the array serialized or in json format, and then retrieve it from the DB and unserialize or json decode. That way you can store the data and preserve the array structure. Commented Sep 12, 2016 at 8:17

1 Answer 1

1

You can create an array with the "btn_lang_id" as a key using the following code:

<?php
$langs = array(5,7,19);

$btn = array(
    '0' => array(
            'btn_id' => 1,
            'btn_group_id' => 1,
            'btn_lang_id' => 5,
            'btn_text' => 'aaa1',
        ),
    '1' => array(
            'btn_id' => 2,
            'btn_group_id' => 1,
            'btn_lang_id' => 7,
            'btn_text' => 'bbb2',
        ),
    '2' => array(
            'btn_id' => 3,
            'btn_group_id' => 1,
            'btn_lang_id' => 19,
            'btn_text' => 'ccc3',
        ),
);

$customArr = array();   
foreach($langs as $key=>$value){
  $customArr[$value] = $btn[$key];
}
print_R($customArr);
?>
Sign up to request clarification or add additional context in comments.

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.