Loop Through Specific Associative Array In PHP - php

I am trying to loop through only a specific sub array in PHP with foreach. Example array:
$testData = array(
$test1=array(
'testname'=>'Test This',
'testaction'=>'create user',
$testData = array(
'item'=>'value',
'foo'=>'bar',
'xyz'=>'value'
),
$anotherArray = array()
),
$test2=array(
'testname'=>'Test That',
'testaction'=>'get user',
$testData = array(
'item'=>'value',
'foo'=>'bar',
'xyz'=>'value'
),
$anotherArray = array()
)
);
And now I am going to go through each test and set some logic based on the name and action, but then need to do several tests on the data. Not sure how to only get $test1's $testData and not $test1's $anotherArray data. I have the following but it doesn't work:
foreach($testData as $test => $section){
foreach($section['testData'] as $field => $value){
\\code
}
}
Any help is appreciated! Thanks!

Try this instead:
$testData = array(
'test1'=>array(
'testname'=>'Test This',
'testaction'=>'create user',
'testData' => array(
'item'=>'value',
'foo'=>'bar',
'xyz'=>'value'
),
'anotherArray' => array()
),
'test2'=>array(
'testname'=>'Test That',
'testaction'=>'get user',
'testData' => array(
'item'=>'value',
'foo'=>'bar',
'xyz'=>'value'
),
'anotherArray' => array()
)
);

Related

Search Arrays to Multidimensional Array

I have 2 dimensional array like
$events = array(
array(
'desc' => 'Cancer Webinar',
'date' => '20201219'
),
array(
'desc' => 'CSR Management',
'date' => '20200812'
),
array(
'desc' => 'Company Anniversary',
'date' => '20200309'
)
);
$look = array('20201219','20200309');
result: array('Cancer Webinar','Company Anniversary');
The function call search from array list ($look) then find it to $events.
From the code above shoud return:
array('Cancer Webinar','Company Anniversary');
If the second array will just search by date use the following code
function search_from_array($array, $events) {
$result = [];
foreach($events as $event) {
if(in_array($event['date'], $array)) {
array_push($result, $event['desc']);
}
}
echo json_encode($result);
}
search_from_array($look, $events);
you can develop it more to can search with any key in the multidimensional array.
in case you want to search but many keys just add OR in the condition and make the same code for other keys like this
if(in_array($event['date'], $array) || in_array($event['desc'], $array))
You can make $look a dict, then check if the date key exist.
$events = array(
array(
'desc' => 'Cancer Webinar',
'date' => '20201219'
),
array(
'desc' => 'CSR Management',
'date' => '20200812'
),
array(
'desc' => 'Company Anniversary',
'date' => '20200309'
)
);
$look = array('20201219','20200309');
$look_dic = array_flip($look);
foreach($events as $event){
if(isset($look_dic[$event["date"]])){
$result[] = $event["desc"];
}
}
var_dump($result);

PHP: not looping inside a foreach

