I want to delete key [Price] but the function which I use for deletion doesn't work for this case
I have:
Array(
[Values] => 1
[Product] => Array(
[Details] => Array(
[ID] => 1
[Price] => Array(
)
)
)
)
My goal is:
Array(
[Values] => 1
[Product] => Array(
[Details] => Array(
[ID] => 1
)
)
)
I use this for removal:
function remove_key($array, $key)
{
foreach($array as $k => $v) {
if(is_array($v)) {
$array[$k] = remove_key($v, $key);
} elseif($k == $key) {
unset($array[$k]);
}
}
return $array;
}
$array = remove_key($array,'Price');
What is wrong here?
<?php
$array = Array(
'Values' => 1,
'Product' => Array(
'Details' => Array(
'id' => 1,
'Price' => Array(
)
)
)
);
unset($array['Product']['Details']['Price']);
echo "<pre>";
print_r($array);
echo "</pre>";
And the output is :
Array
(
[Values] => 1
[Product] => Array
(
[Details] => Array
(
[id] => 1
)
)
)
so if you want to fix your function you must add another condition into first if as so && $k != $key
because you are not getting into elseif and unset is not called
function remove_key($array, $key)
{
foreach($array as $k => $v) {
if(is_array($v) && $k != $key) {
$array[$k] = remove_key($v, $key);
} elseif($k == $key) {
unset($array[$k]);
}
}
return $array;
}
Related
i have data array, and this my array
Array
(
[0] => Array
(
[id] => 9,5
[item] => Item A, Item B
)
[1] => Array
(
[id] => 3
[item] => Item C
)
)
in array 0 there are two ID which I separated using a comma, I want to extract the data into a new array, how to solve this?
so the output is like this
Array
(
[0] => Array
(
[id] => 9
[item] => Item A
)
[1] => Array
(
[id] => 3
[item] => Item C
)
[2] => Array //new array
(
[id] => 5
[item] => Item B
)
)
this my code
$arr=array();
foreach($myarray as $val){
$arr[] = array(
'id' => $val['id'],
'item' => $val['item'],
);
}
echo '<pre>', print_r($arr);
$arr = [
array(
'id' => '9,5',
'item' => 'Item A, Item B'
),
array(
'id' => 3,
'item' => 'Item C'
)
];
$newArr = array_reduce($arr, function($tmp, $ele){
$arrIds = explode(',', $ele['id']);
$arrItems = explode(',', $ele['item']);
forEach($arrIds as $key => $arrId) {
$tmp[] = array('id' => $arrId, 'item' => $arrItems[$key]);
}
return $tmp;
});
The code down below should do the job. But I didn't understand why you didn't create those items seperately in the first place.
foreach ($arr as $i => $data) {
if (!str_contains($data['id'], ',')) continue;
$items = explode(',', $data['item']);
foreach(explode(',', $data['id']) as $i => $id) {
$new = ['id' => $ids[$i], 'item' => $items[$i]];
if ($i) $arr[] = $new;
else $arr[$i] = $new;
}
}
Here is my collected array.
$raw_ar = Array (
0 => Array ( 'ID' => 6, 'pageTitle' => 'First', 'pageContent' => 'http://localhost/cms/1', 'parentID' => 0 ),
1 => Array ( 'ID' => 7, 'pageTitle' => 'Second', 'pageContent' => 'http://localhost/cms/2', 'parentID' => 6 ),
2 => Array ( 'ID' => 8, 'pageTitle' => 'Third', 'pageContent' => 'http://localhost/cms/3', 'parentID' => 6 ) ,
3 => Array ( 'ID' => 9, 'pageTitle' => 'Four', 'pageContent' => 'http://localhost/cms/4', 'parentID' => 0 )
) ;
And my result should be like this
$final_ar = array(
0 => array ( 'ID' => 6, 'pageTitle' => 'First', 'pageContent' => 'http://localhost/cms/1', 'parentID' => 0 ,
'sub_items' => array(
0 => array('ID' => 7, 'pageTitle' =>'second', 'pageContent' => 'http://localhost/cms/2', 'parentID' => 6),
1 => array('ID' => 8, 'pageTitle' => 'Third', 'pageContent' => 'http://localhost/cms/3', 'parentID' => 6),
)
),
1 => array('ID' => 9, 'pageTitle' => 'Four', 'pageContent' => 'http://localhost/cms/4', 'parentID' => 0)
);
And here is my code
$final_ar = array();
foreach ($raw_ar as $value) {
if($value['parentID'] ==0){
$final_ar[] = $value;
}
else{
$pID = $value['parentID'];
foreach ($final_ar as $value1) {
//echo $value1['ID'].'-'.$pID;
if($value1['ID'] == $pID){
//if(isset($value1['sub_items'])){
$value1['sub_items'][] = $value;
//}else
//$value1['sub_items'] = $value;
}
$temp_ar[] = $value1;
}
$exist = 0;
foreach ($final_ar as $key => $val) {
# code...
if($val['ID'] == $temp_ar['ID']){
unset($final_ar[$key]);
$final_ar[$key] = $temp_ar;
$exist =1;
break;
}
}
if($exist == 0)
$final_arr[] = $temp_ar;
//$parent_key = array_column($raw_ar,'ID', 'parentID');
}
}
print_r($final_arr);
And I tried to code it with sub_items . But it helps to create array. But I don't know how to remove existing array once it modifies. It gives the result like this.
Array ( [0] => Array ( [0] => Array ( [ID] => 6 [pageTitle] => First [pageContent] => http://localhost/cms/1 [parentID] => 0 [sub_items] => Array ( [0] => Array ( [ID] => 7 [pageTitle] => Second [pageContent] => http://localhost/cms/2 [parentID] => 6 ) ) ) ) [1] => Array ( [0] => Array ( [ID] => 6 [pageTitle] => First [pageContent] => http://localhost/cms/1 [parentID] => 0 [sub_items] => Array ( [0] => Array ( [ID] => 7 [pageTitle] => Second [pageContent] => http://localhost/cms/2 [parentID] => 6 ) ) ) [1] => Array ( [ID] => 6 [pageTitle] => First [pageContent] => http://localhost/cms/1 [parentID] => 0 [sub_items] => Array ( [0] => Array ( [ID] => 8 [pageTitle] => Third [pageContent] => http://localhost/cms/3 [parentID] => 6 ) ) ) ) )
Try this:
function formatArray($nonFormattedArray) {
$formattedArray = [];
$subItems = [];
foreach ($nonFormattedArray as $item) {
$pid = $item['parentID'];
if ($pid != 0) {
if (isset($subItems[$pid]))
$subItems[$pid][] = $item;
else
$subItems[$pid] = [$item];
} else
$formattedArray[] = $item;
}
foreach ($formattedArray as $key => $parent) {
resolveChild($formattedArray[$key], $subItems);
}
return $formattedArray;
}
function resolveChild(&$parent, &$subItems) {
//return if no child
if (!isset($subItems[$parent['ID']]))
return $parent;
foreach ($subItems[$parent['ID']] as $key => $child) {
if (isset($parent['sub_items']))
$parent['sub_items'][] = resolveChild($subItems[$parent['ID']][$key], $subItems);
else
$parent['sub_items'] = [resolveChild($subItems[$parent['ID']][$key], $subItems)];
}
return $parent;
}
Now, formatArray($nonFormattedArray) should return your desired answer.
This will be independent of the order of your parent and child items and will reduce the total iteration count and execution time.
This will produce an Array as deep as the inheritance in the data.
Note that execution time will increase with the increment in inheritance level.
So many code you have here.
Here's my version:
foreach ($raw_ar as $value) {
if ($value['parentID'] == 0) {
$final_ar[$value['ID']] = $value;
}
}
foreach ($raw_ar as $value) {
$parent_id = $value['parentID'];
if (0 < $parent_id) {
if (!isset($final_ar[$parent_id]['sub_items'])) {
$final_ar[$parent_id]['sub_items'] = [];
}
$final_ar[$parent_id]['sub_items'][] = $value;
}
}
$final_ar = array_values($final_ar); // if you need 0-indexed array
If you're 100% sure that parent items in your array come before child ones - you can join both foreaches into one:
foreach ($raw_ar as $value) {
$parent_id = $value['parentID'];
if ($parent_id == 0) {
$final_ar[$value['ID']] = $value;
} else {
if (!isset($final_ar[$parent_id]['sub_items'])) {
$final_ar[$parent_id]['sub_items'] = [];
}
$final_ar[$parent_id]['sub_items'][] = $value;
}
}
$final_ar = array_values($final_ar); // if you need 0-indexed array
I have following array..I am trying to group this.
Array
(
[0] => Array
(
[Title] => HoMedics MAN-300
[ean] => 31262006288
[upc] => 31262006288
[ProductImageName] =>
[CdnUri] =>
[ASIN] => B000050FEU
[ListPrice] => 129.99
[Status] => 2
[ActualPrice] => 129.99
[ProductID] => 5286728
)
)
I want to group this into
Array
(
[0] => Array
(
[VitalInfo]=>array(
[Title] => HoMedics MAN-300
[ean] => 31262006288
[upc] => 31262006288
[ProductImageName] =>
[CdnUri] =>
[ASIN] => B000050FEU
)
[Price]=>array(
[ListPrice] => 129.99
[Status] => 2
[ActualPrice] => 129.99
[ProductID] => 5286728
)
)
I tried but it doesn't happen as I want ...any help would be great...Thanx in advance..
try this,
CODE :
foreach($old_array as $key_old => $val_old)
{
foreach($val_old as $key => $val)
{
if(in_array($key, $VitalInfo_array))
{
$new_array[$key_old]['VitalInfo'][$key] = $val;
}
else
{
$new_array[$key_old]['Price'][$key] = $val;
}
}
}
OUTPUT :
Array
(
[0] => Array
(
[VitalInfo] => Array
(
[Title] => HoMedics MAN-300
[ean] => 31262006288
[upc] => 31262006288
[ProductImageName] =>
[CdnUri] =>
[ASIN] => B000050FEU
)
[Price] => Array
(
[ListPrice] => 129.99
[Status] => 2
[ActualPrice] => 129.99
[ProductID] => 5286728
)
)
)
DEMO
i hope it will be helpful.
Simply loop through your array and customize a new array accordingly.
Assuming $array is the original array, and $result is the customized array, try this:
foreach ($array as $k => $arr) {
$result[$k]['VitalInfo'] = array(
'Title' => $arr['Title'],
'ean' => $arr['ean'],
'upc' => $arr['upc'],
'ProductImageName' => $arr['ProductImageName'],
'CdnUri' => $arr['CdnUri'],
'ASIN' => $arr['ASIN']
);
$result[$k]['Price'] = array(
'ListPrice' => $arr['ListPrice'],
'Status' => $arr['Status'],
'ActualPrice' => $arr['ActualPrice'],
'ProductID' => $arr['ProductID']
);
}
Input : $info // Your Original Array
Ouput : $finalArr // Your Required Array
$vitalInfo = array ('Title','ean','upc','ProductImageName','CdnUri','ASIN');
$price = array ('ListPrice','Status','ActualPrice','ProductId');
$finalArr = array();
foreach ($info as $arr) {
$result = array();
foreach($arr as $k => $v){
if(in_array($k,$vitalInfo))
$result['VitalInfo'][$k] = $v;
else if(in_array($k,$price))
$result['Price'][$k] = $v;
}
$finalArr[] = $result;
}
If you are grouping this in php, have a look at these answers.
Or just copy this:
$input = [0 => [
'Title' => 'HoMedics MAN-300',
'ean' => 31262006288,
'upc' => 31262006288,
'ProductImageName' => '',
'CdnUri' => '',
'ASIN' => 'B000050FEU',
'ListPrice' => 129.99,
'Status' => 2,
'ActualPrice' => 129.99,
'ProductID' => 5286728
]];
foreach ($input as $in){
$out['VitalInfo'] = [];
$out['Price'] = [];
foreach ($in as $key => $i){
if (in_array($key, ['Title', 'ean', 'upc', 'ProductImageName', 'CdnUri', 'Asin'])){
$out['VitalInfo'][] = [$key => $i];
} else {
$out['Price'][] = [$key => $i];
}
}
$output[]=$out;
}
echo '<pre>';
var_dump($output);
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' : '-';
I array of arrays, what look something like that:
$messages = array (
0 =>
array(
'keyT' => 'id.key'
'mess' => array(
array(1,0)
)
...
)
I want to merge mess preperties of arrays where 'keyT' is not equals.
I run trought the arrays:
foreach ($messages as $k => $current) {
foreach ($messages as $ke => $all) {
if ($current['keyT'] == $all['keyT']) {
array_merge( ... )
}
}
}
But this not deve me any results. Maybe somebody can help me. Thanks!
Try this code
$messages = array(
0 =>
array(
'keyT' => 'A',
'mess' => array(
array(1, 0)
)
),
1 =>
array(
'keyT' => 'A',
'mess' => array(
array(1, 2)
)
),
2 =>
array(
'keyT' => 'B',
'mess' => array(
array(3, 4)
)
)
);
$result = array();
foreach ($messages as $msg) {
$key = $msg['keyT'];
if (!isset($result[$key])) {
$result[$key] = array();
}
$result[$key] = array_merge($result[$key], $msg['mess']);
}
print_r($result);
Output
Array
(
[A] => Array
(
[0] => Array
(
[0] => 1
[1] => 0
)
[1] => Array
(
[0] => 1
[1] => 2
)
)
[B] => Array
(
[0] => Array
(
[0] => 3
[1] => 4
)
)
)