I have a multidimensional array as below. I wish to check if the key [type] is equal to "select". If so, I wish to display the value of key [value]. How do I actually output the value of key [value] which is "Section 100." In the code below, it only outputs the value "select".
array_walk_recursive( $product_extras, function ($value, $key ) {
if( $key === 'type' && $value == 'select' ) {
echo "[$value]"; //This outputs "select". But I actually need the value from key [value]
}
});
My array.
Array
(
[product_id] => 8928
[title] => Example of an Event
[groups] => Array
(
[8929] => Array
(
[8932] => Array
(
[type] => select
[label] => Section
[id] => pewc_group_8929_8932
[group_id] => 8929
[field_id] => 8932
[value] => Section 100
[flat_rate] => Array
(
)
)
)
)
)
If you`re able to pass these ids as parameters in that function you should be able to do something like:
$product_extras['groups'][$runtime_group_id][$runtime_field_id]
With this you should access
Array(
[type] => select
[label] => Section
[id] => pewc_group_8929_8932
[group_id] => 8929
[field_id] => 8932
[value] => Section 100
[flat_rate] => Array
(
using array_map() and recursive(call itself)
call myfunction and send each value inside the array
function yourOuterFunction(){
//your code....
//your array $product_extras .......
array_map("myfunction",$product_extras);
function myfunction($value) {
//search is array or not
if(is_array($value)) {
//search array key and value
if(array_key_exists("type",$value) && array_search("select",$value) == true){
echo "Condition true FOUND..";
print_r($value);
}
//if not found then call itself
else if(is_array($value)) {
array_map("myfunction",$value);
}
}
}//myfunction close
}
Related
I have a query result set which I loop through. Depending on the data inside the result set, I want to add it to the array, or if the specific value contains an ID, loop again, and add it at the current position. I want to end up with something like this.
Array
(
[0] => Array
(
[title] => Array
(
[0] => Array
(
[value] => Lorem ipsum
)
)
[uid] => Array
(
[0] => Array
(
[uid] => Array
(
[value] => 1
)
[field_name] => Array
(
[value] => John Doe
)
)
)
)
)
I accomplished the result, but I'm looping over and over again, I would like it to be recursive. So I ended up doing something like this.
foreach ($fields as $nr => $field){
if ($field_type == 'entity_reference'){
// Query again
...
foreach ($fields2 as $nr2 => $field2){
if ($field_type2 == 'entity_reference'){
// Query again
...
} else {
$return[$nr][$field][$nr2][$fieldl2] = $value2;
}
}
} else {
$return[$nr][$field][] = $value;
}
}
How can I make it recursive, so I don't have to loop many times?
I am needing to echo out the [number], but as you can see each array has a different parent [], how do I by pass the first one and get go to the [number]?
I basically need to skip over first [], and go to the second on that is [number]
Array
(
[e2a4789d22ff47779722b8d8643894cd] => Array
(
[type] => workphone
[visibility] => public
[number] => 999-999-9999
[id] => 2
[order] => 0
[preferred] => 1
)
)
Array
(
[1603ebeff250437480f5ce046cac36aa] => Array
(
[type] => workphone
[visibility] => public
[number] => 999-999-9999
[id] => 3
[order] => 0
[preferred] => 1
)
)
Array
(
[215590630122] => Array
(
[type] => workphone
[visibility] => public
[number] => 999-999-9999
[order] => 0
[preferred] =>
)
)
Your solution for this is using the foreach-loop. Which gives you then the value of the element as variable you tell PHP to assign to.
foreach($array as $element) {
}
You have to use reset function to get the first element of array.
e.g.
$firstElement = reset($arr);
echo $firstElement['number'];
You can just loop over the elements in the array(s) using foreach.
foreach($data as $ele){
foreach($ele as $id=>$val){
echo $val['number'];
}
}
Just an example
$array = $yourarray;
foreach($array as $k=>$v) {
echo $v['number'] . '<br>';
}
hope this helps...
The first bracket is just a unique key index foreach subsequent set of data. for example you can get the first set by accessing through the key like this
$data['e2a4789d22ff47779722b8d8643894cd']
// will return
Array
(
[type] => workphone
[visibility] => public
[number] => 999-999-9999
[id] => 2
[order] => 0
[preferred] => 1
)
loop through the array to access the data you want,
// declare your array if you want to save data in array
$numbers = [];
foreach($data $key =>$value){
// echo just the number
echo $value['number'];
// echo the key and number
echo $key.' '.$value;
// or you can build an array
$numbers[$key] = $value['number'];
}
print_r($numbers);
Hope you find this useful, for more info about arrays take a look at this http://php.net/manual/en/language.types.array.php
foreach ($array as $id => $element)
{
// will echo 999-999-9999
echo $element['number'];
}
$array is the whole data structure. We go through as index, that is the $id, which is for example e2a4789d22ff47779722b8d8643894cd and the $element is the element in the $array[$id] so in the $array[e2a4789d22ff47779722b8d8643894cd]
So the $element is the small array in the large array, and it contains data like:
Array
(
[type] => workphone
[visibility] => public
[number] => 999-999-9999
[id] => 2
[order] => 0
[preferred] => 1
)
So if you need the number attribute, you type $element['number'] and you get it.
If your variable was in an array called $elements then it would look like:
$elements['e2a4789d22ff47779722b8d8643894cd']['number']
I have an object/array like this:
[LineItems] => Array
(
[0] => stdClass Object
(
[ProductNumber] => PAC-051-9716
[Description] => KIT CLOSURE 6" BUTT THRD BLK
[Cost] => 24.84
[ExtCost] => 24.84
[OrdNum] => X4146223
)
)
And the other object/array looks like this:
[0] => VendorBillItem Object
(
[vendorName] => PAC-051-9716
[quantity] => 1
[rate] => 24.84
[amount] => 24.84
)
How can I check if [ProductNumber] field value from the first array exist in the 2nd array by checking it against [vendorName] field value?
Thanks in advance. Cheers!
I recommend you build an index for 2nd array.
foreach ($vendorBills as $key => $vendorBill) {
empty($index[$vendorBill->vendorName]) && $index[$vendorBill->vendorName] = array();
$index[$vendorBill->vendorName][] = $key;
}
After that just check
!empty($index[$lineItem->ProductNumber])
Suppose two arrays are named as $lineitemsArray and $venderBillArray
foreach($lineitemsArray as $lineItem)
{
foreach($venderBillArray as $vendorItem)
{
if($lineItem->ProductNumber==$vendorItem->vendorName)
{
echo "equal";
}
else{
echo "not equal";
}
}
}
Im creating an array of products, each with an ID and score:
$savedProducts = array( 'product' => array("product_id" => $product_id,"product_score" => $score));
I want to be able to update the score by using the product_id as identifier.
I've tried:
foreach($savedProducts as $key => $val)
{
if ($val == $property_id )
{
$savedProducts[$key] = $score;
break;
}
}
Which keeps adding a new array item, rather than updating the original.
I think the issue is that my initial array setup then doesn't match the edited one.
Initial array:
Array
(
[product] => Array
(
[product_id] => 125026
[product_score] => 5
)
)
After trying to update score:
Array
(
[0] => Array
(
[product] => Array
(
[product_id] => 125026
[product_score] => 4
)
)
[1] => Array
(
[0] => Array
(
[product] => Array
(
[product_id] => 125026
[product_score] => 4
)
)
)
)
So it keeps addding elements, rather than updating the existing.
with PHP 5.5 use:
$savedProducts = array_column($savedProducts, NULL, 'product_id');
then you can access your product with:
$savedProducts[$product_id]
Please try this
foreach($savedProducts as $key => $val)
{
if($val['product_id'] == $property_id)
{
$savedProperties[$key]['product_score'] = $score;
break;
}
}
I have the below array coming though, ideally I am looking for a way of matching one value and printing out another value.
e.g.
if($randomvalue == $cards[Card][unit_id]) { echo $cards[SaleDetail][date_pid_signed]; }
I'm not sure exactly how to go about getting the above to work with the current array structure as below.
Any ideas how I can get around this?
Thanks
$cards = Array
(
[0] => Array
(
[Card] => Array
(
[id] => 210
[property_id] => 4
[unit_id] => 90
)
[SaleDetail] => Array
(
[property_agent] =>
[date_pid_signed] => 2012-06-15
[property_date_listed] =>
)
)
[1] => Array
(
[Card] => Array
(
[id] => 209
[property_id] => 4
[unit_id] => 103
)
[SaleDetail] => Array
(
[property_agent] =>
[date_pid_signed] => 2011-10-21
[property_date_listed] =>
)
)
)
foreach($cards as $card){
if($randomvalue == $card[Card][unit_id]) {
echo $card[SaleDetail][date_pid_signed];
}
}
Use $cards[0]['Card']['unit_id'] and $cards[0]['SaleDetail']['date_pid_signed']. Notice the indexes [0]. You can then use [1].
You might also want to check foreach or for loops!
if($randomvalue == $cards[0][Card][unit_id]) { echo $cards[0][SaleDetail][date_pid_signed]; }
also you can do
foreach($cards as $card)
{
if($randomvalue == $card[Card][unit_id])
{
echo $card[SaleDetail][date_pid_signed];
}
}