//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;
}
Related
This is my array:
Array ( [TR] => Array ( [name] => sayfa adı [description] =>
sayfa icerigi
[slug] => sayfa linki ) [EN] => Array ( [name_en] => page name [description_en] =>
page information
[slug_en] => page link ) [DE] => Array ( [name_de] => seite name [description_de] =>
seite informationen
[slug_de] => seite link ) )
this is my codeigniter insert code:
$_INS = array(
'lang_id' => '?',
'name' => '?',
'description' => '?',
'slug' => '?',
'page_id' => $insert_id,
'adding' => $adding,
'updated_at' => date('Y-m-d H:i:s'),
'created_at' => date('Y-m-d H:i:s')
);
and I want to add every array into the DB with a new line
Simple nested foreach with a key modifier to remove the underscore and a batch insert should work:
<?php
$array = array(
'TR' => array (
'name' => 'stmtA_1',
'description' => 'stmtA_2',
'slug' => 'stmtA_3',
),
'EN' => array(
'name_en' => 'stmtB_1',
'description_en' => 'stmtB_2',
'slug_en' => 'stmtB_3',
),
);
$insert_id = null;
$adding = null;
$i = 0;
$data = array();
foreach ($array as $lang => $values) {
$data[$i] = array(
'page_id' => $insert_id,
'adding' => $adding,
'updated_at' => date('Y-m-d H:i:s'),
'created_at' => date('Y-m-d H:i:s'),
'lang_id' => $lang
);
foreach ($values as $identifier => $value) {
$key = explode('_', $identifier)[0];
$data[$i][$key] = $value;
}
$i++;
}
echo '<pre>';
print_r($data);
$this->db->insert_batch('my_table', $data);
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',
),
)
retrieve data from database :
$db = array (
0 =>
array (
'stay' => '10',
'end' => '2015-12-29',
'start' => '2015-12-20',
'pid' => '231096236',
'normal_price' => '385',
'promo_price' => '385',
'resell_price' => '385',
),
1 =>
array (
'stay' => '7',
'end' => '2016-01-05',
'start' => '2015-12-30',
'pid' => '231096235',
'normal_price' => '385',
'promo_price' => '385',
'resell_price' => '385',
),
2 =>
array (
'stay' => '3',
'end' => '2053-01-01',
'start' => '2016-01-06',
'pid' => '231096234',
'normal_price' => '385',
'promo_price' => '385',
'resell_price' => '385',
),
),
new array to merge with:
$new = array (
0 =>
array (
'stay' => '2',
'end' => '2015-07-01',
'start' => '2015-06-03',
'normal_price' => '145',
'promo_price' => '145',
'resell_price' => '135',
),
1 =>
array (
'stay' => '4',
'end' => '2015-07-05',
'start' => '2015-07-02',
'pid' => '231096235',
'normal_price' => '100',
'promo_price' => '100',
'resell_price' => '100',
),
2 =>
array (
'stay' => '4',
'end' => '2015-09-03',
'start' => '2015-07-06',
'pid' => '231096236',
'normal_price' => '100',
'promo_price' => '100',
'resell_price' => '100',
),
),
expected result:
$expected = array (
0 =>
array (
'stay' => '2',
'end' => '2015-07-01',
'start' => '2015-06-03',
'normal_price' => '145',
'promo_price' => '145',
'resell_price' => '135',
),
1 =>
array (
'stay' => '4',
'end' => '2015-07-05',
'start' => '2015-07-02',
'pid' => '231096235',
'normal_price' => '100',
'promo_price' => '100',
'resell_price' => '100',
),
2 =>
array (
'stay' => '4',
'end' => '2015-09-03',
'start' => '2015-07-06',
'pid' => '231096236',
'normal_price' => '100',
'promo_price' => '100',
'resell_price' => '100',
),
3 =>
array (
'stay' => '3',
'end' => '2053-01-01',
'start' => '2016-01-06',
'pid' => '231096234',
'normal_price' => '385',
'promo_price' => '385',
'resell_price' => '385',
),
),
my result so far:
$merge = array (
0 =>
array (
'stay' => '2',
'end' => '2015-07-01',
'start' => '2015-06-03',
'normal_price' => '145',
'promo_price' => '145',
'resell_price' => '135',
),
1 =>
array (
'stay' => '4',
'end' => '2015-07-05',
'start' => '2015-07-02',
'pid' => '231096235',
'normal_price' => '100',
'promo_price' => '100',
'resell_price' => '100',
),
2 =>
array (
'stay' => '4',
'end' => '2015-09-03',
'start' => '2015-07-06',
'pid' => '231096236',
'normal_price' => '100',
'promo_price' => '100',
'resell_price' => '100',
),
),
these are the requirements:
if record in new array does not has pid (price id) then it's
considered as new data. (record with start : 2015-06-03 and end :
2015-06-03)
if record in new array has pid and match with one of record from table, then new record's values will override the old one (record with pid : 231096235 and pid : 231096236)
if record from database has pid and does not match with any pid from new array then it should be added to new array (it shouldb record with pid : 231096234)
my functions :
$merge = array();
foreach ($new as $ikey => $ival) {
$ispid = (isset($ival['pid'])) ? (int) $ival['pid'] : 0;
if ($ispid === 0) {
$merge[] = $ival;
}
if ($ispid > 0) {
$exist = false;
foreach ($db as $rkey => $rval) {
$rspid = (int) $rval['pid'];
if ($ispid === $rspid) {
$spid_exist = true;
overridePriceValue($ival, $rval);
$merge[] = $rval;
}
}
// if ($exist === false) {
// echo "price id " . $ispid . " is not match with any special prices.";
// }
}
}
function overridePriceValue($input_spc_price, &$spc_price) {
if (isset($input_spc_price['stay'])) {
$spc_price['stay'] = $input_spc_price['stay'];
}
if (isset($input_spc_price['end'])) {
$spc_price['end'] = $input_spc_price['end'];
}
if (isset($input_spc_price['start'])) {
$spc_price['start'] = $input_spc_price['start'];
}
if (isset($input_spc_price['normal_price'])) {
$spc_price['normal_price'] = $input_spc_price['normal_price'];
}
if (isset($input_spc_price['promo_price'])) {
$spc_price['promo_price'] = $input_spc_price['promo_price'];
}
if (isset($input_spc_price['resell_price'])) {
$spc_price['resell_price'] = $input_spc_price['resell_price'];
}
}
I need your advise to achieve the goal. thanks :)
You can use the UDF in_array_r from another stackoverflow question (thanks to jwueller) and do it like this:
$merge = $new;
foreach($db as $item) {
if (!in_array_r($item["pid"], $new)) {
$merge[] = $item;
}
}
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
I have an array of data like this . This array is the result of a database query , I want to get the index of rows and columns . I tried to get the index of each row but nonetheless failed. so, can anyone help me?
Query Result
array(
(int) 0 => array(
'B' => array(
'company' => 'ABC'
),
'User' => array(
'company' => 'abc'
),
(int) 0 => array(
'date_part' => '3',
'jumlah' => null,
'jumbuy' => '50990',
'admin' => '50010'
),
(int) 1 => array(
'date_part' => '4',
'jumlah' => null,
'jumbuy' => '98990',
'admin' => '2010'
)
),
(int) 1 => array(
'B' => array(
'company' => 'BCD'
),
'User' => array(
'company' => 'bcd'
),
(int) 0 => array(
'date_part' => '3',
'jumlah' => null,
'jumbuy' => '65000',
'admin' => '5000'
),
(int) 1 => array(
'date_part' => '4',
'jumlah' => null,
'jumbuy' => '9000',
'admin' => '5000'
)
),
(int) 3 => array(
'B' => array(
'company' => 'CDE'
),
'User' => array(
'company' => 'cde'
),
(int) 0 => array(
'date_part' => '4',
'jumlah' => null,
'jumbuy' => '34566',
'admin' => '2010'
)
)
);
Get Index
for ($row = 0; $row < count($array); $row++) {
for($col = 0; $col < count(.....); $col++ ) {
echo "Baris [row] kolom [colum]"; // output row and column
}
}
The below code will give you all the indexes of this given array.
I checked the given array with the following code in my localhost.
And it gives us all the keys and the values in this array.
Try this
<?php
foreach($array as $arr=>$value )
{
foreach($value as $ar=>$a)
{
echo $ar."<br>";
foreach($a as $res =>$r)
{
echo $res.": ";
echo $r;
echo "<br>";
}
}
}
?>
Use nested foreach for echoes keys:
foreach ($array as $row => $v) {
foreach ($v as $col => $val) {
echo 'row: ' . $row . ', col: ' . $col . '<br>';
}
}
use the following
foreach ($values as $inde => $value) {
foreach ($value as $key => $result) {
echo '['. $inde.'] ---' .$key . '<br>';
}
}
output will be
[0] ---B
[0] ---User
[0] ---0
[0] ---1
[1] ---B
[1] ---User
[1] ---0
[1] ---1
[3] ---B
[3] ---User
[3] ---0
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