how to check if value in mutidimension array are the same? - php

if I have multidimensional array like this
Array
(
[0] => Array
(
[value] => Dummy
[key] => 5501____I2-6
)
[1] => Array
(
[value] => Dummy
[key] => 5497____I2-2
)
[2] => Array
(
[value] => Dummy
[key] => 5502____I2-7
)
[3] => Array
(
[value] => Dummy
[key] => 5499____I2-4
)
[4] => Array
(
[value] => Dummy
[key] => 5496____I2-1
)
[5] => Array
(
[value] => Dummy
[key] => 5500____I2-5
)
[6] => Array
(
[value] => Dummy
[key] => 5503____I2-8
)
[7] => Array
(
[value] => Dummy
[key] => 5498____I2-3
)
)
How can I check if each value in [value] => are the same ?
thank for help

For two-dimensional array you can check duplicate value by following.
$array = array();
$temp_array=array();
$array[0]['value']='Dummy';
$array[0]['key']='1';
$array[1]['value']='Dummy';
$array[1]['key']='2';
$array[2]['value']='Not Dummy';
$array[2]['key']='3';
foreach($array as $k=>$step1){
if(in_array($step1['value'],$temp_array)){
// In this you can find out duplicate values.
}
$temp_array[$k]=$step1['value'];
}

thank everyone for help.
I found the way for fix it now.
I'm not sure it 's best way but It can fix this problem for me.
$data = Array
(
[0] => Array
(
[value] => Dummy
[key] => 5501____I2-6
)
[1] => Array
(
[value] => Dummy
[key] => 5497____I2-2
)
[2] => Array
(
[value] => Dummy
[key] => 5502____I2-7
)
[3] => Array
(
[value] => Dummy
[key] => 5499____I2-4
)
[4] => Array
(
[value] => Dummy
[key] => 5496____I2-1
)
[5] => Array
(
[value] => Dummy
[key] => 5500____I2-5
)
[6] => Array
(
[value] => Dummy
[key] => 5503____I2-8
)
[7] => Array
(
[value] => Dummy
[key] => 5498____I2-3
)
)
$newarray = array();
for ($i=0; $i < count($data); $i++) {
array_push($newarray,$json_data[$i]['value']);
}
if (count(array_unique($newarray)) == 1){
echo 'The same';
}
else{
echo 'not The same';
}

Related

php merge duplicate values in a array

I apologize for not being word-perfect in English.
I have this result from a foreach loop in php.
my file is jason.
Merging more value into one array
Array
(
[777565] => Array
(
[0] => Array
(
[0] => 777565-1
[1] => 777565-2
)
[1] => Array
(
[0] => 777565-3
[1] => 777565-4
)
[2] => Array
(
[0] => 777565-5
[1] => 777565-6
)
)
[777566] => Array
(
[0] => Array
(
[0] => 777566-1
[1] => 777566-2
)
[1] => Array
(
[0] => 777566-3
[1] => 777566-4
)
[2] => Array
(
[0] => 777566-5
[1] => 777566-6
)
)
)
but, I want Something like this:
Array
(
[777565] => Array
(
[0] => 777565-1
[1] => 777565-2
[2] => 777565-3
[3] => 777565-4
[4] => 777565-5
[5] => 777565-6
)
[777566] => Array
(
[0] => 777566-1
[1] => 777566-2
[2] => 777566-3
[3] => 777566-4
[4] => 777566-5
[5] => 777566-6
)
)
I tried hard and searched the internet but I could not find any way.
Of course, I have the ability to move it to the database first and then to the array, but I think there should be a faster way. What do you think?
thanks for reply.
If you have no problem looping through it and flatten the array according to your desire then you can try this:
$parent =
Array
(
[777565] => Array
(
[0] => Array
(
[0] => 777565-1
[1] => 777565-2
)
[1] => Array
(
[0] => 777565-3
[1] => 777565-4
)
[2] => Array
(
[0] => 777565-5
[1] => 777565-6
)
)
[777566] => Array
(
[0] => Array
(
[0] => 777566-1
[1] => 777566-2
)
[1] => Array
(
[0] => 777566-3
[1] => 777566-4
)
[2] => Array
(
[0] => 777566-5
[1] => 777566-6
)
)
);
$length = count($parent);
$result=[];
for($i=0; $i<$length; $i++){
for($j=0; $j<3; $j++){
$l=0;
for($k=0; $k<2; $k++){
$result[777565+$i][$j][$l++] = $parent[777565+$i][$j][$k];
}
}
}

