Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The problem is actually, there is an array containing a number of arrays. I have to print the elements of one of the sub arrays named 'list'. But I have a problem to fetch the subarray while using foreach loop.
The following code is:
foreach($arr as $key => $value)
{
$arr1=$value[$list];
echo $arr1;
}
Your code should be:-
// declare second array outside loop
$arr1 = [];
foreach($arr as $key => $value)
{
// list is a key in $value array then you can access via below way
$arr1[] = $value['list'];
}
// print Array using print_r() function outside loop
print_r($arr1);
Hope it will help you :)
If your array like in below format
<?php
$arr = array(
'key' => array('array elements here'),
'list' => array('array elements here'),
'key' => ........
);
?>
you can use below to get only list array
<?php
foreach($arr as $key => $value)
{
if($key == 'list') {
$arr1=$value['list'];
break;
}
}
print_r($arr1);
?>
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
The code below successfully print an array result of all the PHP files in a folder.
$search_data = glob('download/*.php');
print_r($search_data);
Below is the array result:
Array ( [0] => download/index.php
[1] => download/register.php )
Here is my question: how do I print all the files? I have tried adding the code below but it throws error:
Uncaught TypeError: json_decode(): Argument #1 ($json) must be of type string, array given
Code:
$json = json_decode($search_data, true);
foreach ($json as $data) {
//print or list all the files
echo $files = $data;
echo "<br>";
}
If I understand you correctly, its as simple as...
<?php
$files = glob('download/*.php');
foreach($files as $file){
echo $file . '<br />';
}
$search_data isn't a json string to decode. It's already an array. You'd just loop through it normally. Then use echo "$data<br>\n";
Thus you'd have:
$search_data = glob('download/*.php');
foreach ($search_data as $data) {
echo "$data<br>\n";
}
Basically you can cycle the $search_data array into a foreach loop.
If you want to print only the name of each file, you can use pathinfo method to retrieve the base 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
I have two associative arrays:
$array1=[key1=>$value1,key2=>$value2,key3=>$value3];
$array2=[key1=>$value1,key2=>$value2,key4=>$value4];
How to create the following array:
$arraytotal=[key1=>$value1,key2=>$value2,key3=>$value3,key1=>$value1,key2=>$value2,key4=>$value4];
array_merge is not an option because i have to keep the same values in the final array.
Tnx.
You can't have multiple keys set to the same value (you have key1 in each array), and as you've seen, the second key1 will override the first when you use array_merge.
Your options depend on if each original array functions as a separate group of key/value pairs, or if you want similar key/value pairs grouped together.
So, the first one is easy:
$array1=[key1=>$value1,key2=>$value2,key3=>$value3];
$array2=[key1=>$value1,key2=>$value2,key4=>$value4];
$super = [$array1, $array2];
The second needs a bit more looping:
$array1=[key1=>$value1,key2=>$value2,key3=>$value3];
$array2=[key1=>$value1,key2=>$value2,key4=>$value4];
$super = [];
foreach ($array1 as $key => $value) {
$super[$key] = [$value];
}
foreach ($array2 as $key => $value) {
if (!isset($super[$key])) {
$super[$key] = [];
}
$super[$key][] = $value;
}
If you know that you'll always have the same keys in each array, then you can skip the isset
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array like this :
[[a,b],[c,d]]
How can I delete the outer array so it becomes:
[a,b],[c,d]
I tried using unset but cannot find a solution.
i got this answer by asign arrays in loops to another variable.. so how can i access the loops outside the loop it self?
foreach($arr3 as $key => $value)
{
$newArr[$key] = $value;
echo json_encode($value); // this will answer my question
}
echo json_encode($value); // when i echo outside loop it will not display as inside loop
Simple way to do this is as below.
foreach($array as $key => $value)
{
$newArr[$key] = $value;
}
$newArr contains new array which you are asking.
Comment Response
You can also con cat it.
$concat = "";
foreach($arr3 as $key => $value)
{
$newArr[$key] = $value;
$concat .= json_encode($value).',';
}
echo rtrim($concat,',');
You should not delete the outer array. You can simply access the index of the array and cast it to another array.
As an example;
$arr = array(array('x','y'),array('z'));
You can access this with;
$arr[0];
$array2=$array[0];
$array3=$array[1];
unset($array);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
foreach($items as $value)
{
if($value['item1']=='somestring')
{
// some PHP code...
}
}
Shouldn't $value refer to each value in $items array. What do they mean by $value['item1']? Does it mean $items is a multidimensional array or something?
It means $value is an array and therefore $items is a multidimensional array.
The $foo[] syntax is used by arrays and objects that emulate them (if that's what you mean). The index can be an integer or a string. As you already point out, PHP allows multidimensional arrays.
If you print $key and $value then out show you good result
foreach($items as $key=>$value) // $items contain array mean multidimensional
{
print_r($value);
print($key);//$key is index value
//item1 is array element and $value is array exist in $items
if($value['item1']=='somestring')
{
some PHP code...
}
}
so $items multidimensional array and $value array exist in $items array that's by $items multidimensional array
It means that $items is a multi-dimensional array.
And its looped over.
It has following structure for example:
array(
0 => array('item1' => 'something'),
1 => array('item1' => 'something1')
);
Through this code, we are looping so, in the statement,
foreach($items as $value)
We are getting the inner arrays 0, 1, .. and so on.
And in the line, if($value['item1']=='somestring'), we are getting array elements of 0, 1, and so on.
Hope you understand.
'item1' is the key to one of the elements inside the array $value.
So yes, this makes $items a multidimensional array.
All above are true if no errors are found running the code.
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']