I have this array:
$flavours = array (
0 => array(799390 => 'Banana',),
1 => array(799391 => 'Chocolate',)
);
And now I inside a foreach looping a set from my database. I get 'Banana' from the database and I need the array to give me 799390.
I have tried:
‌‌array_search('Banana', $flavours);
but it does not work unless I add:
‌‌array_search('Banana', $flavours[0]);
But I cannot add [0] as I won't be able to tell in which position 'Banana' flavour is in the array.
Any solution apart from looping again inside a foreach??
Thanks a ton
First, we can use array_walk_recursive() to flatten your array, which just gets rid of your nested arrays.
Next, we use array_flip() to swap the keys/values in the flattened array. This makes it easier to grab the ID for a specific term.
<?php
$flavours = [
[799390 => 'Banana'],
[799391 => 'Chocolate']
];
//flatten array
//Produces: Array ( [799390] => Banana [799391] => Chocolate )
array_walk_recursive($flavours,
function($v, $k) use (&$temp) {
$temp[$k] = $v;
}
);
//flip array. Swaps keys with values.
//Produces: Array ( [Banana] => 799390 [Chocolate] => 799391 )
$flavours = array_flip($temp);
Now you can get the ID pretty easily, such as $flavours['Banana'].
If you have a very very large array, this method may become slow. However, I tested this with 100,000 values on my cheap webhost, and ran this method a few times (20-25 times). It is finishing consistently at around (usually under) 0.1 milliseconds, which is about 0.0014 seconds.
You can insert an if statement to set a condition for your loop as
foreach ($flavours as $key => $value) {
if($key = array_search('Banana', $value)){
echo $key;
}
}
Output
799390
If the word being searched for is a different case many of the usual array methods will not work when trying to find the match, using preg_grep however will allow matches to be found in a case-insensitive manner.
function findflavour( $search, $haystack ){
foreach( $haystack as $index => $arr ){
$res=preg_grep( sprintf( '#%s#i', $search ), $arr );
if( !empty( $res ) ) return array_search( array_values( $res )[0], $arr );
}
return false;
}
$search='BaNanA';
$flavours=array(
array( 799390 => 'Banana' ),
array( 799391 => 'Chocolate' ),
array( 729361 => 'Chilli' ),
array( 879695 => 'Apple' ),
array( 995323 => 'Avacado' ),
array( 528362 => 'Orange' ),
array( 723371 => 'Cherry' ),
);
printf( 'Key:%s', findflavour( $search, $flavours ) );
If there might be multiple elements in the source array with the same value but different IDs a slightly different version of the findflavour function
function findflavour( $search, $haystack, $multiple=false ){
$keys=[];
foreach( $haystack as $index => $arr ){
$res=preg_grep( sprintf( '#%s#i', $search ), $arr );
if( !empty( $res ) ) {
$key=array_search( array_values( $res )[0], $arr );
if( $multiple )$keys[]=$key;
else return $key;
}
}
return $multiple ? $keys : false;
}
$multiple=true;
$search='AVacAdo';
$flavours=array(
array( 799390 => 'Banana' ),
array( 799391 => 'Chocolate' ),
array( 291333 => 'Avacado' ),
array( 729361 => 'Chilli' ),
array( 879695 => 'Apple' ),
array( 995323 => 'Avacado' ),
array( 528362 => 'Orange' ),
array( 723371 => 'Cherry' ),
);
printf( 'Key(s): %s', print_r( findflavour( $search, $flavours, $multiple ), true ) );

Fill array with dynamic content in PHP

I need to fill an array with a dynamic list of products.
To do so, I'm using the following code:
$list_array = array(
$products[] = array(
'SKU' => '0001',
'Title' => 'Bread',
'Quantity' => '',
),
$products[] = array(
'SKU' => '0002',
'Title' => 'Butter',
'Quantity' => '',
)
);
return $list_array;
It works fine if I know every product in the array.
But in my use case I have no idea which products are in the array.
So I want to fill the array with dynamic data.
I came up with something this:
$products = get_posts( 'numberposts=-1&post_status=publish&post_type=product' );
foreach ( $products as $product ) {
$products[] = array(
'SKU' => $product->id,
'Title' => $product->post_title,
'Quantity' => '',
),
}
return $products;
I know there is something really wrong with the array. But I couldn't figure out what it is.
The code you submitted cannot work. The short syntax $a[] = ... is to append data to the $a array, for example:
$a = [];
$a[] = 1;
$a[] = 2;
// $a = [1, 2]
You can also do it in a more efficient way with a map function:
function reduce($product)
{
return array(
'SKU' => $product->id,
'Title' => $product->post_title,
'Quantity' => '',
);
}
return array_map('reduce', $products);
It will execute the function reduce and replace value for each element of you array. Complete doc here: https://www.php.net/manual/en/function.array-map.php
Your problem is that you are overwriting the $products array that you are looping over inside the loop. Change the name of the variable in the loop to fix that:
$list_array = array();
foreach ( $products as $product ) {
$list_array[] = array(
'SKU' => $product->id,
'Title' => $product->post_title,
'Quantity' => ''
);
}
return $list_array;

Highcharts php data array