Extract array with same name from multidimensional array

I have this Array from a json that represent a form. I have to collect all the [components] in a new array
Array
(
[components] => Array
(
[0] => Array
(
[key] => number1
[type] => number
[input] => 1
[label] => Test Number Field
)
[1] => Array
(
[key] => testRadioFiled
[type] => radio
[label] => Test Radio Filed
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
[shortcut] =>
)
)
)
[2] => Array
(
[key] => testSelectBoxField
[type] => selectboxes
[input] => 1
[label] => Test Select Box Field
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
[shortcut] =>
)
)
)
[3] => Array
(
[key] => testSelectField
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
)
)
[type] => select
[input] => 1
[label] => Test Select Single Field
)
[4] => Array
(
[key] => testPanel
[type] => panel
[input] =>
[label] => Panel
[title] => Test Panel
[components] => Array
(
[0] => Array
(
[key] => testSelectMultipleField
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[mask] =>
[sort] =>
[type] => select
[input] => 1
[label] => Test Select Multiple Field
)
[1] => Array
(
[key] => testColumns
[type] => columns
[label] => Test Columns
[columns] => Array
(
[0] => Array
(
[key] => column
[type] => column
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => radio
[type] => radio
[input] => 1
[label] => Radio
[values] => Array
(
[0] => Array
(
[label] => first
[value] => first
[shortcut] =>
)
[1] => Array
(
[label] => second
[value] => second
[shortcut] =>
)
[2] => Array
(
[label] => third
[value] => third
[shortcut] =>
)
)
)
)
)
[1] => Array
(
[key] => column
[type] => column
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => testSelectField2
[tab] => 1
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[type] => select
[input] => 1
[label] => Test Select Single Field
)
)
)
)
)
)
[collapsible] =>
)
[5] => Array
(
[key] => tabs2
[mask] =>
[type] => tabs
[input] =>
[label] => Tabs
[components] => Array
(
[0] => Array
(
[key] => tab2
[label] => Tab 1
[components] => Array
(
[0] => Array
(
[key] => columns2
[tab] => 0
[mask] =>
[type] => columns
[input] =>
[label] => Columns
[logic] => Array
(
)
[columns] => Array
(
[0] => Array
(
[key] => column
[type] => column
[input] =>
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => number4
[type] => number
[input] => 1
[label] => Test Number Field in Column
)
)
)
[1] => Array
(
[key] => column
[type] => column
[input] =>
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => number3
[type] => number
[input] => 1
[label] => Test Number Field in Column
)
)
)
)
)
)
)
[1] => Array
(
[key] => tab2
[label] => Tab 2
[components] => Array
(
[0] => Array
(
[key] => testSelectField1
[tab] => 1
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[type] => select
[input] => 1
[label] => Test Select Single Field
)
)
)
)
)
)
)
I solved making a serious of foreach and in each loop I used logic:
If not exist an array "components" -> check if is type columns
if yes check if exist "components" and saved in new array
if not save in new array
If exist again check apply the same condition of point 1 this for 3 or 4 sublevel.
This is the final array that I need
Array
(
[0] => Array
(
[key] => number1
[type] => number
[input] => 1
[label] => Test Number Field
)
[1] => Array
(
[key] => testRadioFiled
[type] => radio
[label] => Test Radio Filed
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
[shortcut] =>
)
)
)
......
[2] => Array
(
[key] => testSelectMultipleField
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[mask] =>
[sort] =>
[type] => select
[input] => 1
[label] => Test Select Multiple Field
)
}
This code make the job but can't be the final solution.
$test_array = Array();
foreach($data['components'] AS $comp_1){
if(!isset($comp_1['components'])){
if($comp_1['type'] != 'columns'){
$test_array[] = $comp_1;
} else {
foreach($comp_1['columns'] as $col_1){
if(!isset($col_1['components'])){
$test_array[] = $col_1;
} else {
foreach($col_1['components'] AS $col_comp_1){
if(!isset($col_comp_1['components'])){
$test_array[] = $col_comp_1;
}
}
}
}
}
} else {
foreach($comp_1['components'] AS $comp_2){
if(!isset($comp_2['components'])){
if($comp_2['type'] != 'columns'){
$test_array[] = $comp_2;
} else {
foreach($comp_2['columns'] as $col_2){
if(!isset($col_2['components'])){
$test_array[] = $col_2;
} else {
foreach($col_2['components'] AS $col_comp_2){
if(!isset($col_comp_2['components'])){
$test_array[] = $col_comp_2;
}
}
}
}
}
} else {
foreach($comp_2['components'] AS $comp_3){
if(!isset($comp_3['components'])){
if($comp_3['type'] != 'columns'){
$test_array[] = $comp_3;
} else {
foreach($comp_3['columns'] as $col_3){
if(!isset($col_3['components'])){
$test_array[] = $col_3;
} else {
foreach($col_3['components'] AS $col_comp_3){
if(!isset($col_comp_3['components'])){
$test_array[] = $col_comp_3;
}
}
}
}
}
}
}
}
}
}
}
My question is there is a way for extract all the "components" array that of coarse take care if not contain other "components" array ?
If I understood you correctly recursion can help:
function extractComponents($arrayWithComponents)
{
$components = [];
if (!isset($arrayWithComponents['components']) || !is_array($arrayWithComponents['components'])) {
return $components;
}
foreach ($arrayWithComponents['components'] as $component) {
if (isset($component['columns'])) {
foreach ($component['columns'] as $column) {
$components = array_merge($components, extractComponents($column));
}
} else {
$components = array_merge($components, extractComponents($component));
unset($component['components']);
$components[] = $component;
}
}
return $components;
}
var_dump(extractComponents($data));
Example.

