php get the value of attribute name from array - php

I have an array stored in my database. So when I try : print_r($arrayname),
It showed the result like this:
Array
(
[0] => Color,Processor
[attribute_name] => Color,Processor
)
I want to get the values of [attribute_name] => Color,Processor .
So far I made the foreach loop like this :
foreach ($arraylist as $name) {
echo $name['attribute_name];
}
But it showing the result like cc. So can someone kindly tell me how to get the values from database?

Actually you don't need to use foreach
You can access it like this.
echo $arraylist["attribute_name"];

Please try this
foreach ($arraylist as $key => $name) {
if($key == 'attribute_name')
echo $name;
}

Related

php get the text into an array list

I have an array in the database. When I used print_r($variable_name), I got the array like this
Array
(
[0] => Array
(
[attribute_name] => Disk space,Color,Processor
)
)
So to get the value of attribute_name I tried this
foreach($attributes_name as $key=>$val) {
echo $val['attribute_name'];
}
Here I got the result like Disk space,Color,Processor. But I want the result should come like a list
<li>Disk Space</li>
<li>Color</li>
<li>Processor</li>
So can someone tell me how to do this?
Try this :
<?php
$arr = array(array("attribute_name" => "Disk space,Color,Processor"));
foreach($arr as $val) {
$resultArr = explode(",",$val['attribute_name']);
foreach($resultArr as $value){
echo "<li>".$value."</li>";
// Add id in li
echo "<li id='".str_replace(" ","_", strtolower($value))."'>".$value."</li>";
}
}
?>

Codeigniter Active Record bug?

I just don't understand, I have code like this:
$this->ci->db->select('liked_posts, liked_comments');
$q = $this->ci->db->get_where('users_fav', array('usr_id' => $this->_usrId));
$result = $q->result_array();
And when I, as always, tried to put it into foreach loop.. it's just didn't work.. Because in $result I've got and array where 2 more arrays where stored (table fields)
so to work in foreach loop it would look like this:
foreach($result[0] as $value)
not:
foreach($result as $value)
And I was looking for my mistake very long.. Maybe I really did something wrong... Or is it a bug?
edit:
print_r($result);
Array
(
[0] => Array
(
[liked_posts] => a:0:{}
[liked_comments] => a:0:{}
)
)
edit2:
But shoudn't it be like this:
Array
(
[liked_posts] => a:0:{}
[liked_comments] => a:0:{}
)
?? Or I'm starting to go crazy???
edit3:
My bad... I realized now... I'm just going crazy.. too much work done today... better go sleep :D Sorry guys
so you can do this
foreach($result as $value)
{
echo $value['fav_posts'];
}
no problem with that.
When using $result = $q->result_array(); you will get a multidimentional array as you have now.:
foreach( $resuls as $key => $each ){
echo "result : ".$each['column_name'];
}
but if you have just a single row fetched you would likely use $result = $q->row_array(); which will return a single dimentional array. And you can directly use like this:
echo $results['column_name'];

how to echo multidimension array using php

I have the following array, I need to display the names on a form.
How can I do this via foreach() loop?
What I am trying is:
Array
(
[0] => Array
(
[to_user_name] => John
)
[1] => Array
(
[to_user_name] => Mike
)
)
foreach( $myArray as $subArray ) {
echo $subArray["to_user_name"];
}
It's not clear how you want to use those values in your form, but just echo the values wherever you'd need them, e.g.,
foreach( $myArray as $subArray ) {
echo "<input type=\"text\" name=\"user_name\" value=\"" . $subArray["to_user_name"] . "\">";
}
I was treating it like a single array and then i realized it is a multidimensino array and the following worked, i hope this helps someone else too
foreach ($messages->getMessage('recipient_names') as $section => $items ){
foreach ($items as $key => $value){
echo "$value, ";
}
}
to see the content you can use
print_r($your_array);
For developing purpose you need to use for/foreach loop
foreach($your_array as $array_temp)
{
foreach($array_temp as $item)
{
echo $item;
}
}

Grab value of array, in array, with for each

I have the following array:
print_r($all_projects);
--------------------------------
Array (
[0] => Array (
[pro_id] => 7
[0] => 7
)
[1] => Array (
[pro_id] => 20
[0] => 20
)
)
How do I grab each pro_id in a foreach loop?
I can find quite a bit information online on how to create an array, and how to grab the key and value from an array. But I have difficulty grabbing the value of a key in an array, in an array.
I currently have this for each:
foreach ($all_projects as $project => $project_id){
echo $project_id;
}
Which in turn returns the following:
Array
Array
This makes sense to me, but how do I go deeper in the array?
foreach($all_projects as $project) {
echo $project['pro_id'];
}
try:
foreach ($all_projects as $project => $project_id){
echo $project_id['pro_id'];
}
or even cleaner to read :
foreach ($all_projects as $project){
echo $project['pro_id'];
}
foreach($all_projects as $KEY => $VALUE) {
echo "KEY: $KEY - PRO_ID: {$VALUE['pro_id']}";
}
In your case, you'd want:
foreach ($all_projects as $project_id => $project) {
echo $project_id . " = " . $project['pro_id'];
}
Now with PHP5.5, you have an easy way to do it:
foreach ($all_projects as list($id))
echo $id;
And the old way:
foreach ($all_projects as $project)
echo $project['pro_id'];
Hope it'll help you!
If we're looping through the $all_projects with foreach loop this way,
foreach ($all_projects as $key => $project) {
echo($key);
}
then $key will actually be 0, 1, etc., not 7 or 20 -- as you might intended this to be -- and $project will be the content of the project.
In your case, I presume that the "project id" you desired are stored inside the "project" array itself, so as other suggested, you should write something like
foreach($all_projects as $project) { // omitting the $key part since you don't need it
echo($project['pro_id']);
}
this will print the actual "project id", which is the pro_id that you want.
If you were to improve this code, you might want to restructure your $all_projects this way
$all_projects = array();
$all_project[7] = $some_project; // your first project with id 7
$all_project[20] = $some_other_project; // your second project with id 20
then you will be able to use your original code to loop through:
foreach($all_projects as $project_id => $project) {
echo($project_id);
}
with $project_id be 7, 20, etc., and $project be the content of your project.
Hope this answers your question!

getting information out of an array with key/value pairs

I have the following snippet of code that is creating the following array...
while ($stmt->fetch()) {
foreach($row as $key => $val) {
$x[$key] = $val;
}
$results[] = $x;
}
Results in the follow array:
Array ( [0] => Array ( [cap_login] => master [cap_pword] => B-a411dc195b1f04e638565e5479b1880956011badb73361ca ) )
Basically I want to extract the cap_login and cap_pword values for testing. For some reason I can't get it!
I've tried this kind of thing:
echo $results[$cap_login];
but I get the error
Undefined variable: cap_login
Can someone put me right here?
Thanks.
cap_login is in an array within $results so you would have to do $results[0]['cap_login']
You would have to do the following:
echo $x[0]['cap_login'] . '<br />';
echo $x[0]['cap_pword'];
The reson $results[$cap_login] wont work is because there isn't a variable called $cap_login, there is a string called cap login. In addition, there isn't a key in $results called $cap_login. There is a value in $results called 'cap_login'

Categories