i am trying to set the web addresses as the valves and set the keys to be short names for the sites. Not sure where im going wrong. Everytime i try and run it it keeps saying line 11 which is $http://www.yahoo.co.uk/= array( key => value,("yahoo_uk");
$http://www.yahoo.co.uk/= array( key => value,("yahoo_uk");
foreach ($array as $key =>$value) {
echo $value;
}
?>
</body>
wow :P There are so many syntax errors I don't even know where to begin
Here's the correct syntax
$array = array('http://www.yahoo.co.uk' => 'yahoo_uk');
Read this chapter of the manual:
http://php.net/manual/en/language.types.array.php
It seems you intend to do something like:
$urls = array();
$urls['yahoo_uk'] = "http://www.yahoo.co.uk/";
This initializes an array to store URLs, then creates an array member with the short name yahoo_uk as key, and its corresponding URL as the value.
You can then access it with foreach:
foreach ($urls as $name => $url) {
echo "name: $name, url: $url\n";
}
I assume this is what you were going for
<?php
$array = array('http://www.yahoo.co.uk/' => 'yahoo_uk');
foreach ($array as $key =>$value) {
echo $value;
}
?>
You are trying to set constants within your array and you're using incorrect PHP syntax. Try this instead:
$urls = array('yahoo_uk' => 'http://www.yahoo.co.uk/');
foreach ($urls as $key => $value) {
echo $value;
}
Or calling a single value like this:
echo $urls['yahoo_uk']; // http://www.yahoo.co.uk/
Also, your question is very vague and hard to understand.
Try this code:
$yahoo = array_assoc('http://www.yahoo.co.uk/' => 'yahoo_uk');
foreach ($yahoo as $key => $value) {
echo $value;
}
?>
Related
I have the following JSON string, I'm wanting to cycle through the 'sub' items and print them out as HTML Select Options but for some reason I can't quite get hold of them.
Only used JSON a couple times before so it's probably a rookie mistake somewhere.
{
"1":{
"question":"How happy are you with your car?",
"sub":[
"Very Happy",
"Not So Happy",
"Unhappy",
"Very Unhappy"
]
}
}
I have the following which let's me echo out the Question Value but how would I loop through each of the 'sub' arrays? (There's only ever going to be 1 question which is why I'm storing it in a single variable)
$questionAnswer = json_decode($data->question,true);
foreach ($questionAnswer as $key => $value) {
$question = $value['question'];
}
Add another loop inside the existing one:
$questionAnswer = json_decode($data->question,true);
foreach ($questionAnswer as $key => $value) {
$question = $value['question'];
echo $question."<br>";
// value['sub'] contains the array of 'subs', so you can loop through that the same way
foreach ($value['sub'] as $sub) { // since the key will be 0,1,2 you might not need it here, so I omitted it.
echo $sub."<br>";
}
}
foreach ($questionAnswer as $key => $value) {
$question = $value['question'];
foreach ($value['sub'] as $answer) {
echo $answer;
}
}
How can i know the variables ?
foreach($tree as $key => $value){
if (in_array($value['name'], $arr_folders)) {
${'id_'.$value['name']} = $value['id'];
}
//how to know variables?
$id_???
Currently I know the $value['name'] ie. it may be one,two, three, etc. but how to use them
echo $id_one;
I wanted to know here to split them in an array. So i can use
print_r($vars); which would result $id_one, $id_two, etc..
Something like this?
<?php
$array = [];
foreach($tree as $key => $value){
if (in_array($value['name'], $arr_folders)) {
$array[] = $value['id'];
}
}
print_r($array);
You can find variables by code:
foreach (get_defined_vars() as $var_name => $var_value) {
if(strpos($var_name, 'id_') ===0 ){
//it's your variable
}
}
But store variable in local scope look wrong.
May be better store to an other array:
$arIds = array();
foreach($tree as $key => $value){
if (in_array($value['name'], $arr_folders)) {
$arIds['id_'.$value['name']] = $value['id'];
}
}
I have an array that looks like this:
$array = array(
array(
"http://google.com",
"Google"
),
array(
"http://yahoo.com",
"Yahoo"
)
);
What is the simplest way to loop through it. Something like:
foreach ($array as $arr) {
// help
}
EDIT: How do I target keys, for example, I want to do:
foreach ($array as $arr) {
echo '<a href" $key1 ">';
echo ' $key2 </a>';
}
In order to echo out the bits you have to select their index in each array -
foreach($array as $arr){
echo ''.$arr[1].'';
}
Here is an example.
Use nested foreach() because it is 2D array. Example here
foreach($array as $key=>$val){
// Here $val is also array like ["Hello World 1 A","Hello World 1 B"], and so on
// And $key is index of $array array (ie,. 0, 1, ....)
foreach($val as $k=>$v){
// $v is string. "Hello World 1 A", "Hello World 1 B", ......
// And $k is $val array index (0, 1, ....)
echo $v . '<br />';
}
}
In first foreach() $val is also an array. So a nested foreach() is used. In second foreach() $v is string.
Updated according to your demand
foreach($array as $val){
echo ''.$val[1].'';
}
The easiest way to loop through it is:
foreach ($array as $arr) {
foreach ($arr as $index=>$value) {
echo $value;
}
}
EDIT:
If you know that your array will have always only two indexes then you can try this:
foreach ($array as $arr) {
echo "<a href='$arr[0]'>$arr[1]</a>";
}
First modify your variable like this:
$array = array(
array("url"=>"http://google.com",
"name"=>"Google"
),
array("url"=>"http://yahoo.com",
"name"=>"Yahoo"
));
then you can loop like this:
foreach ($array as $value)
{
echo '<a href='.$value["url"].'>'.$value["name"].'</a>'
}
The way to loop through is,
foreach($array as $arr)
foreach($arr as $string) {
//perform any action using $string
}
Use the first foreach loop without { } for the simplest use.
That can be the most simple method to use a nested array as per your request.
For your edited question.
Wrong declaration of array for using key.
$array = array(
"http://google.com" => "Google",
"http://yahoo.com" => "Yahoo" );
And then, use the following.
foreach ($array as $key => $value)
echo "<a href='{$key}'>{$value}</a>";
This doesn't slow down your server's performance.
In modern versions of php, you can assign (destructure) subarray values to variables of your choosing.
A good tutorial: https://stitcher.io/blog/array-destructuring-with-list-in-php
Code: (Demo)
$array = [
["http://example1.com", "Example 1"],
["http://example2.com", "Example 2"]
];
foreach ($array as [$key1, $key2]) {
echo "$key2\n";
}
Output:
Example 1
Example 2
To be honest, I'd probably name the variables $url and $text respectively.
I want to use a multidimensional array in different functions.so i am making it as a global variable(array).i created a multidimensional array and made it as global to access in different function.now how can i get the values from it using foreach loop?
here is my code
$test=array(
array(
"input1"=>"v1",
"input2"=>"v2"),
array(
"input3"=>"v3",
"input4"=>"v4")
);
class testing
{
function testp()
{
global $test;
foreach($test as $key => $value)
{
echo $value;
}
var_dump($test);
echo is_array($test);
}
}
$obj = new testing();
$obj->testp();
i used is_array and var_dump to confirm whether its an array.
all are fine
and its shwoing Error suppression ignored. now how can i get the values from it?
It is array of arrays, what works for top order array, works further as well:
foreach($test as $key => $value)
{
foreach($value as $k => $v){
echo $v;
}
}
This will echo your values v1, v2, v3, v4 one after another.
More general answer:
public function visitArray($test)
{
foreach($test as $key=>$value)
{
if(is_array($value))
{
visitArray($value);
}
else
{
echo $value;
}
}
}
Edit
Don't know why you're looping over keys and values, if key isn't took into account
More easy & simple way to access array values within a array.
foreach($test as $array_value){
if(is_array($array_value)) {
foreach ($array_value as $value) {
echo $value.'<br>';
}
}
}
I have a loop and each time the loop runs I need to add two variables to an array. What I am trying right now is:
$attach_array['outline'] = array();
foreach ($_POST['attachment'] as $key => $value) {
$attachmentName = $value['name'];
$path = "1";
$name = "alsdkjf";
$attach_array['outline']['path']=$path;
$attach_array['outline']['name']=$name;
}
Then later in the script I try to get these values out for PHPMAILER:
foreach ($attach_array['outline'] as $key => $value) {
$mail->AddAttachment($value['path'], $value['name']);
}
This and other attempts are not working so I'm hoping for some help on putting $name and $path into an array in my first loop to use later.
You are overriding the same variables on each loop. You should do something like this:
$attach_array['outline'][] = array('path' => $path, 'name' => $name);
By doing this, now all the path and values will remain on the array as separate items. You dont have to change the code you are using it from.