Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a function that searches through an array of vegetable varieties to see if it matches an ID:
// my function
function findVariety($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, findVariety($subarray, $key, $value));
}
return $results;
}
// function call
$picks = findVariety($veg,id,$sf->spring_choice);
when successful, it returns something like this:
// returned from print_r($picks);
Array ( [0] => Array ( [id] => 2 [variety] => Royal Burgundy (bush) ) )
All I'm missing is how to add the variety to an echo that I'm sending to my page, Ex:
echo '<td height="90px">'.$picks['variety'] .'<br />add plants</td>';
As of now, I have been stuck on this last step! Any help would be amazing...
Your returned array is nested so to access the variety you'd need to do this:
echo $picks[0]['variety']
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I wanted to output a 3 dimensional array that will come from the Database. This is my database looks like:
Basically, I wanted my first array will be the header_name and under the header_name the sub_header_name will come then underneath is the name
eg:
User Role Management => array(
'' => array (
'Create User Role'
)
),
Config Management => array(
'Organisation' => array('Create Country','Create
Organisation'),
'Site' => array('Create Site','Edit Site')
)
and here are my codes:
$getAllPermission = Permission::get();
$arrHeader = array();
$arrSubHeader = array();
$arrPermissions = array();
// $x = 0;
foreach($getAllPermission as $value){
$title = $value->header_name;
$sub_header_name = $value->sub_header_name;
$permission_name = $value->name;
if ($sub_header_name == ""){
$sub_header_name = 0;
}
array_push($arrPermissions,$permission_name);
$arrHeader[$title] = array($sub_header_name => array($arrPermissions));
//$x++;
}
and my output was like this:
You're pushing onto the same $arrPermissions array every time through the loop, that's why each of the roles gets a longer and longer copy of the permissions array.
You're overwriting $arrHeader[$title] each time through the loop instead of adding a new key to it.
Your desired output has a key of '' for the empty sub_header_name, so I don't see why you have the if that sets $sub_header_name = 0;.
It should simply be:
foreach ($getAllPermissions as $value) {
$arrHeader[$value->header_name][$value->sub_header_name][] = $value->name;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
If I have an array like this:
$cars = array (
array("name"=>"jeep","Year"=>"2012"),
array("name"=>"ferrari","Year"=>"2017"),
array("name"=>"jaguar","Year"=>"2013")
);
How to print a $cars['name'] where $cars[Year] = 2013, is that possible in the array as we can do in MySQL? As we know with MySQL we can do:
select * from table where //condition
So, how this can be done in arrays?
You could loop through each element in the array and using an 'if' statement echo the name of the car if the year is 2013
$cars = array (
array("name"=>"jeep","Year"=>"2012"),
array("name"=>"ferrari","Year"=>"2017"),
array("name"=>"jaguar","Year"=>"2013")
);
foreach ($cars as $value) {
if($value[Year] == 2013){
echo $value[name] ."<br>";
}
}
And also solution with array_filter, because you will probably have multiple cars with same year.
$cars = array (
array("name"=>"jeep","Year"=>"2012"),
array("name"=>"ferrari","Year"=>"2017"),
array("name"=>"jaguar","Year"=>"2013")
);
$filtered_cars = array_filter($cars, function ($item) {
return $item['Year'] === '2013';
});
print_r(current($filtered_cars)['name']);
An example with the isFromYear function accepting the year as a parameter:
<?php
$cars = array (
array("name"=>"jeep","Year"=>"2012"),
array("name"=>"ferrari","Year"=>"2017"),
array("name"=>"jaguar","Year"=>"2013")
);
class YearFilter {
private $year;
function __construct($year) {
$this->year = $year;
}
function isFromYear($i) {
return $i["Year"] == $this->year;
}
}
$matches = array_filter($cars, array(new YearFilter("2013"), 'isFromYear'));
print_r($matches);
?>
You can use array_filter() and pass an conditional function as the second argument along with your array. As an example in your case:
function filterArray($value){
if($value['Year'] == "2013")
return $value['name'];
}
$filteredArray = array_filter($fullArray, 'filterArray');
So if we passed an array that looks like:
$fullArray = array (
array("name"=>"John","Year"=>"2012"),
array("name"=>"Doe","Year"=>"2017"),
array("name"=>"Martin","Year"=>"2013")
);
Output would be:
Array
(
[2] => Array
(
[name] => Martin
[Year] => 2013
)
)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How i can combine ad array of values into an array with double combination string without duplications ?
For example, if i have something like:
array('one','two','tre','four','five');
I want to obtain an array of combinations like this ('one / two') just one time and not also ('two / one').
In this way i wanto to get something similar to:
array('one/two', 'one/tre', 'one/four', 'one/five', 'two/tree','two/four' .......
Suggestions ?
You can do it with this code. It won't show two/one, three/two, etc (that's how I understood it):
<?php
$array = array('one','two','tre','four','five');
$newArray = [];
foreach ($array as $el) {
foreach ($array as $el2) {
if ($el === $el2) continue;
$newArray[] = $el."/".$el2;
}
array_shift($array); // remove the element we just went through
}
print_r($newArray);
Demo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a complicated JSON and I need create array from this JSON.
I already parsed JSON and create a variablies like this:
$name = $json[response][docs][$i][name][0];
$osm_id = $json[response][docs][$i][osm_id][0];
$place = $json[response][docs][$i][place][0];
$population= $json[response][docs][$i][population][0];
now I need a array, with this variablies, where the $i is changing, like this:
$array = [array_1(name,osm_id,place,population),array_2(name_2,osm_id_2)]
Can you help me with the cycle to fill this array?
If my understanding is correct,
$expected_arr = array();
foreach($json[response][docs] as $inc => $values){
$data = array();
foreach($values as $key => $val){
$data[$key] = $val[0];
}
$expected_arr[$inc] = $data;
}
So you would get something like
array(0 => array( 'name'=>'xxx', 'osm_id'=>'yy',..), 1=> array('name'=>'',.. ,),...)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array with different extension's values, Now i want check whether '.csv' is IN this Array or not . If it is then whats the name.
Ex: - Array
(
[0] => xyz.mp4
[1] => bulk_sample.csv
[2] => abc.avi
[3] => pqr.3gp
)
Here 'abc.csv' is available in array. and name should be in $name='abc.csv';
Simple "one-line" solution using preg_grep function:
$names = preg_grep("/\.csv$/i", $val);
print_r($names);
The output:
Array
(
[3] => abc.csv
)
Note, that hypothetically there could be multiple .csv items
Try this code :
In this foreach loop is checking for sub string which is .csv for all elements in array.
<?php
$array =array('test.mp4','abc.avi','xyz.3gp','abc.csv');
$flag=false;
$filename="";
foreach($array as $check) {
$place = strpos($check, ".csv");
if ($place>0) {
$flag=true;
$filename=$check;
break;
} else {
$flag=false;
}
}
if($flag){
echo "File Name:".$filename;
}else{
echo "not found any .csv in array"."<br>";
}
?>
Note: it will return first found name with .csv extension.