I am struggling to get values added into the array at specific points.
What I am looking for is to create and empty array, take the resultset from the database and depending on whether they are global or local create the PHP array listed below. The problem is that no matter what I try I can't get the array to look like below.
Either values are being replaced, or arrays being replaced or values being added into wrong places
This is my empty array and I am trying to loop each resultset into the array and then how many sold in the day.
$test = array();
foreach($globalDatabase as $value)
{
$test['global'][] = array('name'=>$value->type, 'data' => array($value->day,$value->quantity) );
}
foreach($localDatabase as $value)
{
$test['local'][] = array('name'=>$value->type, 'data' => array($value->day,$value->quantity) );
}
This is what I need it to look like so that it ends up looking like this, which is the output format I need it to be in so it is compatible with highcharts
$test = array(
'global'=>array(
array(
'name'=>'CARS',
'data' => array(
array('mon',1),
array('tue',1)
)
),
array(
'name'=>'BIKES',
'data' => array(
array('mon',1),
array('tue',1)
)
),
array(
'name'=>'BOATS',
'data' => array(
array('mon',1),
array('tue',1)
)
),
),
'local'=>array(
array(
'name'=>'CARS',
'data' => array(
array('mon',1),
array('tue',1)
)
),
array(
'name'=>'BIKES',
'data' => array(
array('mon',1),
array('tue',1)
)
),
)
);
I take it that your data rows pulled from the table are 'flat', meaning each row has only a single value each for 'type', 'day', and 'quantity', with no nesting.
Going on that assumption, you may just need to group your data by common attributes similar to the following:
$databases = array('global'=>$globalDatabase, 'local'=>$localDatabase);
$workData = array('global'=>array(), 'local'=>array());
foreach($databases as $dbType=>$workDb)
{
foreach ($workDb as $value) {
if(!array_key_exists($value->type, $workData[$dbType])) {
$workData[$dbType][$value->type] = array();
}
$workData[$dbType][$value->type][] = array($value->day, $value->quantity);
}
}
$test = array();
foreach ($workData as $type=>$workRow) {
foreach ($workRow as $dataKey=>$dataRow) {
$test[$type][] = array('name'=>$dataKey, 'data' => $dataRow );
}
}

PHP input filtering for complex arrays

Official PHP documentation states that filter_var_array() supports array filtering in the following format:
$data = array(
'testarray' => array('2', '23', '10', '12')
);
$args = array(
'testarray' => array('filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_FORCE_ARRAY
)
);
$myinputs = filter_var_array($data, $args);
However, if the array in question is multi-dimensional and requires different filters for different parts, how would you approach defining filtering options?
As an example:
$data = array(
'testhash' => array('level1'=>'email',
'level2'=> array('23', '10', '12'))
);
Idea 1
Consider using FILTER_CALLBACK. In this way, you can write a callback function that itself uses the filter extension, thus providing a recursive ability.
function validate_array($args) {
return function ($data) use ($args) {
return filter_input_array($data, $args);
};
}
This will generate the callback functions.
$args = array(
'user' => array(
'filter' => FILTER_CALLBACK,
'options' => validate_array(array(
'age' => array('filter' => FILTER_INPUT_INT),
'email' => array('filter' => FILTER_INPUT_EMAIL)
))
)
);
This is what the config array would then look like.
Idea 2
Do not hesitate to pat me on the back for this one because I am quite proud of it.
Take an arg array that looks like this. Slashes indicate depth.
$args = array(
'user/age' => array('filter' => FILTER_INPUT_INT),
'user/email' => array('filter' => FILTER_INPUT_EMAIL),
'user/parent/age' => array('filter' => FILTER_INPUT_INT),
'foo' => array('filter' => FILTER_INPUT_INT)
);
Assume your data looks something like this.
$data = array(
'user' => array(
'age' => 15,
'email' => 'foo#gmail.com',
'parent' => array(
'age' => 38
)
),
'foo' => 5
);
Then, you can generate an array of references that map keys such as 'user/age' to $data['user']['age']. In final production, you get something like this:
function my_filter_array($data, $args) {
$ref_map = array();
foreach ($args as $key => $a) {
$parts = explode('/', $key);
$ref =& $data;
foreach ($parts as $p) $ref =& $ref[$p];
$ref_map[$key] =& $ref;
}
return filter_var_array($ref_map, $args);
}
var_dump(my_filter_array($data, $args));
Now the only question is how you deal with the mismatch between the validation record and the original data set. This I cannot answer without knowing how you need to use them.

Categories