Find array key value from another key of array in php - php

I want to find one key of array value in another key in the same array.
Following is my array output.
Array
(
[0] => Array
(
[id] => 1187
[user_id] => 168
[content] => Thanks a lot man
[item_id] => 1182
[secondary_item_id] => 1186
)
[1] => Array
(
[id] => 1186
[user_id] => 222
[content] => Great Post
[item_id] => 1182
[secondary_item_id] => 1182
)
[2] => Array
(
[id] => 1183
[user_id] => 185
[content] => Amazing first post
[item_id] => 1182
[secondary_item_id] => 1182
)
[3] => Array
(
[id] => 1184
[user_id] => 179
[content] => Wonder Post
[item_id] => 1182
[secondary_item_id] => 1182
)
[4] => Array
(
[id] => 1185
[user_id] => 168
[content] => Rocking Thanks
[item_id] => 1182
[secondary_item_id] => 1183
)
)
Here you can see id & secondary_item_id key in array, So I want to find which array id is used in secondary_item_id key of array or how can i search id key of array is used in secondary_item_id key in array.
For example.. you can see 'Great Post' id key is used in 'Thanks a lot man' secondary_item_id key of array. So i want to search which array key id is used in secondary_item_id key of array.
I have tried using following way but not working.
$commentData = array();
foreach ($commentQuery as $key => $value) {
if(array_search($value['id'], array_column($commentQuery, 'secondary_item_id'))){
echo "list Found".$value['id'];
}else{
echo "list not Found".$value['id'];
}
}
It's only return list not Found1187.

i hope this works for you
$ch=true;
foreach ($commentQuery as $key => $value) {
foreach ($commentQuery as $key2 => $value2) {
if($value2['id']==$value['secondary_item_id']){
$ch=false;
echo "list Found ".$value['id']." in array key ".$key2;
break;
}
}
if($ch){
echo "list not Found".$value['id'];
}
$ch=true;
}

Try this:-only single foreach loop using
$search_column variable is out of foreach because every time this sentence execute and your code execute slowly
$search_column = array_column($commentQuery, 'secondary_item_id');
foreach ($commentQuery as $key => $value) {
$search = null;
$search = array_search($value['id'], $search_column);
if (gettype($search) == 'integer') {
echo "list Found" . $value['id'];
} else {
echo "list not Found" . $value['id'];
}
echo "<br>";
}

Related

How do you change a Multidimensional Array Key to a Childs Key Value?

