Hello guys I have seen a source like
$something = $sql['value']
I have searched a lot about it and I found that it's from arrays. But I didnt understand the exact meaning.
For ex ..
$people = [
'Susan' => [
'Age' => 24,
'Phone' => '555-123-4567'
],
'Jack' => [
'Age' => 27,
'Phone' => '555-9876-5432'
]
];
echo $people['Jack']['Age']; // 27
My question is that can we write a code like this ..
if(!empty($people)
$something = $people['a value']
I Just need to know how can we declare a variable and give a value in square brackets ..ny help would be appreciated. Thanks. :)
If you are using $something = $people['a value']
means you are assigning a value of $people
array having an index of a value
So you don't have that and so it will throw you undefined index error.
What you are using is a nested associative array and you have to output using something like
echo $people['Jack']['Age'];
As you wanted a brief example, say you have an array like
$people = array('name'=>'Jack');
Now, when you want to store the name in a variable, you use
$store_name = $people['name'];
echo $store_name; //echoes Jack
try this
$people = array(
'Susan' => array('Age' => 24,'Phone' => '555-123-4567'),
'Jack' => array('Age' => 27,'Phone' => '555-9876-5432')
);
You can use array
and write it like this
$people = array(
'Susan' => array(
'Age' => 24,
'Phone' => '555-123-4567'
),
'Jack' => array(
'Age' => 27,
'Phone' => '555-9876-5432'
)
);
echo $people['Jack']['Age']; // 27
if(!empty($people)
$something = $people['a value']
Square brackets mean index, so $people['a value'] is a value that lays under 'a value' index of $people array.
Square brackets are also used as shortcut for array().. See it here