Here is my code.php:
$info = array (
0 =>
array (
'num' => 1,
'name' => '15 TV',
'stream_type' => 'live',
'stream_id' => 219,
'stream_icon' => 'https://is1-ssl.mzstatic.com/image/thumb/Purple71/v4/5d/2f/1c/5d2f1cb1-3a21-71ff-c10c-e27253ba13dc/source/512x512bb.jpg',
'epg_channel_id' => NULL,
'added' => '1535765481',
'category_id' => '3',
'custom_sid' => '',
'tv_archive' => 0,
'direct_source' => '',
'tv_archive_duration' => 0,
),
479 =>
array (
'num' => 125,
'name' => 'LA DOS',
'stream_type' => 'live',
'stream_id' => 323,
'stream_icon' => 'http://www.dazplayer.com/img_chh/la_dos_espa%C3%B1a.png',
'epg_channel_id' => NULL,
'added' => '1535838540',
'category_id' => '13',
'custom_sid' => '',
'tv_archive' => 0,
'direct_source' => '',
'tv_archive_duration' => 0,
),
);
I have these array and I want to print it, the arrays go to the number 0 from 479. I tried to use the code that it is below but it is not working as well as I want, because it is not printing the right variables. I know that I have to use a loop and the command line for each but I could not be able to do it work.
The variables that I am interested in are:
name
stream_id
stream_icon
category_id
<?php
foreach($info as $x => $x_value) {
echo "Name=" . $x . ", Value=" . $x_value;
echo "<br>;";
}
?>
If someone could help me fixing the bug, i will be thankful.
Regards, Dix
You can try like this way with foreach() loop as $key=>$value pattern. To get values you've to use $value['field_name_for_that_you_need_value']. For example: $x_value['name'] to get the value of name field
<?php
foreach($info as $x => $x_value) {
echo "Name=" . $x_value['name'] . ", Stream Id=" . $x_value['stream_id'].", Stream Icon=".$x_value['stream_icon']. "Category Id=". $x_value['category_id'];
echo "<br>;";
}
?>
DEMO: https://3v4l.org/cvTsc
You could use this method:
print_r($info);
Which will be in your context:
foreach($info as $key => $value)
{
echo "Name=" . $key . ", Value=" . print_r($value, true);
echo "<br>";
}
Documentation
Related
What I'm trying to accomplish is to write a html string in a controller with array values being looped in it. So for example;
$content = "Your store, at location A, has these items added to them". add array loop here. "Do take note!";
My array would be as such
array (
0 =>
array (
'id' => '5db29b6d31c391731239bbdf',
'name' => 'Diamond bracelet (sample)',
'tags' =>
array (
0 => 'female',
1 => 'jewelry',
),
'category' => 'Accessories',
'sku' => '1029EHW',
'priceType' => 'Fixed',
'unitPrice' => 190,
'cost' => 90,
'trackStockLevel' => true,
'isParentProduct' => false,
),
1 =>
array (
'id' => '5db29b6d31c391731239bbdb',
'name' => 'Long-sleeved shirt(sample)(M)',
'tags' =>
array (
0 => 'tops',
1 => 'cotton',
),
'category' => 'Women\'s Apparel',
'sku' => 'ABC1234-M',
'priceType' => 'Fixed',
'unitPrice' => 47.170000000000002,
'cost' => 20,
'trackStockLevel' => true,
'isParentProduct' => false,
'parentProductId' => '5db29b6d31c391731239bbd4',
'variationValues' =>
array (
0 =>
array (
'variantGroupId' => '5db29b6d31c391731239bbd5',
'value' => 'M',
),
),
),
)
note that the array can have many instances of product_name and sku, or can have none.
How do i populate this in my string to be like;
$content = "Your store, at location A, has these items added to them, 1) asd, 2)def, 3)asf . Do take note!
Try this, I've used \sprintf for ease, check if the output serves your purpose.
<?php
function stringMethod(): string
{
$count = 0;
$arrayString = [];
$array = [['product_name' => 'abc', 'product_sku' => 'def'],['product_name' => 'abc', 'product_sku' => 'asd']];
foreach ($array as $value){
$count++;
$arrayString[] = sprintf('%s)%s', $count, $value['product_sku']);
}
$string = \implode(',', $arrayString);
return \sprintf("Your store, at location A, has these items added to them %s Do take note!", $string);
}
echo stringMethod();
Hope this will help you. try to do it in less number of lines
$content = "Your store, at location A, has these items added to them, ";
$productArray = array (0 => array ('id' => '5db29b6d31c391731239bbdf','name' => 'Diamond bracelet (sample)','tags' => array (0 => 'female',1 => 'jewelry',),'category' => 'Accessories','sku' => '1029EHW','priceType' => 'Fixed','unitPrice' => 190,'cost' => 90,'trackStockLevel' => true,'isParentProduct' => false,),1 => array ('id' => '5db29b6d31c391731239bbdb','name' => 'Long-sleeved shirt(sample)(M)','tags' => array (0 => 'tops',1 => 'cotton',),'category' => 'Women\'s Apparel','sku' => 'ABC1234-M','priceType' => 'Fixed','unitPrice' => 47.170000000000002,'cost' => 20,'trackStockLevel' => true,'isParentProduct' => false,'parentProductId' => '5db29b6d31c391731239bbd4','variationValues' => array (0 => array ('variantGroupId' => '5db29b6d31c391731239bbd5','value' => 'M'))));
foreach ($productArray as $key => $product) {
$content .= ($key+1).') '.$product['name'];
if (count($productArray)-1!=$key) {
$content .= ', ';
}
}
$content .= ". Do take note!";
$content = "Your store, at location A, has these items added to them,". $this->getItemList($collection) .". Do take note!"
# Somewhere else in the controller
protected function getItemList(Collection $collection): string
{
return $collection->pluck('name')
->merge($collection->pluck('sku'))
->map(function($item, $key) {
return ($key + 1) . ') ' . $item;
})
->implode(', ');
}
An easy solution would be
$content = "Your store, at location A, has these items added to them";
$array = array(0=>["product_name" => "abc", "product_sku" => "def"], 1=>['product_name' => 'kdkf', 'product_sku'=> 'ljbkj']);
for($i = 0; $i<count($array); $i++){
$content .= ($i+1).") ".$array[$i]['product_name].' '.$array[$i]['product_sku'].', ';
}
$content .= "Do take note.";
This way you're just constantly concatonating the string value in separate parts instead of trying to inject it in the middle of the parent string.
not tested, may be syntax errors
Hi i need to print the names of $aSpelers and $aRugnummers when array $Posities of the $aSpelers is 'verdediger'
So for example:
Janmaat 7 Verdediger
de Vrij 3 Verdediger
So the first value of the array doesnt has to print out because it's not a 'verdediger'
Here are the arrays i have to use
$aSpelers = array('Cilessen', 'Janmaat', 'de Vrij' , 'Vlaar', 'Blind', 'de Jong', 'Sneijder');
$aRugnummers = array(1, 7, 3, 2, 5,8, 10 );
$Posities = array('doel', 'verdediging', 'verdediging', 'verdediging', 'verdediging','middenveld','middenveld');
I have to use a foreach loop this is what i have already
foreach()
{
}
I suggest using a multidimensional array. This makes your goal (get it?) a lot easier.
$aSpelers = array(
array(
'naam' => 'Cilessen',
'rugnummer' => '1',
'positie' => 'doel',
),
array(
'naam' => 'Janmaat',
'rugnummer' => '7',
'positie' => 'verdediging',
),
array(
'naam' => 'de Vrij',
'rugnummer' => '3',
'positie' => 'verdediging',
),
array(
'naam' => 'Vlaar',
'rugnummer' => '2',
'positie' => 'verdediging',
),
array(
'naam' => 'Blind',
'rugnummer' => '5',
'positie' => 'verdediging',
),
array(
'naam' => 'de Jong',
'rugnummer' => '8',
'positie' => 'middenveld',
),
array(
'naam' => 'Sneijder',
'rugnummer' => '10',
'positie' => 'middenveld',
),
);
foreach ($aSpelers as $speler) {
if ($speler['positie'] == 'verdediger') {
echo $speler['naam'].' heeft rugnummer '.$speler['rugnummer'].' en speelt positie '.$speler['positie'].'<br />';
}
}
That should do the job.
Edit
I added an if condition to check if the speler is a verdediger and only echo if that is the case.
All array contain same number of value so you can use for loop
for ($i = 0; $i < count($aSpelers); $i++) {
if ($Posities[$i] == "verdediging") {// check condition to match Verdediger
echo $aSpelers[$i] . " " . $aRugnummers[$i] . " " . $Posities[$i];
}
}
Use like this for foreach. change array as object and use
$object = (object) $Posities;
$i=0;
foreach($object as $cont){
if($cont=="verdediging")
echo $aSpelers[$i]." - ".$aRugnummers[$i]." - ".$aRugnummers[$i]."<br>";
$i++;
}
I am working on a form that needs to be able to update two different tables.
I am using the Order controller and need to update the order table. The other table I need to update is CampaignCustomers, where I would like to update the campaignID.
The two Models are associated, and here is the result when I debug $order:
array(
'Order' => array(
'OrderID' => (int) 1574996,
'OrderType' => '3',
'UPSTrackingNumber' => null,
'CreatedDate' => 'Mar 30 2019 12:42:00:000PM',
'ShippedOnDate' => null,
'Notes' => null,
'OrderedBy' => 'TIM',
'UserID' => (int) 431,
'CampaignCustomerID' => (float) 78156512,
'TaxPercentage' => (int) 0,
'DiscountID' => null,
'DiscountPercentage' => (int) 8,
'TotalPrice' => (float) 7.75,
'OrderStatusID' => (int) 13,
'LabelCategory' => null,
'LabelPrinted' => (int) 0,
'InvoicePrinted' => (int) 0,
'ShippingMethodID' => (int) 9,
'SaturdayDelivery' => (int) 0,
'ShippingAddress' => 'ert',
'ShippingPrice' => (float) 7.75,
'ConfirmationEmail' => null,
'PaymentMethod' => 'Bill In Full',
'PurchaseOrderNumber' => ' dfqsdfsdafe3r23rwererewrw',
'CreditCardHolderName' => null,
'CreditCardNumber' => null,
'CreditCardExpirationDate' => null,
'CreditCardStreet' => null,
'CreditCardZipCode' => null,
'CreditCardCVV' => null,
'TransactionID' => null
),
'CampaignCustomer' => array(
'CampaignCustomerID' => (float) 78156512,
'CampaignID' => (int) 422,
'CustomerID' => (int) 3633
),
This is my view:
<?php
debug($order);
echo $this->Html->div("box");
echo $this->Html->tag("h3","Edit Order ".$order['Order']['OrderID']);
echo "<p><b>Company: </b>" . $order["Company"]["CompanyName"] . "</p>";
echo "<p><b>Order ID: </b>" . $order["Order"]["OrderID"] . "</p>";
echo $this->Form->create(("Order"));
echo $this->Form->input("PurchaseOrderNumber");
echo $this->Form->input("CampaignID",array("options"=>$campaigns,"label"=>"Campaign","selected"=>$order["Campaign"]["CampaignID"]));
echo $this->Form->input("CreatedDate");
echo $this->Form->input("OrderType",array("options"=>$orderType,"selected"=>$order["Order"]["OrderType"]));
echo $this->Form->input("OrderedBy");
echo $this->Form->input("ShippingMethodID",array("options"=>$shipping_method_ids,"selected"=>$order["Order"]["ShippingMethodID"],"label"=>"Shipping Method"));
echo $this->Form->input("SaturdayDelivery");
echo $this->Form->input("UserID",array("options"=>$users,"selected"=>$order["Order"]["UserID"],"label"=>"Sales Agent"));
echo $this->Form->input("DiscountPercentage");
echo $this->Form->input("ShippingAddress");
echo $this->Form->end("Update Order");
echo "</div>";
?>
The only thing that won't update is the CampaignID field, because it is a different table.
Does anyone know how to solve this problem?
Thanks!
You've not passed the CampaignCustomer data in your form, so when the post request is made, there's nothing for it to update in terms of CampaignCustomer
You need to include this information in your form in either hidden fields or visible ones to ensure it gets updated.
echo $this->Form->create();
echo $this->Form->input("Order.PurchaseOrderNumber");
echo $this->Form->input("Order.CampaignID",array("options"=>$campaigns,"label"=>"Campaign","selected"=>$order["Campaign"]["CampaignID"]));
echo $this->Form->input("Order.CreatedDate");
echo $this->Form->input("Order.rderType",array("options"=>$orderType,"selected"=>$order["Order"]["OrderType"]));
echo $this->Form->input("Order.OrderedBy");
echo $this->Form->input("Order.ShippingMethodID",array("options"=>$shipping_method_ids,"selected"=>$order["Order"]["ShippingMethodID"],"label"=>"Shipping Method"));
echo $this->Form->input("Order.SaturdayDelivery");
echo $this->Form->input("Order.UserID",array("options"=>$users,"selected"=>$order["Order"]["UserID"],"label"=>"Sales Agent"));
echo $this->Form->input("Order.DiscountPercentage");
echo $this->Form->input("Order.ShippingAddress");
echo $this->Form->input("CampaignCustomer.CampaignCustomerID");
echo $this->Form->input("CampaignCustomer.CampaignID");
echo $this->Form->input("CampaignCustomer.CustomerID");
echo $this->Form->end("Update Order");
I'm have time overlap check inside while loop and I would also like to display events title with results which is in another array.
Without double foreach loop I can echo $event->title; and it displays all event titles. If I put array $event which has $event->title, inside foreach which I have for overlap testing it only displays first title.
I'm newbie PHP and I'm banging my head with complete solution for a couple of days now. Any help would be awesome :(
while (list(,$event_row) = each($events)) {
$event->loadEvent($event_row[0]);
foreach ($events as $thisevent) {
$event_array = array($event->id,$event->title);
$conflicts = 0;
foreach ($events as $thatevent) {
if ($thisevent[0] === $thatevent[0]) {
continue;
}
$thisevent_from = $thisevent[1];
$thisevent_ends = $thisevent[2];
$thatevent_from = $thatevent[1];
$thatevent_ends = $thatevent[2];
if ($thatevent_ends > $thisevent_from AND $thisevent_ends > $thatevent_from) {
echo $event->title;
$conflicts++;
echo "Event #" . $thisevent[0] . " overlaps with Event # " . $thatevent[0] . "\n";
}
}
if ($conflicts === 0) {
echo "Event #" . $thisevent[0] . " is OK\n";
}
}
}
Without double foreach var_export($event) displays all event arrays.
array( 'title' => 'Event 1', 'description' => 'Event description 1', 'contact' => 'event contact 1', 'url' => '', 'link' => '', 'email' => 'event#eventemail.com', 'picture' => '', 'color' => '#009933', 'catName' => 'Events', 'catDesc' => 'General events', 'startDate' => 1432918800, 'endDate' => 1432926000, 'startDay' => 29, 'startMonth' => 5, 'startYear' => 2015, 'endDay' => 29, 'endMonth' => 5, 'endYear' => 2015, 'recurStartDate' => NULL, 'recurEndDate' => NULL, 'id' => '11', 'catId' => 1, 'status' => true, 'recType' => '', 'recInterval' => 0, 'recEndDate' => 1432850400, 'recEndType' => 0, 'recEndCount' => 1, ))
And var_export($events) array displays three times all arrays with id, start date and end date:
array ( 0 => array ( 0 => '11', 1 => 1432918800, 2 => 1432926000, ), 1 => array ( 0 => '13', 1 => 1432918800, 2 => 1432926000, ), 2 => array ( 0 => '16', 1 => 1432926000, 2 => 1432929600, ), )
Thank you for looking into it and giving any possible solutions hints, ideas :)
I have an array:
$settings = array(
'name' => array(
0 => 'Large Pouch',
1 => 'XL Pouch'
),
'size' => array(
0 => '9x14',
1 => '12x18'
),
'weight' => array(
0 => '10',
1 => '20'
),
'metro_manila_price' => array(
0 => '59',
1 => '79'
),
'luzvimin_price' => array(
0 => '89',
1 => '139'
)
);
I wanted to put the values from that array to one array. $shipping_options with format of
for example:
$shipping_options = array(
'0' => 'Large Pouch 9x14 - $59',
'1' => 'XL Pouch 12x18 - $79'
);
How to program this?
You could write a loop:
$shipping_options = array();
foreach ($settings['name'] as $key => $value) {
$value = sprintf('%s(%s) - $%s',
$value,
$settings['size'][$key],
$settings['metro_manila_price'][$key]);
$shipping_options[$key] = $value;
}
try this one
echo "<pre>";
$size = count($settings['name']);
$shipping_options = array();
for($i=0; $i<$size; $i++)
{
$shipping_options[$i] = $settings['name'][$i]."(".$settings['size'][$i].") - $".$settings['metro_manila_price'][$i];
}
print_r($shipping_options);
You can try this
foreach ($settings['name'] as $key => $value) {
$shipping_options[$key] = $settings['name'][$key] . " " . $settings['size'][$key] . " - $" . $settings['metro_manila_price'][$key];
}