I have an application using PHP CodeIgniter's Cart library. And I need this output, to send info to google analytics:
$itemsga = array(
array('sku'=>'SDFSDF', 'name'=>'Shoes', 'category'=>'Footwear', 'price'=>'100', 'quantity'=>'1'),
array('sku'=>'123DSW', 'name'=>'Sandles', 'category'=>'Footwear', 'price'=>'87', 'quantity'=>'1'),
array('sku'=>'UHDF93', 'name'=>'Socks', 'category'=>'Footwear', 'price'=>'5.99', 'quantity'=>'2')
);
To achieve that, I created this code:
$itemsga = array(
foreach ($this->cart->contents() as $items){
array(
'sku' => $items['id'],
'name' => $items['name'],
'price' => $items['price'],
'quantity' => $items['qty'],
),
}//endforeach
);
For some reason I'm getting a white screen. No error displayed but my array is not being built.
I know this might be a stupid question, but I got stuck. Can someone help me?
Cheers!
Change your code to:-
$itemsga = array(); // define array variable
foreach ($this->cart->contents() as $items){
$itemsga[] = array(
'sku' => $items['id'],
'name' => $items['name'],
'price' => $items['price'],
'quantity' => $items['qty'],
); // done indexing and add complete array to each index.
}//endforeach
Related
I am new in PHP. I am working for send data using PHP API. Its method is like below
$createContact->setCustomFieldValues(
[
array(
'customFieldId' => 'pcVFD6',
'value' => array('35')
),
array(
'customFieldId' => 'pcVnzW',
'value' => array('37')
)
]
);
I have data in array like this
$aa = array("pcVFD6"=>"35", "pcVnzW"=>"37");
I want pass this values to above function but I am not getting idea about proper way to do it.
I have tried something like this
foreach ($aa as $key => $value){
$createContact->setCustomFieldValues([
array(
'customFieldId' => $key,
'value' => array($value)
)
]
);
}
its working but passing only last one array to API. Anyone here can please help me for achieve my goal?
Thanks!
You need to transform the array before you pass it to setCustomFieldValues. You don't want to be calling setCustomFieldValues multiple times as your current attempt does, as that's not equivalent to the original. You just need to change the structure of the array ahead of time.
For example:
$aa = array("pcVFD6"=>"35", "pcVnzW"=>"37");
$transformedArr = array();
foreach ($aa as $key => $value){
$transformedArr[] = array(
'customFieldId' => $key,
'value' => array($value)
);
}
$createContact->setCustomFieldValues($transformedArr);
I have the following array built:
$arr['ticket']['custom_fields'][] = array('id'=>$key,'value'=>$value);
I want to dump the contents of that array inside the following section, rather than stating each index such as $arr['ticket']['custom_fields'][0]. When I try a for each loop, it fails using var_dump or even var_export? Any ideas on how I might do this?
$create = json_encode(array('ticket' => array('subject' => $arr['z_subject'],
'comment' => array( "body"=> $arr['z_description']), 'requester' =>
array('name' => $arr['z_name'], 'email' => $arr['z_requester']),
'custom_fields' => array(**$arr['ticket']['custom_fields'][]**))));
I get() an multidimensional array and store it inside the $products variable.
I need to Make a copy of that array to create it into a new Webshop because the export provided by the API does not work so I have created this script to copy the data:
foreach ($products as $id => $product) {
$copy = $products[$id];
$createdProducts = $apiSkylux->products->create(
array(
'id' => $copy['id'],
'createdAt' => $copy['createdAt'],
'updatedAt' => $copy['updatedAt'],
'isVisible' => $copy['isVisible'],
'visibility' => $copy['visibility'],
'data01' => $copy['data01'],
'data02' => $copy['data02'],
'data03' => $copy['data03'],
'url' => $copy['url'],
'title' => $copy['title'],
'fulltitle' => $copy['fulltitle'],
'description' => $copy['description'],
'content' => $copy['content'],
'set' => $copy['set'],
'brand' => $copy['brand'],
'categories' => $copy['categories'],
'deliverydate' => $copy['deliverydate'],
'image' => $copy['image'],
'images' => $copy['images'],
'relations' => $copy['relations'],
'reviews' => $copy['reviews'],
'type' => $copy['type'],
'attributes' => $copy['attributes'],
'supplier' => $copy['supplier'],
'tags' => $copy['tags'],
'variants' => $copy['variants'],
'movements' => $copy['movements'],
)
);
}
The copy is working. But i thought #2016 and all, can't this be achieved with less lines of code?
This is what I receive with var_dump of the first array:
var_dump($products[0]);
exit;
//result
array(28) {
["id"]=>
int(26136946)
//rest of array
So I can see the array has a number (28) , what does this represent?
I've tried several attempts, closest attempt was :
$copy = $products[$id];
$createProducts = $products;
$createdProducts = $apiSkylux->products->create($createProducts);
But then I also got an error : Invalid data input
Can I copy the data from the array easier then the method I am currently using?
array(
'id' => $copy['id'],
...
)
This can be reduced to simply:
$copy
Yes, reassigning every single key into a new array is the same as using the original array in the first place.
foreach($products as $id => $product){
$copy = $products[$id];
This can be reduced to:
foreach ($products as $product){
$copy = $product;
Obviously you could leave out $copy entirely and just use $product.
Bottom line:
foreach ($products as $product) {
$createdProducts = $apiSkylux->products->create($product);
}
What you do with $createdProducts I don't know; you don't seem to be doing anything with it inside the loop, so at best it'll hold the last product after the loop, so is probably superfluous.
Probably you could do:
array_map([$apiSkylux->products, 'create'], $products);
or
$createdProducts = array_map([$apiSkylux->products, 'create'], $products);
depending on whether you need the return values or not.
So I can see the array has a number (28) , what does this represent?
It means it's an array with 28 elements in it.
This doesn't make any sense. Simply use the $product variable within the loop. Done!
I'm writing a php web application where I have a nested array which looks similar to the following:
$results = array(
array(
array(
'ID' => 1,
'Name' => 'Hi'
)
),
array(
array(
'ID' => 2,
'Name' => 'Hello'
)
),
array(
array(
'ID' => 3,
'Name' => 'Hey'
)
)
);
Currently this means that when I want to use the ID field I have to call $results[0][0]['ID'] which is rather inefficient and with an array of over several hundred records becomes messy quickly. I would like to shrink the array down so that I can call $results[0]['ID'] instead.
My understanding is that a function that uses a foreach loop to iterate through each row in the array and change the format would be the best way to go about changing the format of the $results array but I am struggling to understand what to do after the foreach loop has each initial array.
Here is the code I have so far:
public function filterArray($results) {
$outputArray = array();
foreach ($results as $key => $row) {
}
return $outputArray;
}
Would anyone be able to suggest the most effective way to achieve what I am after?
Thanks :)
Simply use call_user_func_array as
$array = call_user_func_array('array_merge', $results);
print_r($array);
Demo
im currently trying to generate a dynamic subnavigation. Therefor i pull data off of the database and store some of it an an array. Now i want to do an foreach in this array. Well as far as I know, this isn't possible.
But maybe I'm wrong. I would like to know if it would be possible, and if so how do i do it?
This is my code, which wont work since it hands out an syntax error.
$this->subnav = array(
'' => array(
'Test Link' => 'login.php',
'Badged Link' => array('warning',10023,'check.php')
),
'benutzer' => array(
'Benutzer suchen' => '/mother/index.php?page=benutzer&subpage=serach_user',
'Benutzer hinzufügen' => '/mother/index.php?page=benutzer&subpage=add_user',
'Rechtevergabe' => '/mother/index.php?page=benutzer&subpage=user_rights'
),
'logout' => array(
'Login' => '/mother/login.php',
'Logout' => '/mother/index.php?page=logout'
),
'datenbank' => array(
(foreach($this->system->get_databases() as $db){array($db->name => $db->url)}),
'Deutschland' => '/mother/login.php',
'Polen' => '/mother/index.php',
'Spanien' => '/mother/index.php',
'Datenbank hinzufügen' => '/mother/index.php?page=datenbank&subpage=add_database'
)
);
}
You can't place foreach loop inside an array like this. You can do something like this though.
foreach($this->system->get_databases() as $db)
{
$this->subnav['datenbank'][$db->name] = $db->url;
}
This is not possible. but you can do it in other way like you can place the foreach outsite and top of this array and assign to an array and then you can use that array variable.
e.g.
$arrDB = array();
foreach($this->system->get_databases() as $db) {
$arrDB[$db->name] = $db->url;
}
Now assign it to:
'datenbank' => $arrDB