Related
//codeigniter array passed form controller to view
$data['combined_array'] = array('75x35' => $output,'20x5' => $output1);
$all_dates = Array ('2019-11-19','2019-11-20', '2019-11-21','2019-11-22' );
foreach($combined_array as $key => $value){
foreach($value as $key1 => $value1)
{
foreach($value1 as $key2 => $value2)
{
if($value2['size'] == '20x5'){
if(in_array($value2['date'], $all_dates))
{
echo "exists -".$value2['date'];
}
else
{
echo "doesnt exists -".$value2['date'];
}
}
}
}
}
$value2['date'] contains the values 2019-11-20, 2019-11-21,2019-11-22. i want to check if all the values in all_dates exists in $value2['date']. if not then echo doesn't exists. but my code doesn't work.
var_export($combined_array)
array ( '75x35' => array ( '2019-11-19' => array ( 0 => array ( 'size' => '75x35', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-19', 'stock_out' => '4', ), ), '2019-11-20' => array ( 0 => array ( 'size' => '75x35', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-20', 'stock_out' => '6', ), ), '2019-11-21' => array ( 0 => array ( 'size' => '75x35', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-21', 'stock_out' => '5', ), ), '2019-11-22' => array ( 0 => array ( 'size' => '75x35', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-22', 'stock_out' => '5', ), ), ), '20x5' => array ( '2019-11-20' => array ( 0 => array ( 'size' => '20x5', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-20', 'stock_out' => '2', ), ), '2019-11-21' => array ( 0 => array ( 'size' => '20x5', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-21', 'stock_out' => '1', ), ), '2019-11-22' => array ( 0 => array ( 'size' => '20x5', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-22', 'stock_out' => '3', ), ), ), )
For checking each date value of each size row replace your if statement with a new one:
$temp = [];
foreach($combined_array as $key => $value){
foreach($value as $key1 => $value1)
{
foreach($value1 as $key2 => $value2)
{
//if($value2['size'] == '20x5'){
if (!in_array($value2['date'],$temp)){
$temp[] = $value2['date'];
if(in_array($value2['date'], $all_dates))
{
echo "exists -".$value2['date'];
echo "\r\n";
}
else
{
echo "doesnt exists -".$value2['date'];
echo "\r\n";
}
}
}
}
}
EDITED:
Or you can check all_dates values on existing inside defined size:
$temp = [];
foreach($combined_array as $key => $value){
if($key == '20x5') {
$temp = array_keys($value);
$diff = array_diff($all_dates, $temp);
foreach($all_dates as $date)
{
echo !in_array($date, $diff) ? "exists -".$date.PHP_EOL : "doesnt exists -".$date.PHP_EOL;
}
}
}
Demo
A simple solution is for each part of the combined array is to check the difference between the list of dates ( the keys ) and the list of dates you want. This can be done using array_keys() to get a list of the dates and array_diff() to check for missing dates...
foreach( $combined_array as $key => $value){
$dateMismatch = array_diff($all_dates, array_keys($value));
if(count($dateMismatch) > 0 ){
echo "Dates for {$key} missing=".implode(",", $dateMismatch).PHP_EOL;
}
}
This just displays the differences as...
Dates for 20x5 missing=2019-11-19
for your test data.
EDIT:
If you just want the dates different for 1 size, then you don't need a loop, just pick out the details first using something like $combined_array['20x5']...
$dateMismatch = array_diff($all_dates, array_keys($combined_array['20x5']));
if(count($dateMismatch) > 0 ){
echo "Dates missing=".implode(",", $dateMismatch).PHP_EOL;
}
Well Im new to YII 1.1 framework and I have to debug a code.Here the widget CGridView sorts date as number rather than date. That is 01-02-2017,10-01-2017,15-01-2017,21-12-2016.
It should sort with month and year that is 21-12-2016,10-01-2017,15-01-2017,01-02-2017.
My code for view is following :-
<article class="listBox list-view">
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'child-invoice-payments-grid',
'htmlOptions' => array('class' => 'table-responsive'),
'itemsCssClass' => 'table invoiceTable',
'summaryText' => '',
'dataProvider' => new CArrayDataProvider($response, array(
'id' => 'id',
'sort' => array(
//'defaultOrder'=>'date_of_payment ASC',
'attributes' => array(
'date_of_payment','id', 'payment_mode','type','amount'
)
),
'pagination' => array(
'pageSize' => 10
)
)),
'enablePagination' => true,
'pagerCssClass' => 'text-center',
'pager' => array(
'header' => '',
'maxButtonCount' => 4,
'firstPageLabel' => '<<',
'prevPageLabel' => '<',
'nextPageLabel' => '>',
'lastPageLabel' => '>>',
'pageSize' => 5,
'htmlOptions' => array(
'class' => 'pagination',
)
),
'columns' => array(
array(
'name' => 'id',
'type' => 'raw',
'header' => 'Transaction ID',
'value' => 'CHtml::link($data["id"], $data["url"])'
),
**array(
'name' => 'date_of_payment',
'type' => 'raw',
'header' => 'Payment Date',
'value' => 'CHtml::link($data["date_of_payment"], $data["url"])'
)**,
array(
'name' => 'payment_mode',
'type' => 'raw',
'header' => 'Payment Mode',
'value' => 'CHtml::link($data["payment_mode"], $data["url"])'
),
array(
'name' => 'type',
'type' => 'raw',
'header' => 'Payment Type',
'value' => 'CHtml::link($data["type"], $data["url"])'
),
array(
'name' => 'amount',
'type' => 'raw',
'header' => 'Amount',
'value' => 'CHtml::link($data["amount"], $data["url"])'
)
)
));
?>
</article>
and the code for the controller is as follows :-
public function actionPayments($child_id) {
$response = array();
$criteria = new CDbCriteria();
$criteria->condition = "child_id = :child_id AND (invoice_type = 0 OR invoice_type = 1)";
$criteria->params = array(':child_id' => $child_id);
$invoiceModel = ChildInvoice::model()->findAll($criteria);
if (!empty($invoiceModel)) {
foreach ($invoiceModel as $invoice) {
$transactionModel = ChildInvoiceTransactions::model()->findAllByAttributes(array(
'invoice_id' => $invoice->id, 'credit_note_id' => NULL, 'payment_id' => NULL));
if (!empty($transactionModel)) {
foreach ($transactionModel as $transaction) {
$temp = array();
$temp['id'] = "Invoice Payment-" . $transaction->id;
$temp['amount'] = $transaction->paid_amount;
$temp['date_of_payment'] = $transaction->date_of_payment;
$temp['payment_mode'] = customFunctions::getPaymentOptionName($transaction->payment_mode);
$temp['type'] = "Invoice Payment";
$temp['url'] = Yii::app()->createUrl('childInvoice/view', array('child_id' => $invoice->child_id,
'invoice_id' => $invoice->id));
$response[] = $temp;
}
}
}
}
$creditNotesModel = ChildInvoice::model()->findAllByAttributes(array('child_id' => $child_id,
'invoice_type' => 3, 'is_deposit' => 1), array('order' => 'invoice_date'));
if (!empty($creditNotesModel)) {
foreach ($creditNotesModel as $creditNote) {
$temp = array();
$temp['id'] = "Credit Note-" . $creditNote->invoiceUrn;
$temp['amount'] = sprintf("%0.2f", -$creditNote->total);
$temp['date_of_payment'] = $creditNote->invoice_date;
$temp['payment_mode'] = $creditNote->description;
$temp['type'] = "Deposit";
$temp['url'] = Yii::app()->createUrl('childInvoice/updateCreditNote', array(
'id' => $creditNote->id, 'child_id' => $creditNote->child_id));
$response[] = $temp;
}
}
$paymentsModel = Payments::model()->findAllByAttributes(array('branch_id' => Yii::app()->session['branch_id']), array(
'order' => 'date_of_payment'));
if (!empty($paymentsModel)) {
foreach ($paymentsModel as $payments) {
if (in_array($child_id, explode(",", $payments->child_id))) {
$temp = array();
$temp['id'] = "Payment-" . $payments->id;
$temp['amount'] = sprintf("%0.2f", $payments->amount);
$temp['date_of_payment'] = $payments->date_of_payment;
$temp['payment_mode'] = customFunctions::getPaymentOptionName($payments->payment_mode);
$temp['type'] = "Payments";
$temp['url'] = Yii::app()->createUrl('payments/view', array('id' => $payments->id));
$response[] = $temp;
}
}
}
usort($response, function($i, $j) {
$a = strtotime(date("Y-m-d", strtotime($i['date_of_payment'])));
$b = strtotime(date("Y-m-d", strtotime($j['date_of_payment'])));
if ($a == $b)
return 0;
elseif ($a > $b)
return 1;
else
return -1;
});
$this->render('payments', array(
'**response**' => $response,
));
}
So the response array which looks like this will have to be formated :-
[0] => Array
(
[id] => Payment-8
[amount] => 100.00
[date_of_payment] => 06-03-2017
[payment_mode] => Cash
[type] => Payments
[url] => /new_management/index.php/payments/8
)
[1] => Array
(
[id] => Payment-12
[amount] => 1500.00
[date_of_payment] => 22-03-2017
[payment_mode] => Bank/Standing Order
[type] => Payments
[url] => /new_management/index.php/payments/12
)
[2] => Array
(
[id] => Payment-14
[amount] => 150.00
[date_of_payment] => 27-03-2017
[payment_mode] => Cheque
[type] => Payments
[url] => /new_management/index.php/payments/14
)
Below is screenshot of the page and the problem :-
enter image description here
I've been trying to organize data into a multidimensional array from a foreach loop but the data is all over the place.
This is what I coded:
$productIDs = array(
'0' => array(
'product_id' => '10',
'product_name' => 'Test',
'product_file' => 'file10',
'product_image' => 'https://i.imgur.com/dXb6.png',
),
'1' => array(
'product_id' => '20',
'product_name' => 'Test1',
'product_file' => 'file20',
'product_image' => 'https://i.imgur.com/MuP8.png',
),
'2' => array(
'product_id' => '30',
'product_name' => 'No product',
'product_file' => 'file30',
'product_image' => 'https://i.imgur.com/kWP3.png',
)
);
$urlIDs = array(10,20);
function getFiles($productIDs, $urlIDs)
{
foreach($productIDs as $ids)
{
foreach($urlIDs as $products)
{
if(in_array($products, $ids)){
$data1[] = $ids['product_id'];
$data2[] = $ids['product_file'];
$data3[] = $ids['product_image'];
}
}
}
return array($data1, $data2, $data3);
}
$getFiles = getFiles($productIDs, $urlIDs);
And the output for the following is:
Array
(
[0] => Array
(
[0] => 10
[1] => 20
)
[1] => Array
(
[0] => file10
[1] => file20
)
[2] => Array
(
[0] => https://i.imgur.com/dXb6.png
[1] => https://i.imgur.com/MuP8.png
)
)
Although what I'm trying to accomplish is:
Array
(
[0] => Array
(
[id] => 10
[file] => file10
[image] => https://i.imgur.com/dXb6.png
)
[1] => Array
(
[id] => 20
[file] => file20
[image] => https://i.imgur.com/MuP8.png
)
)
I tried the following:
function getFiles($productIDs, $urlIDs)
{
foreach($productIDs as $ids)
{
foreach($urlIDs as $products)
{
if(in_array($products, $ids)){
$data['id'] = $ids['product_id'];
$data['file'] = $ids['product_file'];
$data['image'] = $ids['product_image'];
}
}
}
return array($data);
}
Which returns the following without looping through all id's, it should return both arrays since both id's are matching.
Array
(
[0] => Array
(
[id] => 20
[file] => file20
[image] => https://i.imgur.com/MuP8.png
)
)
You could say the first batch of code without the specific key names isn't what I want, but I just wanted to show what I had tried. The last batch works (with key names), but doesn't loop through all of the $urlIDs, and for some reason that I can't understand, the code isn't even returning id = 10, it's returning id = 20, though it's second in the array. If someone could explain why this is happening I'd appreciate it.
In case it's useful: https://eval.in/764083
PHP code demo
<?php
$productIDs = array(
'0' => array(
'product_id' => '10',
'product_name' => 'Test',
'product_file' => 'file10',
'product_image' => 'https://i.imgur.com/dXb6.png',
),
'1' => array(
'product_id' => '20',
'product_name' => 'Test1',
'product_file' => 'file20',
'product_image' => 'https://i.imgur.com/MuP8.png',
),
'2' => array(
'product_id' => '30',
'product_name' => 'No product',
'product_file' => 'file30',
'product_image' => 'https://i.imgur.com/kWP3.png',
)
);
$urlIDs = array(10,20);
function getFiles($productIDs, $urlIDs)
{
$result=array();
foreach($productIDs as $key => $productData)
{
if(!in_array($productData['product_id'],$urlIDs))
{
unset($productIDs[$key]);
}
else
{
$result[]=array("id"=>$productData['product_id'],"file"=>$productData['product_file'],"image"=>$productData['product_image']);
}
}
return $result;
}
$getFiles = getFiles($productIDs, $urlIDs);
print_r($getFiles);
Output:
Array
(
[0] => Array
(
[id] => 10
[file] => file10
[image] => https://i.imgur.com/dXb6.png
)
[1] => Array
(
[id] => 20
[file] => file20
[image] => https://i.imgur.com/MuP8.png
)
)
Leveraging your tried out solution, change your tried solution to this.
function getFiles($productIDs, $urlIDs)
{
foreach($productIDs as $ids)
{
foreach($urlIDs as $products)
{
if(in_array($products, $ids)){
$data[] = [ "id" => $ids['product_id'],
"file" => $ids['product_file'],
"image" => $ids['product_image']];
}
}
}
return $data;
}
Try this one.
function getFiles($productIDs, $urlIDs)
{
$newDara = [];
foreach($productIDs as $ids)
{
foreach($urlIDs as $products)
{
if(in_array($products, $ids)){
$data['id'] = $ids['product_id'];
$data['file'] = $ids['product_file'];
$data['image'] = $ids['product_image'];
$newData[] = $data;
}
}
}
return array($newData);
}
Try this version
<?php
/**
* Created by PhpStorm.
* User: lenovo
* Date: 3/30/2017
* Time: 5:53 AM
*/
$productIDs = array(
'0' => array(
'product_id' => '10',
'product_name' => 'Test',
'product_file' => 'file10',
'product_image' => 'https://i.imgur.com/dXb6.png',
),
'1' => array(
'product_id' => '20',
'product_name' => 'Test1',
'product_file' => 'file20',
'product_image' => 'https://i.imgur.com/MuP8.png',
),
'2' => array(
'product_id' => '30',
'product_name' => 'No product',
'product_file' => 'file30',
'product_image' => 'https://i.imgur.com/kWP3.png',
)
);
$urlIDs = array(10,20);
$ouput = [];
foreach($productIDs as $product){
if(in_array($product['product_id'],$urlIDs)){
$ouput[] = array("id"=>$product["product_id"], "file"=>$product["product_file"], "image"=>$product["product_image"]);
}
}
echo "<pre>";
print_r($ouput);
echo "</pre>";
Check the
Good Luck
The other answers are either doing too many loops, or looping through the $productIDs array (which I assume is much longer in your actual project).
This method will only iterate on your search array, this should provide a performance boost. You will see this approach posted as the top comment # http://php.net/manual/en/function.array-search.php
Code: (Demo)
$productIDs = array(
'0' => array(
'product_id' => '10',
'product_name' => 'Test',
'product_file' => 'file10',
'product_image' => 'https://i.imgur.com/dXb6.png',
),
'1' => array(
'product_id' => '20',
'product_name' => 'Test1',
'product_file' => 'file20',
'product_image' => 'https://i.imgur.com/MuP8.png',
),
'2' => array(
'product_id' => '30',
'product_name' => 'No product',
'product_file' => 'file30',
'product_image' => 'https://i.imgur.com/kWP3.png',
)
);
$urlIDs=array(10,20);
foreach($urlIDs as $prd_id){
if(($i=array_search($prd_id,array_column($productIDs,'product_id')))!==false){
$result[]=['id'=>$productIDs[$i]['product_id'],
'file'=>$productIDs[$i]['product_file'],
'image'=>$productIDs[$i]['product_image']
];
}else{
echo "$prd_id not found";
}
}
var_export($result);
Output:
array (
0 =>
array (
'id' => '10',
'file' => 'file10',
'image' => 'https://i.imgur.com/dXb6.png',
),
1 =>
array (
'id' => '20',
'file' => 'file20',
'image' => 'https://i.imgur.com/MuP8.png',
),
)
Let's say i have an array like this:
$array = array(
0 =>
array (
'value' => '1' ,
'name' => 'dasdfa sadfa' ),
1=> Array (
'value' => 'adresa#gmail.com' ,
'name' => 'd2' ),
21 =>
array(
'value' => 'adresa#gmail.com' ,
'name' => 'name1`' ),
23 =>
array(
'value' => 'popescu.catalina#gmail.com' ,
'name' => 'POPESCU CATALINA' ),
24 =>
array(
'value' => 'popescu.catalina#gmail.com' ,
'name' => 'POPESCU CATALINA' ),
26 =>
array(
'value' => 'ricardo.ramos#amadeus.com',
'name' => '43414 Test01'),
27 =>
array(
'value' => 'sta3no213123ct3av#yahoo.com',
'name' => 'oct oct' )
);
I want to know if exists duplicated value in array with key 'value' I know how to do this if i want a specified value but general no. The result must be an array with no duplicated values(eg:
$array = array(
0 =>
array (
'value' => '1' ,
'name' => 'dasdfa sadfa' ),
1=> Array (
'value' => 'adresa#gmail.com' ,
'name' => 'd2' ),
23 =>
array(
'value' => 'popescu.catalina#gmail.com' ,
'name' => 'POPESCU CATALINA' ),
26 =>
array(
'value' => 'ricardo.ramos#amadeus.com',
'name' => '43414 Test01'),
27 =>
array(
'value' => 'sta3no213123ct3av#yahoo.com',
'name' => 'oct oct' )
);`
Please help me.
This is my try
function has_dupes($array){
$dupe_array = array();
foreach($array as $val){
if(++$dupe_array[$val] > 1){
return true;
}
}
return false;
}
Try this way:
$array = array(
'0' =>
array (
'value' => '1' ,
'name' => 'dasdfa sadfa' ),
'1'=> Array (
'value' => 'adresa#gmail.com' ,
'name' => 'd2' ),
'21' =>
array(
'value' => 'adresa#gmail.com' ,
'name' => 'name1`' ),
'23' =>
array(
'value' => 'popescu.catalina#gmail.com' ,
'name' => 'POPESCU CATALINA' ),
'24' =>
array(
'value' => 'popescu.catalina#gmail.com' ,
'name' => 'POPESCU CATALINA' ),
'26' =>
array(
'value' => 'ricardo.ramos#amadeus.com',
'name' => '43414 Test01'),
'27' =>
array(
'value' => 'sta3no213123ct3av#yahoo.com',
'name' => 'oct oct' )
);
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
$result = array_unique($array);
print_r($result);
And if you want to store all unique data in one array do it like this:
//declare $array
$unique_array = array();
foreach ($array as $key => $type) {
foreach($type as $vale => $name) {
if ($vale == 'value') {
//echo $name . '<br>';
array_push($unique_array, $name);
}
}
}
$result = array_unique($unique_array);
foreach ($result as $res) {
echo $res . '<br>';
}
Try this
$values = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($values as $key => $value)
{
if ( is_array($value) )
{
$values[$key] = $value;
}
}
print_r($values);
$unique_data = array(); // the result array
$duplicate_data = array();
$seen = array();
foreach ($array as $key => $arr) {
$value = $arr['value'];
if (!isset($seen[$value])) {
$seen[$value] = '';
$unique_data[$key] = $arr;
} else {
$duplicate_data[$key] = $arr; // optional
}
}
unset($seen); // optional in function scope
I am able to traverse a multi-dimensional array, but I also need information about dependencies. Here is what I am trying to do. I have an array like this:
array(
'top1' => 'sth',
'top2' => array(
'sub1' => 'sth',
'sub2' => array(
'line1' => 'sth',
'line2' => 'sth'
)
)
'top3' => 'sth'
)
I am able to traverse the array to get all the keys, result is this:
array([0] => 'top1', [1] => 'top2', [2] => 'sub1', [3] => 'sub2', ...)
However, I need to know the parent of the current element. So I hope I could get something like this:
array(
[top1] => array('parent' => 0, 'id' => 1),
[top2] => array('parent' => 0, 'id' => 2),
[sub1] => array('parent' => 2, 'id' => 2.1),
[sub2] => array('parent' => 2, 'id' => 2.2),
[line1] => array('parent' => 2.2, 'id' => 2.2.1),
...
[top3] => array('parent' => 0, 'id' => 3)
)
I have been trying many ways, but couldn't always get the correct result. Can anyone solve this out? Thanks!
here is a working example for you
function traverse(array $input, $parent = null) {
$result = array();
$current = 1;
foreach ($input as $key => $value) {
$id = null !== $parent ? $parent . '.' . $current : $current;
$result[$key] = array('parent' => null !== $parent ? $parent : 0, 'id' => $id);
if (is_array($value)) {
$result = array_merge($result, traverse($value, $id));
}
$current++;
}
return $result;
}
$input = array(
'top1' => 'sth',
'top2' => array(
'sub1' => 'sth',
'sub2' => array(
'line1' => 'sth',
'line2' => 'sth'
)
),
'top3' => 'sth'
);
echo '<pre>';
print_r($input);
echo '<hr>';
print_r(traverse($input));
echo '</pre>';