Array
Array (
[0] => Array (
[0] => Array ( [planoption_id] => 1 )
[1] => Array ( [planoption_id] => 2 )
[2] => Array ( [planoption_id] => 3 )
[3] => Array ( [planoption_id] => 4 )
[4] => Array ( [planoption_id] => 5 )
[5] => Array ( [planoption_id] => 6 )
[6] => Array ( [planoption_id] => 7 )
[7] => Array ( [planoption_id] => 53 )
[8] => Array ( [planoption_id] => 1 )
[9] => Array ( [planoption_id] => 2 )
)
)
how to find the value in givene array.above array is $arraypush ?
if (in_array('2', $arraypush)) {
echo "INCLUDED";
} else {
echo "-";
}
How can i do this ?
This should work for all multidimensional arrays :
function findInArray($array, $value){
foreach($array as $k=>$v){
if((is_array($v) && findInArray($v, $value)) || ($v == $value)){
return "INCLUDED";
}
}
return false;
}
I think you want like this:-
<?php
$arraypush = Array ( 0 => Array ( 0 => Array ( 'planoption_id' => 1 ), 1 => Array ( 'planoption_id' => 2 )));
echo "<pre/>";print_r($arraypush);
foreach($arraypush as $key=>$value){
foreach($value as $key1=>$val){
if(in_array('2', $val) == true){
echo "value exist at the initial array [".$key.']['.$key1.'] index and is INCLUDED.';
}
}
}
?>
Output:- https://eval.in/388240
The in_array() function in your code is searching for the data in $arraypush and the data is in $arraypush[0]
And you can't compare a value with a array in in_array() function, in yor array the 'needle' isn't a value, is an array so you have to make an array who gets the value in a format comparable whit the array you have
$arraypush = array(
0 => array(
0 => array( 'planoption_id' => 1 ),
1 => array( 'planoption_id' => 2 ),
2 => array( 'planoption_id' => 3 ),
3 => array( 'planoption_id' => 4 ),
4 => array( 'planoption_id' => 5 ),
5 => array( 'planoption_id' => 6 ),
6 => array( 'planoption_id' => 7 ),
7 => array( 'planoption_id' => 53 ),
8 => array( 'planoption_id' => 1 ),
9 => array( 'planoption_id' => 2 )
)
);
//gettig the value for the search
$foo = $_GET['foo'];
// making the 'needle' in a array format like your array definition
$foo_array = array('planoption_id' => $foo);
//use in_array() for search your array-needle
//in the $arraypush[0] where is the data, not in $arraypush only
if(in_array($foo_array, $arraypush[0]))
{
die($foo);
}
else
{
die("-");
}
Try this I think is help full to you.
<?php
$a = array (
0 => array (
0 => array ( 'planoption_id' => 1 ),
1 => array ( 'planoption_id' => 2 ),
2 => array ( 'planoption_id' => 3 ),
3 => array ( 'planoption_id' => 4 ),
4 => array ( 'planoption_id' => 5 ),
5 => array ( 'planoption_id' => 6 ),
6 => array ( 'planoption_id' => 7 ),
7 => array ( 'planoption_id' => 53 ),
8 => array ( 'planoption_id' => 1 ),
9 => array ( 'planoption_id' => 2 ),
)
);
function multi_in_array($value, $array)
{
foreach ($array AS $item)
{
if (!is_array($item))
{
if ($item == $value)
{
//return true;
echo "INCLUDED";
}
continue;
}
if (in_array($value, $item))
{
//return true;
echo "INCLUDED";
}
else if (multi_in_array($value, $item))
{
//return true;
echo "-";
}
}
//return false;
echo "-";
}
echo multi_in_array(2, $a);
Output:- -INCLUDED-------INCLUDED--
The following even works for arrays of arbitrary dimensions:
$found = false;
$needle = 2;
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($it as $v) {
if ($v == $needle) {
$found = true;
break;
}
}
echo $found ? 'INCLUDED' : '-';
Related
I have an array of arrays like this :
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '103',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Balance',
1 => 'ID',
),
1 =>
array (
0 => '4533',
1 => '101',
)
),
'data3' => array (
0 =>
array (
0 => 'Active',
1 => 'ID',
),
1 =>
array (
0 => 'Yes',
1 => '101',
),
2 =>
array (
0 => 'No',
1 => '103',
)
),
);
Here is the current working answer by user #Nigel Ren I have to this question here.
$store = [];
$headers = [];
foreach ( $data as $set ) {
$headerRow = array_shift($set);
// Collect all header columns
$headers = array_merge($headers, $headerRow);
foreach ( $set as $index => $list ){
// Create associative list of data so they can be combined (i.e. ID fields)
$list = array_combine($headerRow, $list);
// Use ID value as key and create if needed
if ( !isset($store[$list["ID"]]) ) {
$store[$list["ID"]] = $list;
}
else {
$store[$list["ID"]] = array_merge($store[$list["ID"]], $list);
}
}
}
$headers = array_unique($headers);
$output = [ 'output' => [$headers]];
// Create template array, so that missing fields will be set to null
$blank = array_fill_keys($headers, null);
foreach ( $store as $dataRow ) {
// Fill in the fields for this ID and then change to numeric keys
$output['output'][] = array_values(array_merge($blank, $dataRow));
}
print_r($output);
Above code works perfectly for the above given array.
Here are the new cases I need to handle:
CASE 1 :
When the data contains only one array where the ID are same :
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '101',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Balance',
1 => 'ID',
),
),
'data3' => array (
0 =>
array (
0 => 'Active',
1 => 'ID',
),
),
);
In this case Nigel's answer doesn't output both the both records for the same ID. It outputs just a single record.
Expected output when there's a single array with duplicate ID's present :
Array
(
[output] => Array
(
[0] => Array
(
[0] => ID
[1] => PinCode
[2] => Date
)
[1] => Array
(
[0] => 101
[1] => 454075
[2] => 2012-03-03
)
[2] => Array
(
[0] => 101
[1] => 786075
[2] => 2012-09-05
)
)
)
CASE 2 :
When any of the array's other than the first array contain's entries for multiple same ID.
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '103',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Order',
1 => 'ID',
),
1 =>
array (
0 => 'Colgate',
1 => '101',
),
2 =>
array (
0 => 'Waffles',
1 => '101',
)
),
);
Expected output in this case :
Array
(
[output] => Array
(
[0] => Array
(
[0] => ID
[1] => PinCode
[2] => Date
[3] => Order
)
[1] => Array
(
[0] => 101
[1] => 454075
[2] => 2012-03-03
[3] => Colgate
)
[2] => Array
(
[0] => 101
[1] => 454075
[2] => 2012-03-03
[3] => Waffles
)
[3] => Array
(
[0] => 103
[1] => 786075
[2] => 2012-09-05
[3] =>
)
)
)
How do I do it?
you can get the desired output by using a loop:
$desired_array = array();
$n_data1 = count($data['data1']);
$n_data2 = count($data['data2']);
$n_data3 = count($data['data3']);
for ($i=0; $i < $n_data1; $i++) {
if ($n_data2 > $i) {
foreach ($data['data2'][$i] as $key => $value) {
if(!in_array($value,$data['data1'][$i])){
$data['data1'][$i][]=$value;
}
}
} else {
$data['data1'][$i][]= null;
}
}
for ($i=0; $i < $n_data1; $i++) {
if ($n_data3 > $i) {
foreach ($data['data3'][$i] as $key => $value) {
if(!in_array($value,$data['data1'][$i])){
$data['data1'][$i][]=$value;
}
}
} else {
$data['data1'][$i][]= null;
}
}
$desired_array = $data['data1'];
hi i want to create an array dynamically with the index value and the key value.
here $head is the array name and $values is the key value
$head = Array ( [0] => Dis_id [1] => Dis_Desc [2] => Dis_Per [3] => Dis_val )
$values = Array ([0] => Dl-Dis1 [1] => Discount [2] => 7.500 [3] => 26.25 )
Array ([0] => Dl-Dis2 [1] => Discount [2] => 2.500 [3] => 73.13 )
foreach($values as $valu => $key)
{
$value = $value + array($head[$valu]=>$key.",");
}
echo '<pre>';
print_r($value);
The output will be as
Array
(
[Dis_id] => Dl-Dis2,
[Dis_Desc] => Discount,
[Dis_Per] => 2.500,
[Dis_val] => 73.13,
)
But the output i need is as follows
Array
(
[Dis_id] => Dl-Dis1,
[Dis_Desc] => Discount,
[Dis_Per] => 7.500,
[Dis_val] => 26.25,
)
Array
(
[Dis_id] => Dl-Dis2,
[Dis_Desc] => Discount,
[Dis_Per] => 2.500,
[Dis_val] => 73.13,
)
i dont know how to do please help me
update to get my output
$values = array(array (0 => 'Dl-Dis1', 1 => 'Discount', 2 => 7.500, 3 => 26.25 ), array (0 => 'Dl-Dis2', 1 => 'Discount', 2 => 2.500, 3 => 73.13 )) ;
thank u all
You may need to use array_combine. While under the loop, combine the $head and the values itself to it and put it inside a new container. Consider this example:
$head = Array ( 0 => 'Dis_id', 1 => 'Dis_Desc', 2 => 'Dis_Per', 3 => 'Dis_val', );
$values = array(array (0 => 'Dl-Dis1', 1 => 'Discount', 2 => 7.500, 3 => 26.25 ), array (0 => 'Dl-Dis2', 1 => 'Discount', 2 => 2.500, 3 => 73.13 )) ;
$new_values = array();
foreach($values as $value) {
$new_values[] = array_combine($head, $value);
}
Sample Output
You can do it like this:
$head = array ( 0 => 'Dis_id', 1 => 'Dis_Desc', 2 => 'Dis_Per', 3 => 'Dis_val' );
$values = array(array (0 => 'Dl-Dis1', 1 => 'Discount', 2 => '7.500', 3 => 26.25 ),
array (0 => 'Dl-Dis2', 1 => 'Discount', 2 => 2.500, 3 => 73.13 ));
$result = array();
foreach($values as $value) {
$res = array();
foreach ($value as $key => $val) {
$res[$head[$key]] = $val;
}
$result[] = $res;
}
echo '<pre>';
print_r($result);
I have 2 arrays:
Array ( [0] => Array ( [intTrackId] => 41 [intAverageRating] => 10 [bolNewRelease] => 0 [dtDateAdded] => 2013-03-08 17:32:26 ) [1] => Array ( [intTrackId] => 1 [intAverageRating] => 7 [bolNewRelease] => 0 [dtDateAdded] => 2013-03-08 18:54:35 ))
Array ( [0] => Array ( [intTrackId] => 41 [intAverageRating] => 5.5000 [bolNewRelease] => 1 [dtDateAdded] => 2014-03-25T09:39:28Q ) [1] => Array ( [intTrackId] => 361 [intAverageRating] => 8.0000 [bolNewRelease] => 1 [dtDateAdded] => 2014-03-25T09:39:28Q ))
I want to remove the items in the second which have a matching track ID in the first. So in this example, I would get:
Array ( [0] => Array ( [intTrackId] => 361 [intAverageRating] => 8.0000 [bolNewRelease] => 1 [dtDateAdded] => 2014-03-25T09:39:28Q ))
Is this possible with array_filter or is this a little complex for that?
Just use array_udiff() - it's intended to do this:
$one = Array (
0 => Array ('intTrackId' => 41, 'intAverageRating' => 10, 'bolNewRelease' => 0, 'dtDateAdded' => '2013-03-08 17:32:26' ),
1 => Array ('intTrackId' => 1, 'intAverageRating' => 7, 'bolNewRelease' => 0, 'dtDateAdded' => '2013-03-08 18:54:35' )
);
$two = Array (
0 => Array ('intTrackId' => 41, 'intAverageRating' => 5.5000, 'bolNewRelease' => 1, 'dtDateAdded' => '2014-03-25T09:39:28Q' ),
1 => Array ('intTrackId' => 361, 'intAverageRating' => 8.0000, 'bolNewRelease' => 1, 'dtDateAdded' => '2014-03-25T09:39:28Q' )
);
$result = array_udiff($two, $one, function($x, $y)
{
return $x['intTrackId']-$y['intTrackId'];
});
Yes it can be done with array_filter:
$array1 = array(...);
$array2 = array(...);
$newArray = array_filter($array2, function($item) use ($array1){
foreach($array1 as $elem){
if($item['intTrackId'] == $elem['intTrackId']){
return false;
}
}
return true;
});
I would first create a loop and store all track IDs from the first array in a separate array.
Then I'd loop over the second array and delete those keys that exist in the track ID array.
$track_ids = array();
foreach($array1 as $index => $items) {
$track_ids[$items['intTrackId']] = $index;
}
foreach($array2 as $items) {
if (isset($track_ids[$items['intTrackId']])) {
unset($array2[$track_ids[$items['intTrackId']]]);
}
}
How combine arrays in this way?
source:
Array
(
[0] => Array
(
[id] => 3
[title] => book
[tval] => 10000
)
[1] => Array
(
[id] => 3
[title] => book
[tval] => 1700
)
[3] => Array
(
[id] => 27
[title] => fruit
[tval] => 3000
)
.......
)
result:
Array
(
[0] => Array
(
[id] => 3
[title] => book
[tval] => 10000,1700
)
[1] => Array
(
[id] => 27
[title] => fruit
[tval] => 3000
)
.......
)
please help to solve this problem,
thanks!!!
sorry for bad english(
This should work:
$result = array();
foreach($array as $elem) {
$key = $elem['id'];
if (isset($result[$key])) {
$result[$key]['tval'] .= ',' . $elem['tval'];
} else {
$result[$key] = $elem;
}
}
This basically groups elements by id, concatenating tvals (separated by ,).
Simply building slightly on user576875's method:
$a = array ( 0 => array ( 'id' => 3,
'title' => 'book',
'tval' => 10000
),
1 => array ( 'id' => 3,
'title' => 'book',
'tval' => 1700
),
3 => array ( 'id' => 27,
'bcalias' => 'fruit',
'tval' => 3000
)
);
$result = array();
foreach ($a as $elem) {
$key = $elem['id'];
if (isset($result[$key])) {
$result[$key]['tval'] .= ',' . $elem['tval'];
} else {
$result[$key] = $elem;
}
}
$result = array_merge($result);
var_dump($result);
gives a result of:
array
0 =>
array
'id' => int 3
'title' => string 'book' (length=4)
'tval' => string '10000,1700' (length=10)
1 =>
array
'id' => int 27
'bcalias' => string 'fruit' (length=5)
'tval' => int 3000
The only real difference is the array_merge() to reset the keys
I Have the next array:
Array
(
[1000] => Array
(
[pv] => 81
)
[1101] => Array
(
[1102] => Array
(
[pv] => 33
)
[1103] => Array
(
[pv] => 15
)
[pv] => 72
)
)
I want to make new array from this like this:
Array(
[1000] => Array(['generation'] => 1, ['pv'] => 81)
[1101] => Array(['generation'] => 1, ['pv'] => 72)
[1102] => Array(['generation'] => 2, ['pv'] => 33)
[1103] => Array(['generation'] => 2, ['pv'] => 15)
)
Generation is a level of array's deep. There are a lot of levels.
Thanks.
Be creative...
function coffee($elm, $cur_key=NULL, $level=0, &$push_arr=NULL){
if(!is_array($push_arr)) $pusH_arr = array();
$level++;
foreach($elm as $key=>$val){
if($key == 'pv'){
$push_arr[$cur_key] = array(
'generation' => $level,
'pv' => $val
);
}
else if(is_array($val)){
coffee($val, $key, $level, $push_arr);
}
else{
// unexpected value
}
}
return $push_arr;
}
// Usage:
$normalised_array = coffee($array_from_your_question);