Let's say I have an array formatted like so:
$data = array(
'variables' => array(
'823h9fhs9df38h4f8h' => array(
'name' => 'Foo',
'value' => 'green'
),
'sdfj93248fhfhf88rh' => array(
'name' => 'Bar',
'value' => 'red'
)
)
);
Say I wanted to access the name & values of each array in the variables array. Surely you can access it just looping over the main variables array and not looping over each individual item array? Something like so?
foreach ($data as $k => $v) {
$name = $data['variables'][0]['name'];
}
I'm sure I'm missing something simple...
You can do
foreach ($data['variables'] as $k => $v) {
$name = $v['name'];
}
You can also try this
create a new array containing just the names..
$new_arr = array_column($data['variables'],'name' );
echo $new_arr[0].'<br/>';
echo $new_arr[1].'<br/>';
Related
I'm looking for a function to search for a matching keyword in a multi dimensional array and return the corresponding name and path values.
$search_array = array(
array(
'keywords' => array('apple', 'orange'),
'name' => 'Url One',
'path' => 'http://www.urlone.com'
),
array(
'keywords' => array('bananna'),
'name' => 'Url Two',
'path' => 'http://www.urltwo.com'
)
)
If there's a better way to structure this array to make it simpler the that would also be great, thanks
This can be accomplished using array filter:
$matches = array_filter($search_array, function($array){
return in_array('bananna', $array['keywords']);
});
Or using a foreach loop:
$matches = [];
foreach ($search_array as $key => $array) {
if (in_array('apple', $array['keywords'])) {
$matches[$key] = $search_array[$key];
}
}
I have a problem where I am getting data from database and need to put that in an array. The array is associative and I am not sure about this practice so I thought I should ask the community. Is this the correct way of adding data to array? The array is for the radio buttons that will be provided to the helper class in prestashop. The array structure is important. This is the var_dump array structure which I have in $options_break2.
$options_value = array();
$options = array();
for($z=0; $z<sizeof($options_break2); $z++)
{
$options_value = array_push($options_value,
array(
"id" => $options_break2[$z],
"name" => $options_break2[$z],
"label" => $options_break2[$z],
)
);
}
$options = array_push($options, $options_value);
What I want is that the array should contain something like:
$example = array(
array(
'id_option' => 'some value',
'name' => 'some value',
),
array(
'id_option' => 'some value',
'name' => 'some value',
),
);
Actually you don't need to use array_push and array() if your PHP version is above 5.6, and you can improve your loop by using the foreach loop:
$options_value = [];
foreach ($options_break2 as $opt) {
$options_value[] = [
"id_option" => $opt, // some_value
"name" => $opt // some_value
];
}
$options = $options_value; // you don't really need this
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;
I have a simple multidimensional array with two other arrays in it.
<?php
$data = array(
'first_array' => array(
'name' => 'Test1',
'description' => '...',
),
'second_array' => array(
'title' => 'Test2',
'description' => '...',
)
);
?>
And I have a simple foreach array like this:
function show($data, $id){
foreach ($data as $course) {
}
}
How can I display (and get) the name of the array in every iteration (I mean if it is 'first_array' or 'second_array', not the name fields in the arrays).
Use key=>val syntax
foreach ($data as $key=>$course) {
echo $key;
}
use this syntax for foreach :
foreach ($data as $name => $course) {
//do sth
}
I have an array with 2 same keys i want to foreach out if possible. The currently code looks like:
$arrayName = array(
1 => array('detail' => 'detail1' , 'detail' => 'detail2')
);
foreach ($arrayName[1] as $key['detail'] => $value) {
echo $value;
}
Thanks for any help!
Your keys are overwriting themselves. You may want to approach the solution like this:
$arrayName = array(
array('name' => 'detail' , 'value' => 'detail1'),
array('name' => 'detail' , 'value' => 'detail2')
);
foreach ($arrayName as $i) {
echo $i['value'];
}
You cannot have identical keys for an array.. The final key overwrites the first one. (in your case)
From the PHP Docs..
If multiple elements in the array declaration use the same key, only
the last one will be used as all others are overwritten.
A dynamic way of doing this..
<?php
$new_arr = array();
foreach(range(1,5) as $v)
{
$new_arr['detail'.$v]='detail'.$v;
}
print_r($new_arr);
OUTPUT :
Array
(
[detail1] => detail1
[detail2] => detail2
[detail3] => detail3
[detail4] => detail4
[detail5] => detail5
)