0
$gender = array ('boy', 'girl', 'trangender');
$shopping_data = array(
            'boy' => array(
                'accessory' => array('belt','wallet','watch'),
                'age' => array(20,45,50)
             ),
             'girl'=> array(
                'accessory' => array('hair-clip','wallet','watch'),
                'age' => array(30,40)
             )
);

how to get value "hair-clip" by using $gender?

4
  • I'm not sure how they're related, can you explain? Commented Nov 21, 2018 at 7:06
  • 1
    You can get it like this by Hard coding echo $shopping_data[$gender[1]]['accessory'][0]; Commented Nov 21, 2018 at 7:08
  • $gender is an array. using which index of $gender? Commented Nov 21, 2018 at 7:10
  • 1
    You've asked this question yesterday. Still no logic explanation what's the connection between arrays. Commented Nov 21, 2018 at 7:18

2 Answers 2

1

Can you merge the two arrays at all?

I was thinking something along the lines of:

$gender = array_merge(array_flip($gender), $shopping_data);

echo '<pre>';
var_dump($gender['girl']['accessory'][0]);
echo '</pre>';
exit;
Sign up to request clarification or add additional context in comments.

Comments

0

You mean loop and echo the items to each gender?

$gender = array ('boy', 'girl', 'trangender');
$shopping_data = array(
            'boy' => array(
                'accessory' => array('belt','wallet','watch'),
                'age' => array(20,45,50)
             ),
             'girl'=> array(
                'accessory' => array('hair-clip','wallet','watch'),
                'age' => array(30,40)
             )
);


foreach($gender as $gen){ // loop genders
    if(isset($shopping_data[$gen])){ // is gender in shopping_data
        echo $gen .":\n";
        foreach($shopping_data[$gen]["accessory"] as $acc){ // loop the accessories to the gender
            echo $acc . "\n";
        }
        echo "\n\n";
    }
}

output:

boy:
belt
wallet
watch


girl:
hair-clip
wallet
watch

https://3v4l.org/hjCrV

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.