How do I parse stdClass - php? - php

When I do var_dump, I get the following result:
stdClass::__set_state(array(
'0' =>
stdClass::__set_state(array(
'field' => 'name',
'value' => 'John Smith',
'description' => 'User full name',
))
))
I need the field, value & description in separate string vars.
How do I do this?
I tried:
$field = $obj->field;
$field = $obj->0->field;
$field = $obj->'0'->field;
$field = $obj[0]->field;
$field = $obj['0']->field;
Nothing works!?
Thanks

You can use the curly brace syntax to traverse the object, like so:
$obj->{'0'}->field;
$obj->{'0'}->value
$obj->{'0'}->description

Related

PHP store foreach query results in array in specific format

I'm trying to store the results of a query in an arrow, but need to do so in a specific format (I think).
The required format I need the results to be in is as follows:
'screenshots' => 'Plugin Screenshots',
This is what I have so far, along with my failed attempt to store the results:
$my_fake_pages = array();
$args = array(
'parent' => 8,
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
$my_fake_pages[] = $category->slug . '=>' . $category->slug;
}
What troubles me most is I do not understand how I would go about getting the => in there, as without the inverted commas dreamweaver throws up PHP errors.
This is what it would look like normally:
$my_fake_pages = array(
'installation' => 'Plugin Installation',
'usage' => 'Plugin Usage',
'screenshots' => 'Plugin Screenshots',
'changelog' => 'Plugin Changelog',
'feedbacks' => 'Users\' Feedbacks',
);
Any help is appreciated.
foreach($categories as $category) {
$my_fake_pages[$category->slug] = $category->slug;
}
print_r($my_fake_pages);
The notation 'something' => 'another thing' means it's a key/value pair. So all you need to do is change your assignment line to something like $my_fake_pages[$category->slug] = $category->name.
See http://php.net/manual/en/language.types.array.php#language.types.array.examples

passing query fields n rows to array