Read complex array from php

I get this array in response from webservice, so How do I read it in a foreach cycle? or some easy way to read it. The [group] they are more than 12 [id]
there is.
Array
( [response] => Array (
[single] => Array ( [parameters] => Array ( [0] => Array ( [name] => msgCode [value] => 0101 )
[1] => Array ( [name] => msgDesc [value] => OK )
[2] => Array ( [name] => status [value] => 1)
[3] => Array ( [name] => message [value] => Normal) ) )
[group] => Array ( [id] => N4BD767 [parameters] => Array ( [0] => Array ( [name] => idFee [value] => 000 ) echo
[1] => Array ( [name] => typeFee [value] => Cuota)
[2] => Array ( [name] => entryDate [value] => 2014-12-17T14:06:47-03:00 )
[3] => Array ( [name] => expirationDate [value] => 2015-12-05T00:00:00-03:00)
[4] => Array ( [name] => amountOrigin [value] => 221980)
[5] => Array ( [name] => surcharges [value] => 1856)
[6] => Array ( [name] => entity [value] => ONLINE)
[7] => Array ( [name] => feeStatus [value] => inicial )
[8] => Array ( [name] => tranNumber [value] => 27) ) ) ) )
Okay guys, this is what I did and it work fine. With no foreach.
$output = array();
array_walk_recursive($result, function($item,$key) use (&$output){
array_push($output,$key,$item);
});
echo var_dump($output);

How to access 2D array fields in PHP

-----problem solved, see the update 2 below----
I put all MySQL query results into a 2D array:
$suppDescription=mysql_query($query);
$rows = mysql_num_rows($results);
$allSupplierInfo=array();
for($i=0; $i<$rows; $i++){
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
But now I cannot access the $allSuppliersInfo fields.
echo $allSupplierInfo[1][1]; // prints out 'Array'
echo $allSupplierInfo[1]['id']; //prints out nothing
What am I doing wrong?
------- UPDATE-----
print_r($allSupplierInfo) prints the following, so it looks like the loop is not working as I wanted it to:
Array (
[0] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) )
[1] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) )
[2] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) )
[3] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) )
[4] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) [4] => Array ( [0] =>ID_E[1] => Name_E [2] => Address_E [3] => Address_E ) ) )
------ UPDATE 2-----
Using the while loop, as suggested by RiggsFolly, solved the problem and I can access the fields as I initially wanted. I still do not understand why the for loop I used did not loop as I thought it would - any explanation would be greatly appreciated.
I think you got confused with your mysql result processing, you are using $result when checking for the number of rows returned but it should be $suppDescription.
This means that your for loop will not run as you will be getting 0 or probbaly FALSE as a response to mysql_num_rows($suppDescription);
$suppDescription=mysql_query($query);
$rows = mysql_num_rows($suppDescription);
$allSupplierInfo=array();
for($i=0; $i<$rows; $i++){
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
Also this is easier done with a while loop, then you just process whatever is returned and dont need to bother getting the number of rows.
$suppDescription=mysql_query($query);
$allSupplierInfo=array();
while ( $rows = mysql_fetch_row($suppDescription) ) {
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}

Retrieve the value from the array and then add it to the new PHP

i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.

Categories