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
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;
}
I have a multidimensional array with some duplicate sub-arrays:
$data = array(
2 => array (
'User' => 'cat',
'File_name' => 'cat.docx',
'Document' => 'Document1',
'Date' => '2017-03-02',
'Pages' => 1
),
3 => array (
'User' => 'dog',
'File_name' => 'dog.docx',
'Document' => 'Document2',
'Date' => '2017-03-02',
'Pages' => 3
),
4 => array (
'User' => 'shark',
'File_name' => 'shark.docx',
'Document' => 'Document3',
'Date' => '2017-03-01',
'Pages' => 5
),
5 => array (
'User' => 'dog',
'File_name' => 'dog.docx',
'Document' => 'Document2',
'Date' => '2017-03-02',
'Pages' => 3
),
6 => array (
'User' => 'dog',
'File_name' => 'dog.docx',
'Document' => 'Document2',
'Date' => '2017-03-02',
'Pages' => 3
),
//...
);
I could recognize which items are duplicated with:
$final = $dupli = array();
foreach ($data as $key => $array ) {
if(!in_array($array, $final)){
$final[$key] = $array;
}
else
{
$dupli[$key] = $array;
}
}
print_r($dupli);
But I would like to know what index was the first subarray (the key of this) found, as a message, for example:
$final = $dupli = array();
foreach ($data as $key => $array ) {
if(!in_array($array, $final)){
$final[$key] = $array;
}
else
{
$dupli[$key] = $array;
echo "Line " . $key . " duplicated row, the original row is: " . $originalkey;
}
}
Output:
Line 5 duplicated row, the original row is: 3
Line 6 duplicated row, the original row is: 3
To find the key that has a specific value, you can use array_search().
So, to fetch the key from the final array, this should do it in your else statement:
$dupli[$key] = $array;
$originalkey = array_search($array, $final);
echo "Line " . $key . " duplicated row, the original row is: " . $originalkey . "\n";
Read more about array_search() in the docs
As Magnus Eriksson said you can use array_search, if you use it instead of in_array in your existing cycle you can do the operation only once per cycle:
$final = $dupli = array();
foreach ($data as $key => $array ) {
if(($originalkey = array_search($array, $final)) === false){
$final[$key] = $array;
}
else
{
$dupli[$key] = $array;
echo "Line " . $key . " duplicated row, the original row is: " . $originalkey;
}
}
Using array_search() along with loop you can simplify your code like below :
foreach($data as $index => $value)
{
$key = array_search($value, $data);
if($key!==false && $key != $index){
printf("Line %d duplicated row, the original row is: %d\n",$index, $key);
}
}
Test results on CLI
Script
akshay#db-3325:/tmp$ cat test.php
<?php
$data = array(
2 => array (
'User' => 'cat',
'File_name' => 'cat.docx',
'Document' => 'Document1',
'Date' => '2017-03-02',
'Pages' => 1
),
3 => array (
'User' => 'dog',
'File_name' => 'dog.docx',
'Document' => 'Document2',
'Date' => '2017-03-02',
'Pages' => 3
),
4 => array (
'User' => 'shark',
'File_name' => 'shark.docx',
'Document' => 'Document3',
'Date' => '2017-03-01',
'Pages' => 5
),
5 => array (
'User' => 'dog',
'File_name' => 'dog.docx',
'Document' => 'Document2',
'Date' => '2017-03-02',
'Pages' => 3
),
6 => array (
'User' => 'dog',
'File_name' => 'dog.docx',
'Document' => 'Document2',
'Date' => '2017-03-02',
'Pages' => 3
),
//...
);
foreach($data as $index => $value)
{
$key = array_search($value, $data);
if($key!==false && $key != $index){
printf("Line %d duplicated row, the original row is: %d\n",$index, $key);
}
}
?>
Output
akshay#db-3325:/tmp$ php test.php
Line 5 duplicated row, the original row is: 3
Line 6 duplicated row, the original row is: 3
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 have this php array X.
X= array(
'Parent' => array(
'title' => '123',
)
)
I have this php array Y.
Y = array(
'Parent' => array(
'id' => '16',
'title' => 'T1',
),
'Children' => array(
(int) 0 => array(
'id' => '8',
'serial_no' => '1',
),
(int) 1 => array(
'id' => '9',
'serial_no' => '2',
),
(int) 2 => array(
'id' => '14',
'serial_no' => '6',
)
)
)
I want to copy the Children of array Y to the parent of array X to form array Z such that it looks like this;
Z= array(
'Parent' => array(
'title' => '123',
)
'Children' => array(
(int) 0 => array(
'serial_no' => '1'
),
(int) 1 => array(
'serial_no' => '2'
),
(int) 2 => array(
'serial_no' => '6'
)
)
)
Please note that the id key-value pair was removed from the Children of array Y.
I wrote some code of my own.
$Z = array();
$i=0;
foreach($Y as $temp)
{
$Z['Children'][$i] = $temp['Children'][$i];
unset($Z['Children'][$i]['id'];
$i++;
}
$Z['Parent']=$temp['Parent'];
Unfortunately, there is an undefined index error. How can this be done in php? Forget about my code if there are better approaches.
Actually your approach works too, but you need to iterate over sub-array:
$Z = array();
$i=0;
foreach($Y['Children'] as $temp)
{
$Z['Children'][$i] = $temp;
unset($Z['Children'][$i]['id'];
$i++;
}
or what I may do:
$Z = $X;
$Z['Children'] = array();
foreach ( $Y['Children'] as $child ) {
$Z['Children'][] = array(
'serial_no' => $child['serial_no'],
);
}
You can do like.
$Z = array();
foreach($Y['Children'] as $temp)
{
$Z['Children'][] = array('serial_no' => $temp['serial_no']);
}
$Z['Parent']=$X['Parent'];
I have two arrays
$array1 = array(
0 => array(
'user' => 'user0',
'id' => 'id0'
),
1 => array(
'user' => 'user1',
'id' => 'id1'
),
2 => array(
'user' => 'user2',
'id' => 'id2'
)
);
$array2 = array(
0 => array(
'emp' => 'emp0',
'id' => 'id3'
),
1 => array(
'emp' => 'emp1',
'id' => 'id1'
),
2 => array(
'emp' => 'emp2',
'id' => 'id2'
)
);
i need to loop array 2 first an d give input of id from array1 to the array 1 and search whether the value of id1 from arr1 exists in array2
Maybe this could work? (If I understood your question correctly)
$id_arr = array();
$final_arr = array();
checkArray($array1, $id_arr, $final_arr);
checkArray($array2, $id_arr, $final_arr);
function checkArray($arr, &$id_arr, &$final_arr) {
foreach ($arr as $key => $value) {
if (!in_array($value['id'], $id_arr)) {
$id_arr[] = $value['id'];
$final_arr[] = $value;
}
}
}
var_dump($final_arr);