I have an array like this and the code using foreach loop.
$arr = array( array ( array( 'CAR_TIR', 'Tires', 100 ),
array( 'CAR_OIL', 'Oil', 10 ),
array( 'CAR_SPK', 'Spark Plugs', 4 )
),
array ( array( 'VAN_TIR', 'Tires', 120 ),
array( 'VAN_OIL', 'Oil', 12 ),
array( 'VAN_SPK', 'Spark Plugs', 5 )
),
array ( array( 'TRK_TIR', 'Tires', 150 ),
array( 'TRK_OIL', 'Oil', 15 ),
array( 'TRK_SPK', 'Spark Plugs', 6 )
)
);
function recarray($array)
{
foreach($array as $key=>$value)
{
if(is_array($value))
{
RecArray($value);
}
else
{
echo "key = $key value = $value";
}
}
}
recarray($arr);
I have to traverse the array using recursion and without using foreach.
Simple depth first search:
function DFS($array) {
for ($i = 0; $i < count($array); $i ++) {
if (is_array($array[$i])) {
DFS($array[$i]);
}
else {
echo "key: ".$i.", value: ".$array[$i]."<br />";
}
}
}
What about array_walk_recursive()? it can apply function to each element of the array:
function test_print($value, $key)
{
echo "key = $key value = $value";
}
array_walk_recursive($arr, 'test_print');
not tested
I would use a while loop - like
while($i < count($array)) {}
Related
I have this array:
$actualPlan = 'medium';
$plans = array(
array(
'plans' => array(
'tiny' => 29,
'small' => 69,
'medium' => 179,
'big' => 359
)
)
);
During a foreach, I display the contents of plans of this array like this:
foreach($plans as $key => $data) {
foreach($data['plans'] as $plan => $rate) {
...
}
}
But how can I know the position of the $actualPlan ?
For example, for :
if $actualPlan == medium it should return me 3.
if $actualPlan == tiny it should return me 1.
Thanks.
inside the loop:
echo array_search($actualPlan, array_keys($rate)); // returns the index position as int
here will output 1,2,3,4 for the inputs 'tiny','small','medium','big'
Within the foreach($plans as $key => $data) {, you could make the conditional like this :
$current_plan = explode('___', $actualPlan);
$current_key = array_search($current_plan[0],array_keys($data['plans']));
foreach($data['plans'] as $plan => $rate) {
$current_iterated_key = array_search($plan,array_keys($data['plans']));
if ($current_iterated_key < $current_key) {
echo "$plan => Downgrade\r\n";
} elseif ($current_iterated_key > $current_key) {
echo "$plan => Upgrade\r\n";
} elseif($current_iterated_key == $current_key) {
echo "$plan => Current\r\n";
}
}
I have two arrays:
Array 1:
$art_style = ['Title1','Title2','Title3'];
Array 2:
array(
'name' => array('Title1', 'Title3', 'Title2'),
'value' => array('2,0x1,0', '2,5', '15,0'
);
I need to compare Array 2 "name" with Array 1 and output the values from Array 2 in the order of Array 1.
So the output in this case would be:
2,0x1,0 - 15,0 - 2,5
Any Idea how I could achieve that?
Try something like this:
// Array1 order
foreach ($art_style as $key => $value) {
if(in_array($value,$array2['name']))
echo $array2['value'][$key];
}
// Array2 order
foreach ($array2['name'] as $key => $value) {
if(in_array($value,$art_style))
echo $array2['value'][$key];
}
Little Long Method. But, It Worked.
<?
$array1 = ['Title1','Title2','Title3'];
$array2=array(
'name' => array('Title1', 'Title3', 'Title2'),
'value' => array('2,0x1,0', '2,5', '15,0')
);
$SizeofArray2=sizeof($array2['name']);
for($i=0;$i<$SizeofArray2;$i++)
{
$Array2Value= $array2['name'][$i];
for($j=0;$j<sizeof($array1);$j++)
{
if($Array2Value==$array1[$j])
{
if($j==$i)
{
echo " ".$array2['value'][$i];
}
if($j!=$i)
{
echo " -".$array2['value'][$i];
}
}
}
}
?>
Output: 2,0x1,0 -2,5 -15,0
try this:
$art_style = array('Title1','Title2','Title3');
$array2 = array(
'name' => array('Title1', 'Title3', 'Title2'),
'value' => array('2,0x1,0', '2,5', '15,0')
);
foreach ($art_style as $style) {
foreach ($array2['name'] as $id => $name) {
if ($name == $style) {
echo $array2['value'][$id].' - ';
break;
}
}
}
I was wondering when working with multimedional arrays, if a certain key is the same, is there a way to combine the contents of other keys into its own array if a certain key is the same?
Something like this:
// name is the same in both arrays
array(
array(
'name' => 'Pepsi',
'store' => 'Over here',
'number' => '1234567'
),
array(
'name' => 'Pepsi',
'store' => 'Over here',
'number' => '5556734'
)
)
into something like this
array(
array(
'name' => 'Pepsi',
'store' => array('Over here', 'Over here'),
'number' => array('1234567', '5556734')
)
)
The defining key is checking if the name element is the same for the other arrays.
You can try a function like this.
function mergeByKey($array,$key){
$tmp_array = array();
foreach ( $array as $k => $row ) {
$merged = false;
foreach ($tmp_array as $k2 => $tmp_row){
if ($row[$key] == $tmp_row[$key]){
foreach ( $row as $k3 => $value ) {
if ($k3 == $key) continue;
$tmp_array[$k2][$k3][] = $value;
$merged = true;
}
}
if ($merged) break;
}
if (!$merged) {
$new_row = array();
foreach ( $row as $k4 => $value ) {
if ($k4 == $key) $new_row[$k4] = $value;
else $new_row[$k4] = array($value);
}
$tmp_array[] = $new_row;
}
}
foreach ( $tmp_array as $t => $row ) {
foreach ( $row as $t2 => $value ) {
if ( count($value) == 1 && $t2 != $key ) $tmp_array[$t][$t2] = $value[0];
}
}
return $tmp_array;
}
passing the array as first parameter and the key as second one.
I'm referencing to your array structure
edited: missed a piece
edited2: if resultin array contains elements with one string, it returns a string and not a array with one element
demo
This function uses a given field name as the grouping identifier and turns all other fields into arrays.
Note that single occurrences of your field name will yield arrays with a single element for the other fields. I wasn't sure whether that's a desirable trait, but just making sure you know ;-)
$arr = array(
array(
'name' => 'Pepsi',
'store' => 'Over here',
'number' => '1234567'
),
array(
'name' => 'Pepsi',
'store' => 'Over here',
'number' => '5556734'
)
);
function mergeArray($array, $column)
{
$res = array();
foreach ($array as $item) {
foreach ($item as $key => $value) {
if ($key === $column) {
$res[$column][$key] = $value;
} else {
$res[$column][$key][] = $value;
}
}
}
return array_values($res);
}
print_r(mergeArray($arr, 'name'));
Demo
Thanks to Gianni Lovece for her answer but I was able to develop a much simpler solution based on this problem. Just plug in the $result_arr to browse through and the $key you want to use as basis and it immediately outputs a multidimensional array with non-repeating values for repeating elements (see example below).
function multiarray_merge($result_arr, $key){
foreach($result_arr as $val){
$item = $val[$key];
foreach($val as $k=>$v){
$arr[$item][$k][] = $v;
}
}
// Combine unique entries into a single array
// and non-unique entries into a single element
foreach($arr as $key=>$val){
foreach($val as $k=>$v){
$field = array_unique($v);
if(count($field) == 1){
$field = array_values($field);
$field = $field[0];
$arr[$key][$k] = $field;
} else {
$arr[$key][$k] = $field;
}
}
}
return $arr;
}
For example, in the sample array for this question, running multiarray_merge($mysample, 'name') returns
array(
'Pepsi' => array(
'name' => 'Pepsi',
'store' => 'Over here', // String: Not an array since values are not unique
'number' => array('1234567', '5556734') // Array: Saved as array since values are unique
)
);
I have an array that includes empty values.
Is there any way to split array into chunks using empty value as a mask?
Thank you,
Without seeing your array structure I think a simple foreach could do the job:
$a = array(
1 => 'test',
2 => 'test',
3 => '',
4 => 'test',
5 => 'test',
6 => '',
7 => 'test'
);
$new_array = array();
$i = 0;
foreach ($a as $k => $v)
{
if ( ! empty($v))
{
$new_array[$i][] = $v;
continue;
}
$i++;
}
print_r($new_array);
Try this,
If $values is the array, then
$i = 0;
foreach($values as $value)
{
if($value != "")
{
$new[$i][] = $value;
}
else
{
$i++;
}
}
Hope, this wil help.
Right now i got an array which has some sort of information and i need to create a table from it. e.g.
Student{
[Address]{
[StreetAddress] =>"Some Street"
[StreetName] => "Some Name"
}
[Marks1] => 100
[Marks2] => 50
}
Now I want to create database table like which contain the fields name as :
Student_Address_StreetAddress
Student_Address_StreetName
Student_Marks1
Student_Marks2
It should be recursive so from any depth of array it can create the string in my format.
You can use the RecursiveArrayIterator and the RecursiveIteratorIterator (to iterate over the array recursively) from the Standard PHP Library (SPL) to make this job relatively painless.
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
$keys = array();
foreach ($iterator as $key => $value) {
// Build long key name based on parent keys
for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {
$key = $iterator->getSubIterator($i)->key() . '_' . $key;
}
$keys[] = $key;
}
var_export($keys);
The above example outputs something like:
array (
0 => 'Student_Address_StreetAddress',
1 => 'Student_Address_StreetName',
2 => 'Student_Marks1',
3 => 'Student_Marks2',
)
(Working on it, here is the array to save the trouble):
$arr = array
(
'Student' => array
(
'Address' => array
(
'StreetAddress' => 'Some Street',
'StreetName' => 'Some Name',
),
'Marks1' => '100',
'Marks2' => '50',
),
);
Here it is, using a modified version of #polygenelubricants code:
function dfs($array, $parent = null)
{
static $result = array();
if (is_array($array) * count($array) > 0)
{
foreach ($array as $key => $value)
{
dfs($value, $parent . '_' . $key);
}
}
else
{
$result[] = ltrim($parent, '_');
}
return $result;
}
echo '<pre>';
print_r(dfs($arr));
echo '</pre>';
Outputs:
Array
(
[0] => Student_Address_StreetAddress
[1] => Student_Address_StreetName
[2] => Student_Marks1
[3] => Student_Marks2
)
Something like this maybe?
$schema = array(
'Student' => array(
'Address' => array(
'StreetAddresss' => "Some Street",
'StreetName' => "Some Name",
),
'Marks1' => 100,
'Marks2' => 50,
),
);
$result = array();
function walk($value, $key, $memo = "") {
global $result;
if(is_array($value)) {
$memo .= $key . '_';
array_walk($value, 'walk', $memo);
} else {
$result[] = $memo . $key;
}
}
array_walk($schema, 'walk');
var_dump($result);
I know globals are bad, but can't think of anything better now.
Something like this works:
<?php
$arr = array (
'Student' => array (
'Address' => array (
'StreetAddress' => 'Some Street',
'StreetName' => 'Some Name',
),
'Marks1' => array(),
'Marks2' => '50',
),
);
$result = array();
function dfs($data, $prefix = "") {
global $result;
if (is_array($data) && !empty($data)) {
foreach ($data as $key => $value) {
dfs($value, "{$prefix}_{$key}");
}
} else {
$result[substr($prefix, 1)] = $data;
}
}
dfs($arr);
var_dump($result);
?>
This prints:
array(4) {
["Student_Address_StreetAddress"] => string(11) "Some Street"
["Student_Address_StreetName"] => string(9) "Some Name"
["Student_Marks1"] => array(0) {}
["Student_Marks2"] => string(2) "50"
}
function getValues($dataArray,$strKey="")
{
global $arrFinalValues;
if(is_array($dataArray))
{
$currentKey = $strKey;
foreach($dataArray as $key => $val)
{
if(is_array($val) && !empty($val))
{
getValues($val,$currentKey.$key."_");
}
else if(!empty($val))
{
if(!empty($strKey))
$strTmpKey = $strKey.$key;
else
$strTmpKey = $key;
$arrFinalValues[$strTmpKey]=$val;
}
}
}
}