I inherited maintenance of a Drupal 7 website and I have zero PHP experience and I need some help. I have just updated Drupal to 7.31 and am getting the error "Only variables should be passed by reference in..." on lines 43 and 86 of an .inc file.
Here is line 43:
$item = array_shift(array_values($data));
Here is line 86:
$item = array_shift(array_values($data));
Can anyone help me out?
I don't know if this is allowed but here the code for the .inc file
<?php
$plugin = _cdd_core_content_type_plugin('nauticus_core', 'menu_full_leaf', 'Page: menu leaf', array(
'description' => 'Add menu children',
), array(
'override_title' => 1,
'override_title_text' => ''
), 'Sail Nauticus');
function nauticus_core_menu_full_leaf_content_type_plugin_render($subtype, &$conf, $panel_args) {
$trail = menu_get_active_trail();
$menu = '';
if (is_array($trail)) {
$leaf = array_pop($trail);
if (isset($leaf['menu_name'])) {
$menu_name = $leaf['menu_name'];
if ($menu_name !== 'navigation') {
$depth = $leaf['depth'];
$p1 = $leaf['p1'];
$data = menu_tree_page_data($menu_name, $depth + 1);
foreach ($data as $i => $item) {
if ($item['link']['mlid'] !== $leaf['plid']
&& !($leaf['plid'] == 0 && ($item['link']['mlid'] == $leaf['mlid']))
) {
unset($data[$i]);
}
}
if (sizeof($data) > 1) {
$menu = menu_tree_output($data);
foreach ($menu as $i => $item) {
if (isset($item['#original_link'])) {
if ($item['#original_link']['mlid'] !== $p1) {
if (isset($menu[$i]['#below'])) {
$menu[$i]['#below'] = array();
}
}
}
}
}
elseif (sizeof($data) == 1) {
$item = array_shift(array_values($data));
if (isset($item['below']) && !empty($item['below'])) {
$menu = menu_tree_output($item['below']);
foreach ($menu as $i => $item) {
if (isset($item['#original_link'])) {
if ($item['#original_link']['mlid'] !== $p1) {
if (isset($menu[$i]['#below'])) {
$menu[$i]['#below'] = array();
}
}
}
}
}
}
}
}
}
return $menu;
}
function nauticus_core_menu_full_leaf_content_type_plugin_title($subtype, &$conf, $panel_args) {
$title = 'Navigation';
$trail = menu_get_active_trail();
if (is_array($trail)) {
$leaf = array_pop($trail);
if (isset($leaf['menu_name'])) {
$menu_name = $leaf['menu_name'];
if ($menu_name !== 'navigation') {
$depth = $leaf['depth'];
$p1 = $leaf['p1'];
$data = menu_tree_page_data($menu_name, $depth + 1);
foreach ($data as $i => $item) {
if ($item['link']['mlid'] !== $leaf['plid']
&& !($leaf['plid'] == 0 && ($item['link']['mlid'] == $leaf['mlid']))
) {
unset($data[$i]);
}
}
if (sizeof($data) >= 1) {
$item = array_shift(array_values($data));
if (isset($item['link']) && !empty($item['link'])) {
if (isset($item['link']['link_title'])) {
$title = $item['link']['link_title'];
}
}
}
}
}
}
return $title;
}
array_shift requires that the first argument be a pass by reference and array_values returns an array.
change those lines that you have $item = array_shift(array_values($data)); to
$item = array_values($data);
$item = array_shift(&$item);
the & means the variable is pass by reference. This should fix your problem.
if that does not work just remove the &.
Change
$item = array_shift(array_values($data));
to
$tmp = array_values($data);
$item = array_shift($tmp);
Related
I have folder structure like below:
-Manga
-Berserk
-345
-346
-One Piece
-840
-841
And want JSON output like below:
{"0" => ["name":"Berserk", "last":"346"],
"1" => ["name":"One Piece, "last":"841"]}
Like the title says I want JSON output that gives me every series name in manga folder and biggest numbered folder that belongs to that series.
I have something like this at the moment.
<?php
$path = dirname(__FILE__) . "/../../manga";
$dir = new DirectoryIterator($path);
$data = array("mangas"=> array());
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot() && $fileinfo->getFilename() !== ".DS_Store") {
array_push($data["mangas"], $fileinfo->getFilename());
}
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($data);
?>
Edit: Slim Framework API
$app->get('/ana', function ($request, $response, $args) {
$path = "../../manga";
function getAll($path)
{
$dirs = [];
foreach (new DirectoryIterator($path) as $item) {
if (!$item->isDir() || $item->isDot()) {
continue;
}
if ($max = getMaxInDir($item->getRealPath())) {
$dirs[] = [
'name' => $item->getFilename(),
'last' => $max,
];
}
}
return $dirs;
}
function getMaxInDir($path)
{
$max = 0;
foreach (new DirectoryIterator($path) as $item) {
$name = $item->getFilename();
if (!$item->isDir() || $item->isDot() || !is_numeric($name)) {
continue;
}
if ($current = (int)$name > $max) {
$max = $current;
};
}
return $max;
}
return $this->response->withJson(getAll($path));
});
Output of http://127.0.0.1/api/public/ana
[{"name":"Berserk","last":true},{"name":"One Piece","last":true}]
First of all your desired output is not a valid JSON. It should rather be
[
{"name":"Berserk","last":346},
{"name":"One Piece","last":841}
]
Now one way to do it
echo json_encode(getAll($path));
function getAll($path)
{
$dirs = [];
foreach (new DirectoryIterator($path) as $item) {
if (!$item->isDir() || $item->isDot()) {
continue;
}
if ($max = getMaxInDir($item->getRealPath())) {
$dirs[] = [
'name' => $item->getFilename(),
'last' => $max,
];
}
}
return $dirs;
}
function getMaxInDir($path)
{
$max = 0;
foreach (new DirectoryIterator($path) as $item) {
$name = $item->getFilename();
if (!$item->isDir() || $item->isDot() || !is_numeric($name)) {
continue;
}
if (($current = (int)$name) > $max) { // updated line
$max = $current;
};
}
return $max;
}
How can i change the test1 array format as like in test2?
$test1 = array(
'size'=>array('V'=>array('V'),'R'=>array('R','R')),
'price'=>array('V'=>array('77'),'R'=>array('88','99')),
'unit'=>array('V'=>array('3'),'R'=>array('3','2')),
'color'=>array('V'=>array('Black'),'R'=>array('Green','Red')));
$test2 = array(
'size'=>array('V','R'),
'price'=>array('V'=>array('Black'=>'77'),
'R'=>array('Green'=>'88','Red'=>'99')),
'unit'=>array('V'=>array('Black'=>'3'),
'R'=>array('Green'=>'3','Red'=>'2')),
'color'=>array('V'=>array('Black'),'R'=>array('Green','Red')));
Thanks!
With a foreach loop you can change the array structure.
http://php.net/manual/de/control-structures.foreach.php
Something like that should work: its pretty unclear what the rules are to convert from one array to the other in your case.
$test2 = array();
foreach ($test as $type => $array) {
$newArray = array();
foreach ($array as $key => $value) {
if ($key == 'V') {
if ($value == 3) {
$newArray['V'] = array('Black' => 3);
} else if ($value = 2) {
$newArray['V'] = array('Green' => 2);
}
} else if ($key == 'R') {
//....
}
}
$test2[$type] = $newArray;
}
Thanks for the response. and sorry for not being more specific. Actually i wanted all the values in dynamic manner. I constructed a loop :). Thanks!
$myArray = array();
foreach($test as $type => $array){
$k = 0;
foreach ($array as $key => $value) {
if($type == 'size'){
$myArray['size'][] = $key;
}else if($type == 'price' || $type == 'unit' ){
$m = 0;
foreach($value as $vall){
if($type == 'price'){
$myArray['price'][$myArray['size'][$k]][$test['color'][$myArray['size'][$k]][$m]] = $test['price'][$myArray['size'][$k]][$m];
}else if($type == 'unit'){
$myArray['unit'][$myArray['size'][$k]][$test['color'][$myArray['size'][$k]][$m]] = $test['unit'][$myArray['size'][$k]][$m];
}
$m++;
}
}else if($type == 'color'){
$myArray['color'][] = $value;
}
$k++;
}
}
I have Already Created The Categories in Magento by using the custom script,
but issue is when I again run the same URL my script creates another set of same category. as i want to update the existing category and insert the new categories if exist in the csv.
My csv structure is:
Category 1,Description,Category 2,Description,Category 3,Description,Category 4,Description
1,COSTUMES,1,ADULTS,1,MENS
1,COSTUMES,1,ADULTS,1,MENS,2,ASIAN
1,COSTUMES,1,ADULTS,1,MENS,3,BIKER
1,COSTUMES,1,ADULTS,1,MENS,1,CAPES & ROBES
1,COSTUMES,1,ADULTS,1,MENS,5,CAVE PEOPLE
Thanking All of You in Advance For Your Answer
Below is my code to create multi level categories:
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$count = 0;
$file = fopen('export/web categories.csv', 'r');
function odd($var)
{
$odd = array();
foreach ($var as $k => $v) {
if ($k % 2 !== 0) {
$odd[$k] = $v;
}
}
return $odd;
}
function even($var)
{
$even = array();
foreach ($var as $k => $v) {
if ($k % 2 == 0) {
$even[$k] = $v;
}
}
return $even;
}
function strcount($str,$sy){
$ch= explode($sy, $str);
return count($ch);
}
$pos=0;
$row_config= array();
$test=array();
$catgoryID=array();
$parID = 1;
while (($line = fgetcsv($file)) !== FALSE) {
if(!in_array($valtest,$row_config)){
if($count == 0){
$count++;
continue;
}
$count++;
$filterd_data=array_filter($line);
$odd_cell_data=odd($filterd_data);
$even_cell_data=even($filterd_data);
$config=array();
$string='';
$intialID=$even_cell_data[0];
foreach($odd_cell_data as $key=>$val){
if(!in_array($val,$config)){
$config[] = $val;
$data['general']['name'] =$val;
$data['general']['meta_title'] = "";
$data['general']['meta_description'] = "";
$data['general']['is_active'] = "";
$data['general']['url_key'] = "";
$data['general']['is_anchor'] = 0;
$data['general']['position'] =1 ;
$storeId = 0;
$string .=$val.'~';
if(!array_key_exists($string, $row_config)){
$catID=createCategory($data, $storeId);
$catgoryID[$string]=$catID;
$row_config[$string]=$parID;
} else {
$parID =$row_config[$string] ;
$row_config[$string]=$row_config[$string];
}
if( strcount($string,'~')==2){
$parID=1;
}
$int[$string]=$parID;
assignCat($catgoryID[$string],$parID);
$parID = $catgoryID[$string];
sleep(0.5);
unset($data);
}
}
}
}
//This Function is used for create each category
function createCategory($data, $storeId) {
$category = Mage::getModel('catalog/category');
$category->setStoreId($storeId);
if (is_array($data)) {
$category->addData($data['general']);
if (!$category->getId()) {
$parentId = $data['category']['parent'];
if (!$parentId) {
if ($storeId) {
$parentId = Mage::app()->getStore($storeId)->getRootCategoryId();
} else {
$parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
}
}
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
} if ($useDefaults = $data['use_default']) {
foreach ($useDefaults as $attributeCode) {
$category->setData($attributeCode, null);
}
}
$category->setAttributeSetId($category->getDefaultAttributeSetId());
if (isset($data['category_products']) && !$category->getProductsReadonly()) {
$products = array();
parse_str($data['category_products'], $products);
$category->setPostedProducts($products);
} try {
$category->save();
$category = Mage::getModel('catalog/category')->load($category->getId());
$category->setPosition($data['general']['position']);
$category->addData($data['general']);
$category->save();
echo "Suceeded <br /> ";
} catch (Exception $e) {
echo "Failed <br />";
}
}
return $category->getId();
}
//This Function is used for moving category under respective parent
function assignCat($id, $parent){
$category = Mage::getModel( 'catalog/category' )->load($id);
Mage::unregister('category');
Mage::unregister('current_category');
Mage::register('category', $category);
Mage::register('current_category', $category);
$category->move($parent);
return;
}
I have a problem with shuffling and slicing array.
I have this code:
$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$selectedProducts = array_slice($selectedProducts, 0, $maxDisplayItem);
foreach ($selectedProducts as $_id) {
shuffle($products);
foreach ($products as $_product) {
....
}
}
My code limiting the number of displayed item but didn't shuffle it at all.
When I change the order of actions:
shuffle($selectedProducts);
foreach ($selectedProducts as $_id) {
$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$products = array_slice($products, 0, $maxDisplayItem);
foreach ($products as $_product) {
....
}
}
the code shuffling and slicing results but only first (e.g. 3 results) from whole array who has 50 items.
Could anyone help me with this?
here is the whole function:
function displayProductList()
{
// Store View
$store = $this->getStoreViewCode();
$selectedProducts = $this->getSelectedProducts();
$products = $this->_products->getProductsFromDb($selectedProducts, $store, $this->getProductsStoragePid());
// Load Template File
$templateHtml = $this->cObj->fileResource( $this->_getConfig('templateProductList') );
$productListHtml = $this->cObj->getSubpart($templateHtml, '###PRODUCT_LIST###');
$productHtml = $this->cObj->getSubpart($productListHtml, '###PRODUCT_ITEM###');
$subPartContent = ''; $item = 0; $items = count($products); $even = true; $line = '';
shuffle($selectedProducts);
foreach ($selectedProducts as $_id) {
$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$products = array_slice($products, 0, $maxDisplayItem);
foreach ($products as $_product) {
if ($_id === $_product['product_id']) {
$markers = $this->_products->getProductMarkers($_product);
// Even/Odd CSS Class Determination
if ($even === true) {
$line = 'even';
$even = false;
} else {
$line = 'odd';
$even = true;
}
// Class Determination First/Last
if ($item == 0) {
$markers['###EVENODD###'] = $line . ' ' . 'first';
} else if ($item == $items-1) {
$markers['###EVENODD###'] = $line . ' ' . 'last';
} else {
$markers['###EVENODD###'] = $line;
}
// Check if the product has an image
$imageHtml = '<p>'.$this->pi_getLL('template_label_no_image_available').'</p>';
if ($markers['###DETAIL_IMAGE###'] != 'no_selection') {
$imageHtml = $this->cObj->getSubpart($productHtml, '###PRODUCT_IMAGE###');
}
$p = $this->cObj->substituteSubpart($productHtml, '###PRODUCT_IMAGE###', $imageHtml);
$subPartContent .= $this->cObj->substituteMarkerArray($p, $markers);
$item++;
}
}
}
return $this->cObj->substituteSubpart($productListHtml, '###PRODUCT_ITEM###', $subPartContent);
}
function displayProductList(){
// Store View
$store = $this->getStoreViewCode();
$selectedProducts = $this->getSelectedProducts();
$products = $this->_products->getProductsFromDb($selectedProducts, $store, $this->getProductsStoragePid());
// Load Template File
$templateHtml = $this->cObj->fileResource( $this->_getConfig('templateProductList') );
$productListHtml = $this->cObj->getSubpart($templateHtml, '###PRODUCT_LIST###');
$productHtml = $this->cObj->getSubpart($productListHtml, '###PRODUCT_ITEM###');
$subPartContent = ''; $item = 0; $items = count($products); $even = true; $line = '';
$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$products = array_slice($products, 0, $maxDisplayItem);
shuffle($products);
foreach ($products as $_product) {
$markers = $this->_products->getProductMarkers($_product);
// Even/Odd CSS Class Determination
if ($even === true) {
$line = 'even';
$even = false;
} else {
$line = 'odd';
$even = true;
}
// Class Determination First/Last
if ($item == 0) {
$markers['###EVENODD###'] = $line . ' ' . 'first';
} else if ($item == $items-1) {
$markers['###EVENODD###'] = $line . ' ' . 'last';
} else {
$markers['###EVENODD###'] = $line;
}
// Check if the product has an image
$imageHtml = '<p>'.$this->pi_getLL('template_label_no_image_available').'</p>';
if ($markers['###DETAIL_IMAGE###'] != 'no_selection') {
$imageHtml = $this->cObj->getSubpart($productHtml, '###PRODUCT_IMAGE###');
}
$p = $this->cObj->substituteSubpart($productHtml, '###PRODUCT_IMAGE###', $imageHtml);
$subPartContent .= $this->cObj->substituteMarkerArray($p, $markers);
$item++;
}
return $this->cObj->substituteSubpart($productListHtml, '###PRODUCT_ITEM###', $subPartContent);
}
I want to remove some duplicate values on an array, but there is a condition that the script has to ignore the array that contains a specific word.
Below code is adapted from PHP: in_array.
$array = array( 'STK0000100001',
'STK0000100002',
'STK0000100001', //--> This should be remove
'STK0000100001-XXXX', //--> This should be ignored
'STK0000100001-XXXX' ); //--> This should be ignored
$ignore_values = array('-XXXX');
if(make_unique($array, $ignore_values) > 0) {
//ERROR HERE
}
The function to make the array unique is:
function make_unique($array, $ignore) {
$i = 0;
while($values = each($array)) {
if(!in_array($values[1], $ignore)) {
$dupes = array_keys($array, $values[1]);
unset($dupes[0]);
foreach($dupes as $rmv) {
$i++;
}
}
}
return $i;
}
I have tried to use if(!in_array(str_split($values[1]), $ignore)) ... but it just the same.
The array should become like:
STK0000100001
STK0000100002
STK0000100001-XXXX
STK0000100001-XXXX
How to do that?
Try this one, just remove the print_r(); inside the function when using in production
if(make_unique($array, $ignore_values) > 0) {
//ERROR HERE
}
function make_unique($array, $ignore) {
$array_hold = $array;
$ignore_val = array();
$i = 0;
foreach($array as $arr) {
foreach($ignore as $ign) {
if(strpos($arr, $ign)) {
array_push( $ignore_val, $arr);
unset($array_hold[$i]);
break;
}
}
$i++;
}
$unique_one = (array_unique($array_hold));
$unique_one = array_merge($unique_one,$ignore_val);
print_r($unique_one);
return count($array) - count($unique_one);
}
This should work for >= PHP 5.3.
$res = array_reduce($array, function ($res, $val) use ($ignore_values) {
$can_ignore = false;
foreach ($ignore_values as $ignore_val) {
if (substr($val, 0 - strlen($ignore_val)) == $ignore_val) {
$can_ignore = true;
break;
}
}
if ( $can_ignore || ! in_array($val, $res)) {
$res[] = $val;
}
return $res;
}, array()
);
Otherwise
$num_of_duplicates = 0;
$res = array();
foreach ($array as $val) {
$can_ignore = false;
foreach ($ignore_values as $ignore_val) {
if (substr($val, 0 - strlen($ignore_val)) == $ignore_val) {
$num_of_duplicates++;
$can_ignore = true;
break;
}
}
if ( $can_ignore || ! in_array($val, $res)) {
$res[] = $val;
}
}
Edit: Added duplicate count to the second snippet.