Jump to content

Correct syntax to list an array


zigojacko

Recommended Posts

I need to list an array (comma separated) and the data I need is available in the var_dump. I just don't know how to write the syntax to display the array list.

 

From within: var_dump ($product->getMediaGalleryImages());

 

I can see multiple instances of: ["url"]=> string(89) "https://www.example.com/media/catalog/product/x/x/x-image.jpg"
 
What do I need to write in order to list all the ["url"]
 
Thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/297326-correct-syntax-to-list-an-array/
Share on other sites

Did you try the manual?

 

Of course. It's the first place I look.

 

I couldn't resolve it after trying things from the manual...

 

Syntactically, I don't think this is right:-

$array = array($product->getMediaGallery());

So, the following returns NULL:-

var_dump ($array["url"]);

In case you've missed the obvious, I'm not really sure what I am doing with this hence why I have come here for help.

If you couldn't use the manual to figure out this problem, how the heck are you writing all your other php code?

 

$str = '';
foreach($array as $item)
$str .= $item .', ';
$str = trim($str,',');
echo $str;

 

OR the simpler approach:

 

$str = implode(',',$array);
echo $str;

 

I think you should spend some time reading the manual and getting familiar with the language you have chosen to use. :)

You indicated in your first post that $product->getMediaGallery() was already returning an array, so you do not need to wrap it in another array.

 

 

Just to clarify, does $product->getMediaGallery() contain an array? If so, you should be able to use a foreach loop (or loops) to get all the 'url' parts.

 

 

I don't think I explained well (or correctly). I think $product->getMediaGallery() is an object. The output of var_dump ($product->getMediaGalleryImages()); is below:-

