This question already has answers here:
PHP foreach with Nested Array?
(6 answers)
Closed 7 years ago.
I have a array like this. I want to loop over the array, but do not know how to handle the internal arrays. Can anyone help me?
$a = array(
0 => array(
'B' => array(
'company' => 'ZZZZZZ'
),
'User' => array(
'company' => 'ABC'
),
0 => array(
'jumlah' => null,
'jumbuy' => '98990',
'admin' => '2010'
)
)
);
If you want to use foreach on this array do like this.
foreach($a as $key=>$value)
{
print_r($value);
}
You can also use nested foreach.
You can use a recursive function (but these can spiral out of control).
function print_array($array) {
foreach($array as $key => $value) {
echo "{$key} is: ";
if (is_array($value)) {
echo "an array.\n"
print_array($value);
} else {
echo "{$value}.\n";
}
echo "\n";
}
Related
This question already has answers here:
How to modify an array's values by a foreach loop?
(2 answers)
Closed 4 months ago.
I have a multidimensional associative array and im using two foreach to iterate them, i need to change a value from the original array, here a representation of my code and what ive tried:
$array = [
['id' => 1, 'customers' => [['customerName' => 'Daniel', 'age' => 20, 'isYoung' => false], ['customerName' => 'Patrick', 'age' => 56, 'isYoung' => false]]],
['id' => 4, 'customers' => [['customerName' => 'Paul', 'age' => 41, 'isYoung' => false]]]
];
foreach($array as $key => $value) {
foreach($value['customers'] as $sKey => $sValue {
if($sValue['age'] < 35) {
$array[$key]['customers'][$sKey]['isYoung'] = true; //Doesnt Work
$value['customers'][$sKey]['isYoung'] = true; //Doesnt Work
}
}
}
Any leads?
The first assignment works for me. But you can simplify it by using reference variables for the iteration variables.
foreach($array as &$value) {
foreach($value['customers'] as &$sValue) {
if($sValue['age'] < 35) {
$sValue['isYoung'] = true;
}
}
}
This question already has answers here:
How to convert array values to lowercase in PHP?
(10 answers)
Closed 5 years ago.
I have this code that is looking for $array2 in $array1.
The issue that I have is that I need to lowercase both arrays so the in_array matching works and this code operates as expected but $array1 is greater than 20k objects -- is there anyway to do the lowercase without losing the array structure and looping?
$array1 = array(code => 200, status => success,
array(
'email' => 'Example1223#sample.com',
'status' => 'Pending'
),
array(
'email' => 'example123#sample.com',
'status' => 'Approved: Printed & Cleared'
),
array(
'email' => 'example23#sample.com',
'status' => 'Approved'
),
array(
'email' => 'Example22#sample.com',
'status' => 'Approved: Printed & Cleared'
),
);
$yourArray = array();
$array = array();
foreach ($array1 as &$array){
$yourArray[] = array_map('strtolower', $array);
}
echo "<pre>"; print_r($yourArray);
$array2 = array(
'email' => 'example1223#sample.com',
'status' => 'Pending'
);
$yourArray2 = array_map('strtolower', $array2);
if(in_array($yourArray2 , $yourArray)) {
echo "match";
} else {
echo "no match";
}
echo "<pre>"; print_r($yourArray2);
You can always use preg_grep() function:
preg_grep("/ONe/i", $yourArray2);
I have a PHP array, something like below:
Array(
[0] => Array
(
[name] => month_year
[value] => 201609
)
[1] => Array
(
[name] => advance_amount
[value] => 50%
)
)
Using this array, I want to create 2 variables like this:
$month_year = '201609';
$advance_amount = '50%';
Can anybody tell me is this possible in php?
I tried it using two foreach but I don't have any idea how to precced.
foreach ($_POST as $k => $data) {
//echo "<pre>", print_r($data)."</pre>";
foreach ($data as $key => $value) {
echo $key."<br>";
}
}
Use variable variables. Your foreach loop should be like this:
foreach ($_POST as $k => $data) {
$$data['name'] = $data['value'];
}
// display variables
echo $month_year . "<br />";
echo $advance_amount;
PHP7 style:
$a = [['name' = 'month_year', 'value' => '201609'], ['name' => 'advance_amount', 'value' => '50%']];
foreach ($a as $line) {
${$line['name']} = $line['value'];
}
php > echo $month_year; //201609
Have a look at the Variables reference
Caution
Further dereferencing a variable property that is an array has
different semantics between PHP 5 and PHP 7. The PHP 7.0 migration
guide includes further details on the types of expressions that have
changed, and how to place curly braces to avoid ambiguity.
using list it gives you the cleaner way to solve the issue: for example
$data = [
[
'name' => 'month_year',
'value' => 201609
],
[
'name' => 'advance_amount',
'value' => '50%'
]
];
list($month_year, $advance_amount) = array_map(function($value){
return $value['value'];
}, $data);
echo sprintf('Month of the year is %s with porcentage %s', $month_year, $advance_amount);
This will have the result you are looking for with a clearer code.
Month of the year is 201609 with porcentage 50%
first create loop and declare var.
$data = array(array(
'name' => 'month_year',
'value' => 201609,
), array(
'name' => 'advance_amount',
'value' => '50%',
)
);
foreach ($data as $key => $value) {
${$value['name']} = $value['value'];
}
echo $month_year;
echo '<br>';
echo $advance_amount;
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 3 months ago.
I have this output:
I don't have any idea how can I make my array look like this:
$array[
0 => [
'item_id' => 6,
'price' => "2311.00",
'qty' => 12,
'discount' => 0
],
1 => [
'item_id' => 7,
'price' => "1231.00",
'qty' => 1,
'discount' => 12
],
2 => [
'item_id' => 8,
'price' => "123896.00",
'qty' => 0,
'discount' => 24
]
]
I have started the loop but I don't really know how to get that kind of structure.
foreach( $array as $wishlist ){
foreach( $wishlist as $k => $v ){
}
}
You can iterate the outer and inner arrays to build your data like so, this allows you to add further keys to the array later - but does depend on your inner array keys being contiguous
$wishlist = [];
foreach ($array as $outerKey => $outerValue) {
foreach ($outerValue as $innerKey => $innerValue) {
$wishlist[$innerKey][$outerKey] = $innerValue;
}
}
Your loop should look like this:
foreach( $array as $item => $wishlist ){
foreach( $wishlist as $k => $v ){
$new_array[$k][$item] = $v;
}
}
You should have to use for loop.
for($i=0;$i<count(youarray['item_id']);$i++) {
$wishlist[$i]['item_id'] = youarray['item_id'][$i];
$wishlist[$i]['price'] = youarray['price'][$i];
$wishlist[$i]['qty'] = youarray['qty'][$i];
$wishlist[$i]['discount'] = youarray['discount'][$i];
}
or user foreach like this
foreach(youarray['item_id'] as $key=>$val) {
$wishlist[$key]['item_id'] = $val;
$wishlist[$key]['price'] = youarray['price'][$key];
$wishlist[$key]['qty'] = youarray['qty'][$key];
$wishlist[$key]['discount'] = youarray['discount'][$key];
}
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
PHP: get keys of independent arrays
Hello.
I have a multi-dimensional array. I want a function that finds the position of the given array key (all my array keys are strings) and then returns the position of the key as an array.
E.g:
$arr = array
(
'fruit' => array(
'apples' => array(),
'oranges' => array(),
'bananas' => array()
),
'vegetables' => array(
'tomatoes' => array(),
'carrots' => array(),
'celery' => array(),
'beets' => array
(
'bears' => array(),
'battlestar-galactica' => array()
),
),
'meat' => array(),
'other' => array()
);
Now if I call the function like this:
theFunction('bears');
It should return:
array(1, 3, 0);
function array_tree_search_key($a, $subkey) {
foreach (array_keys($a) as $i=>$k) {
if ($k == $subkey) {
return array($i);
}
elseif ($pos = array_tree_search_key($a[$k], $subkey)) {
return array_merge(array($i), $pos);
}
}
}