I'm trying to rename the parent keys in a Multidimensional Array to the value of a child key.
For example in the code below I would like to change the key [0] to [111] and the key [1] to [222] so it is easy for me to identify keys later for an array merge.
Array (
[0] => Array ( [product_id] => 111 [product_name] => Foo [quantity] => 4 )
[1] => Array ( [product_id] => 222 [product_name] => Bar [quantity] => 2 )
)
I've tried various ways of doing this but after entering a loop, I can't work out how to affect the parent key and assume it's impossible after passing it to a variable. Is there an easy solution to change the key I am missing or is it a case of entering the loop and rebuilding a new array with the desired key?
One-line solution using array_combine and array_column functions:
$result = array_combine(array_column($arr, 'product_id'), $arr);
print_r($result);
The output:
Array
(
[111] => Array
(
[product_id] => 111
[product_name] => Foo
[quantity] => 4
)
[222] => Array
(
[product_id] => 222
[product_name] => Bar
[quantity] => 2
)
)
You need to create a new array, such as:
$original = array(
[0] => Array ( [product_id] => 111 [product_name] => Foo [quantity] => 4 )
[1] => Array ( [product_id] => 222 [product_name] => Bar [quantity] => 2 )
)
$new = array();
foreach ($original as $val) {
$new[$val->product_id] = $val;
}
create new array use foreach to loop through your old array and assign the value of new one
<?php
$oldArray[0] = Array ("product_id"=> 111 , "product_name" => "Foo" , "quantity" => 4 );
$oldArray[1] = Array ("product_id"=> 222 , "product_name" => "Bar" , "quantity" => 2 );
$newArray=array();
foreach($oldArray as $childarray){
$newArray[$childarray['product_id']]=array('product_id'=>$childarray["product_id"],'product_name'=>$childarray["product_name"],'quantity'=>$childarray["quantity"]);
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
?>

PHP Looping multi dim array

Array
(
[stat] => ok
[offset] => 0
[limit] => 50
[total] => 1
[monitors] => Array
(
[monitor] => Array
(
[0] => Array
(
[id] =>
[friendlyname] =>
[url] =>
[type] => 3
[subtype] =>
[keywordtype] =>
[keywordvalue] =>
[httpusername] =>
[httppassword] =>
[port] =>
[interval] => 300
[status] => 2
[alltimeuptimeratio] => 100
[log] => Array
(
[0] => Array
(
[type] => 2
[datetime] => 11/24/2016 04:01:32
)
[responsetime] => Array
(
[0] => Array
(
[datetime] => 12/09/2016 19:34:02
[value] => 109
)
[1] => Array
(
[datetime] => 12/09/2016 19:29:02
[value] => 110
)
[2] => Array
(
[datetime] => 12/09/2016 19:24:02
[value] => 110
)
)
)
)
)
)
I need to get the value of datetime, and value from the responsetime array. I tried the following but it seems to not return anything.
foreach($multidim as $value) {
foreach($value as $key => $val) {
if($key == "responsetime") {
echo $val[3];
}
}
}
Where $multidim is the large multi-dim array listed above. Any help is appreciated as I am not sure where to go from here.
Thank you in advance.
to access all the response times you should do sth like this
foreach($multidim['monitors']['monitor'][0]['responsetime'] as $key => $value) {
//here $key will be 0,1,2,3...
//$value['datetime'] will be 11/24/2016 04:01:32...
//$value['value'] will be 109,110,...
}
that is if you just want to access all the response times of the first monitor. if you want to access all the monitors and their response times you would need 2 loops for example
foreach($multidim['monitors']['monitor'] as $monitorId => $monitorData) {
foreach($monitorData['responsetime'] as $key => $value) {
//here you can access all the variables e.g
//$monitorId will be 0,1,2,3...
//$key will be 0,1,2,3...
//$value['datetime'] will be 11/24/2016 04:01:32...
//$value['value'] will be 109,110,...
}
}
I hope that sends you in the right direction :)

PHP traversing a multidimensional array

I have the following multidimensional array:
Array
(
[0] => 57950340
[1] => SALE-86
[2] => COMPLETE
[3] =>
[4] => 333
[5] => 819
[6] => Array
(
[0] => Array
(
[number] => 1
[product] => Array
(
[id] => 90316
[name] => CLASSIC COCKTAIL
)
[quantity] => 1
[price_variation] => 1
[modifiers] => Array( )
[notes] =>
[unit_price] => 16.3636
[unit_tax] => 1.63636
)
[1] => Array
(
[number] => 2
[product] => Array
(
[id] => 90316
[name] => CLASSIC COCKTAIL
)
[quantity] => 1
[price_variation] => 1
[modifiers] => Array ( )
[notes] =>
[unit_price] => 16.3636
[unit_tax] => 1.63636
)
)
)
I'm trying to loop through the array so that I can echo the name of the product items (held within the array at key 6 and echo each of these out on a separate line with the unit price and an the initial order ID (key 0 of the initial array).
I've been trying to do this for a few hours but am going round in very confusing circles, can anyone shed any light on how I can do this?
<?PHP
$multi_dimensional_array = [...]; // Your array here
$order_id = $multi_dimensional_array[0];
$products_array = $multi_dimensional_array[6];
foreach($products_array as $product) {
echo $product['product']['name']." costs ".$product['unit_price'];
echo " - ORDER: ".$order_id;
echo "<br/>";
}
?>
Useforeach and is_array() to check if the values is array then foreach to access the inside variables then lastly you can echo it.
foreach($array as $key => $val)
{
if(is_array($val){
foreach($val as $key2 => $val2)
{
//print_r($val2); to see the actual data you are accessing
echo "ID: " . $val2['product']['id']. ' Product Name: ' . $val2['product']['name'] . ' Quantity: ' . $val2['quantity'];
}
}
}

Filter multidimensional multilevel array to uniq

I have the next probleme, i want to filter a multidimensional and multi level array with for the uniqe one.
An example:
Array
(
[Home] => Array
(
[Kids] => Array
(
[For sleeping] => Array
(
[0] => Sleeping Bags
[1] => mattress
[2] => mattress
[3] => mattress
[4] => Beds
[5] => Beds
[6] => Beds
[..]
The befored array i want to make it with the uniqe values.
I don't know if this is the fastest / shortest answer, but the code below might work for you:
#Function to make a multidimensional array unique
function makeUnique(&$array)
{
foreach($array as $key => &$value)
{
if(is_array($value))
{
makeUnique($value);
$value = array_unique($value);
}
}
return $array;
}
#Example of your array
$exampleArray = Array(
'Home' => Array(
'Kids' => Array(
'For sleeping' => Array(
0 => 'Sleeping Bags',
1 =>'mattress',
2 =>'mattress')
)
)
);
#Make the array unique and print the results
makeUnique($exampleArray);
print_r($exampleArray);
I have resolved this problem. The solution for this problem is:
$test = Array
(
[Home] => Array
(
[Kids] => Array
(
[For sleeping] => Array
(
[0] => Sleeping Bags
[1] => mattress
[2] => mattress
[3] => mattress
[4] => Beds
[5] => Beds
[6] => Beds
[..]
foreach ($test as $key=>$value){
foreach ($value as $key2 => $value2) {
foreach ($value2 as $key3=>$value3) {
$cat[$key][$key2][$key3]= array_unique($value3);
}
}
}

php delete element from array

Im using cakephp 2.0 and i have data submitted that i want to cleanup, the array structure is below how do i delete the element (quoteitem) where the quantity=null?
i have this but it doesnt work;
foreach($this->request->data['Quoteitem'] as $qi) {
if($qi['quantity']==null){
echo 'quantity is null,delete this quote item from array';
unset($qi);
}
}
structure of array called ($this->request->data)
Array
(
[Quote] => Array
(
[customer_id] => 72
[user_id] => 104
)
[Range] => Array
(
[id] =>
)
[Quoteitem] => Array
(
[0] => Array
(
[product_id] =>
[unitcost] =>
[quantity] => 1
)
[1] => Array
(
[product_id] =>
[unitcost] =>
[quantity] => 22
)
[2] => Array
(
[product_id] => 339
[unitcost] => 5
[quantity] =>
)
)
)
You can remove it using the array keys:
foreach($this->request->data['Quoteitem'] as $key => $qi) {
if($qi['quantity'] == null){
echo 'quantity is null,delete this quote item from array';
unset($this->request->data['Quoteitem'][$key]);
}
}
Note that this will create gaps in the array (nonexistent indexes), usually this won't be a problem but if it is you can re-index the array with array_values().
Foreach makes a copy, try this:
foreach($this->request->data['Quoteitem'] as $key => $qi) {
if($qi['quantity']==null){
echo 'quantity is null,delete this quote item from array';
unset($this->request->data['Quoteitem'][$key]);
}
}

Categories