object(Varien_Data_Collection)#402 (13) { ["_items":protected]=> array(5) { [226]=> object(Varien_Object)#401 (7) { ["_data":protected]=> array(12) { ["value_id"]=> string(3) "226" ["file"]=> string(35) "/k/i/kimono-sleeve-top-black_1_.jpg" ["product_id"]=> string(2) "51" ["label"]=> string(55) "silk print bamboo kimono sleeve zip top " ["position"]=> string(1) "1" ["disabled"]=> string(1) "0" ["label_default"]=> string(55) "silk print bamboo kimono sleeve zip top " ["position_default"]=> string(1) "1" ["disabled_default"]=> string(1) "0" ["url"]=> string(89) "https://www.example.com/media/catalog/product/k/i/kimono-sleeve-top-black_1_.jpg" ["id"]=> string(3) "226" ["path"]=> string(102) "/var/www/vhosts/example.com/httpdocs/media/catalog/product/k/i/kimono-sleeve-top-black_1_.jpg" } ["_hasDataChanges":protected]=> bool(false) ["_origData":protected]=> NULL ["_idFieldName":protected]=> NULL ["_isDeleted":protected]=> bool(false) ["_oldFieldsMap":protected]=> array(0) { } ["_syncFieldsMap":protected]=> array(0) { } } [227]=> object(Varien_Object)#374 (7) { ["_data":protected]=> array(12) { ["value_id"]=> string(3) "227" ["file"]=> string(35) "/k/i/kimono-sleeve-top-black_2_.jpg" ["product_id"]=> string(2) "51" ["label"]=> string(60) "silk print bamboo kimono sleeve zip top front" ["position"]=> string(1) "2" ["disabled"]=> string(1) "0" ["label_default"]=> string(60) "silk print bamboo kimono sleeve zip top front" ["position_default"]=> string(1) "2" ["disabled_default"]=> string(1) "0" ["url"]=> string(89) "https://www.example.com/media/catalog/product/k/i/kimono-sleeve-top-black_2_.jpg" ["id"]=> string(3) "227" ["path"]=> string(102) "/var/www/vhosts/example.com/httpdocs/media/catalog/product/k/i/kimono-sleeve-top-black_2_.jpg" } ["_hasDataChanges":protected]=> bool(false) ["_origData":protected]=> NULL ["_idFieldName":protected]=> NULL ["_isDeleted":protected]=> bool(false) ["_oldFieldsMap":protected]=> array(0) { } ["_syncFieldsMap":protected]=> array(0) { } } [225]=> object(Varien_Object)#375 (7) { ["_data":protected]=> array(12) { ["value_id"]=> string(3) "225" ["file"]=> string(35) "/k/i/kimono-sleeve-top-black_3_.jpg" ["product_id"]=> string(2) "51" ["label"]=> string(62) "silk print bamboo kimono sleeve zip top quarter" ["position"]=> string(1) "3" ["disabled"]=> string(1) "0" ["label_default"]=> string(62) "silk print bamboo kimono sleeve zip top quarter" ["position_default"]=> string(1) "3" ["disabled_default"]=> string(1) "0" ["url"]=> string(89) "https://www.example.com/media/catalog/product/k/i/kimono-sleeve-top-black_3_.jpg" ["id"]=> string(3) "225" ["path"]=> string(102) "/var/www/vhosts/example.com/httpdocs/media/catalog/product/k/i/kimono-sleeve-top-black_3_.jpg" } ["_hasDataChanges":protected]=> bool(false) ["_origData":protected]=> NULL ["_idFieldName":protected]=> NULL ["_isDeleted":protected]=> bool(false) ["_oldFieldsMap":protected]=> array(0) { } ["_syncFieldsMap":protected]=> array(0) { } } [228]=> object(Varien_Object)#319 (7) { ["_data":protected]=> array(12) { ["value_id"]=> string(3) "228" ["file"]=> string(35) "/k/i/kimono-sleeve-top-black_4_.jpg" ["product_id"]=> string(2) "51" ["label"]=> string(59) "silk print bamboo kimono sleeve zip top back" ["position"]=> string(1) "4" ["disabled"]=> string(1) "0" ["label_default"]=> string(59) "silk print bamboo kimono sleeve zip top back" ["position_default"]=> string(1) "4" ["disabled_default"]=> string(1) "0" ["url"]=> string(89) "https://www.example.com/media/catalog/product/k/i/kimono-sleeve-top-black_4_.jpg" ["id"]=> string(3) "228" ["path"]=> string(102) "/var/www/vhosts/example.com/httpdocs/media/catalog/product/k/i/kimono-sleeve-top-black_4_.jpg" } ["_hasDataChanges":protected]=> bool(false) ["_origData":protected]=> NULL ["_idFieldName":protected]=> NULL ["_isDeleted":protected]=> bool(false) ["_oldFieldsMap":protected]=> array(0) { } ["_syncFieldsMap":protected]=> array(0) { } } [229]=> object(Varien_Object)#327 (7) { ["_data":protected]=> array(12) { ["value_id"]=> string(3) "229" ["file"]=> string(35) "/k/i/kimono-sleeve-top-black_5_.jpg" ["product_id"]=> string(2) "51" ["label"]=> string(61) "silk print bamboo kimono sleeve zip top detail" ["position"]=> string(1) "5" ["disabled"]=> string(1) "0" ["label_default"]=> string(61) "silk print bamboo kimono sleeve zip top detail" ["position_default"]=> string(1) "5" ["disabled_default"]=> string(1) "0" ["url"]=> string(89) "https://www.example.com/media/catalog/product/k/i/kimono-sleeve-top-black_5_.jpg" ["id"]=> string(3) "229" ["path"]=> string(102) "/var/www/vhosts/example.com/httpdocs/media/catalog/product/k/i/kimono-sleeve-top-black_5_.jpg" } ["_hasDataChanges":protected]=> bool(false) ["_origData":protected]=> NULL ["_idFieldName":protected]=> NULL ["_isDeleted":protected]=> bool(false) ["_oldFieldsMap":protected]=> array(0) { } ["_syncFieldsMap":protected]=> array(0) { } } } ["_itemObjectClass":protected]=> string(13) "Varien_Object" ["_orders":protected]=> array(0) { } ["_filters":protected]=> array(0) { } ["_isFiltersRendered":protected]=> bool(false) ["_curPage":protected]=> int(1) ["_pageSize":protected]=> bool(false) ["_totalRecords":protected]=> NULL ["_isCollectionLoaded":protected]=> NULL ["_cacheKey":protected]=> NULL ["_cacheTags":protected]=> array(0) { } ["_cacheLifetime":protected]=> int(86400) ["_flags":protected]=> array(0) { } }

I have replaced references to the domain with example.com.

 

I need to print a list of the ["url"] that can be seen in this var_dump and I don't know the correct syntax to do this.

 

Hope that explains a bit better.

Thanks for your replies. I figured it out now...

$gallery_images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages();

$items = array();

foreach($gallery_images as $g_image) {
    $items[] = $g_image['url'];
}

Then I could fetch them outside of the loop with:-

implode(', ', $items);

Thanks for your replies anyway...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.