I'm trying to fetch all rows to an array variable
array that i want is like this
$data1 = array('fields'=>array(
array(
'id' => 1,
'nama_file' => "sunset.jpg",
'judul' => "Sunset",
'isi' => "Matahari terbenam indah sekali",
),
array(
'id' => 2,
'nama_file' => "water_lilies.jpg",
'judul' => "Bunga Lilly",
'isi' => "Bunga lilly air sangat indah",
),)
And I've done this:
$q = $this->db->query('select id, nama_file, judul, isi from tfoto where dihapus ="T" ');
$data1=array('fields');
foreach($q->result() as $row) {
$data1['fields']=array('id'=>$row->id,'nama_file'=>$row->nama_file,'judul'=>$row->judul, 'isi'=>$row->isi);
}
test output:
<?php
foreach($fields as $field){
echo $field['nama_file'];
.
.
.
};?>
and I got Message: Illegal string offset 'nama_file';'judul'; etc.
I am a newbie to MySQL/PHP, so forgive me if this is a very basic question. I tried looking all over but I could not find an answer to it.
This line:
$data1['fields']=array('id'=>$row->id,'nama_file'=>$row->nama_file,'judul'=>$row->judul, 'isi'=>$row->isi);
Should be:
$data1['fields'][] = array('id'=>$row->id,'nama_file'=>$row->nama_file,'judul'=>$row->judul, 'isi'=>$row->isi);
Because you have to append new arrays to $data1 and not replacing it.

Cannot apply $pull/$pullAll modifier to non-array

I have a table in mongo with the following structure:
array(
'Subscriber' => array(
'0' => 'Name 1',
'1' => 'Name 1',
'2' => 'Name 2',
)
)
I am using the following code to remove an item from subscriber:
$setData["subscriber.1"]=1;
$result = $this->mongo->pages->update($condition1, array('$unset' => $setData), array('safe' => true));
$result = $this->mongo->pages->update($condition1, array('$pull' => array('subscriber' => null)), array('safe' => true));
It works fine but gives Mongo cursor Error:Cannot apply $pull/$pullAll modifier to non-array
Anyone know why?
I am guessing that it is because of:
$result = $this->mongo->pages->update($condition1, array('$pull' => array('Pinned' => null)), array('safe' => true));
You have no Pinned in your document and it most definitely is not an array.
That is essentially what this error says: Pinned is not an array.
You did used single quotes (''). PHP will ignore the variables which are within single quotes. You have to remove the single quotes or replace them by double quotes.
This should work:
$setData["subscriber.1"]=1;
$result = $this->mongo->pages->update($condition1, array($unset => $setData), array('safe' => true));
$result = $this->mongo->pages->update($condition1, array($pull => array('Pinned' => null)), array('safe' => true));

Get value from custom category attribute

I am trying to get the value from a custom category attribute in Magento. The attribute is a select field and is been made with the install script below:
$this->startSetup();
$this->addAttribute('catalog_category', 'category_categorycolor', array(
'group' => 'General Information',
'input' => 'select',
'type' => 'varchar',
'label' => 'Categorie kleur',
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'option' => array (
'value' => array('yellow' => array('Geel'),
'purple' => array('Paars'),
'blue' => array('Blauw'),
'red' => array('Rood'),
'orange' => array('Oranje'),
'green' => array('Groen'),
'darkblue' => array('Donkerblauw'),
'lightgreen' => array('Lichtgroen'),
)
),
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
Unfortunately only getting numbers and not text value. I use this line to retrieve the value:
<?php $_category_categorycolor = $_category->getData('category_categorycolor'); if($_category_categorycolor): ?> <?php echo $_category_categorycolor; ?> <?php endif; ?>
Can someone help me?
Something like this:
$category_id = '10';
$attribute_code = 'category_categorycolor';
$category = Mage::getModel('catalog/category')->load($category_id);
echo $category->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($category);
The sollution is pretty messy (the only one I know of).
$opt = array(); // will contain all options in a $key => $value manner
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_categorycolor');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
foreach ($options as $o) {
$opt[$o['value']] = $o['label'];
}
}
$categoryColorId = $_category->getData('category_categorycolor');
$categoryColorLabel = $opt[$categoryColorId];
// if you have problems, do a Zend_Debug::dump($opt);
// - it should contain an array of all the options you added
Didn't test it out, let me know if it works or not.
PS: can't reply to your comment, not sure why. What does $opt contain ?
The numbers you are getting back are the id's of each value in the dropdown. You have to load the dropdown values too.
See the following page. It helped me understand this.
http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/

How to store a variable in an array?

I have data from a form submission stored in a variable called $post_data. When I do print_r($post_data); I get the following array:
Array
(
[element_3] => John Doe
[element_2] => john#example.com
[element_14] => City
[element_15] => Country
[form_id] => 1
[submit] => Submit
);
I want to store some of the fields in another array to pass to another script. Will my code below work? If not, how do I fix it?
$submitted_data = array(
'Fields' => array(
array(
'Key' => 'Name',
'Value' => $post_data['element_3']
)
array(
'Key' => 'Email',
'Value' => $post_data['element_2']
)
)
)
Also, a PHP noob question, do I need another comma (,) in between the Name and Email array?
Thanks!
I'm not exactly sure why you would want to do this, but depending on the field name you can consider using loops to help automate the entire process.
$field_map = array(
'element_3' => 'Name',
'element_2' => 'E-mail',
'element_14' => 'City',
'element_15' => 'Country'
);
$submitted_data = array('fields' => array());
foreach ( $field_map as $key => $label)
{
$submitted_data['fields'][] = array(
'key' => $key, // e.g. element_2
'label' => $label, // e.g. E-mail
'value' => $post_data[$key] // e.g. john#example.com
);
}
This separates the storage/mapping of key/label pairs from the part which processes it, making it easier to maintain and modify in the future.
Another way might be (depending on how "fixed" the second script is, if you can alter it).
$submitted_data['Name']=$post_data['element_3'];
$submitted_data['Email']=$post_data['element_2'];
To get a result more like the one in your question:
$submitted_data['Fields']['0']['Key']='Name';
$submitted_data['Fields']['0']['Value']=$post_data['element_3'];
$submitted_data['Fields']['1']['Key']='Email';
$submitted_data['Fields']['1']['Value']=$post_data['element_2'];

Categories