This question already has answers here:
How to determine if an array has any elements or not?
(8 answers)
Closed 4 years ago.
Is possible to loop trough an array in php like we do it in JavaScript for example without using the for ( $X as $Y){}
For example we in JavaScript we can use this code :
var names=['john','tom','jane'];
for (i=0;i<names.length;i++){
names[i];
}
Now in the case of using the same method for this loop it would be this one and it gives us an error :
$names=['john','tom','jane'];
for ($i=0;$i<$names.length;$i++){
$names[$i];
}
So is there a way around this?
You can use count() for the length of the array.
$names = ['john', 'tom', 'jane'];
for ($i=0; $i < count($names); $i++){
echo $names[$i];
}
First of all the code gives you errors because you have written lenght wrong. It should be sizeof() or count().Secondly there is also another option for looping through array. That’s foreach. As I’m not on the computer you could check out the php manual for foreach and how to use it. I hope I was useful !
Related
This question already has answers here:
How to get value at a specific index of array In JavaScript?
(8 answers)
Closed 2 years ago.
so I am currently learning javascript on Codecademy. And a weird kind of thing got introduced that I don't quite get.
if you look at the code. you see at the end in console that after logging animals it is a [i] like why is that there? I get that is has something to do with the for loop. But I don't quite understand like why or what that it does. I don't know if question is clear enough but if you just try to explain why it is there and what it does there. that would be greatly appreciated:)
const animals = ['Grizzly Bear', 'Sloth', 'Sea Lion'];
for (let i = 0; i < animals.length; i++){
console.log(animals[i]);
}
the bracket notation is used to specify the index of the array you are looping through. You can always read more about javascript arrays on w3schools
This question already has answers here:
Short circuit Array.forEach like calling break
(30 answers)
Closed 5 years ago.
I have an object mobile_specs which have several fields such as DeviceName, Brand, Camera.
I loop though the mobile_specs object so that i can print the specifications of two mobiles in tabular format:
var i=0;
Object.keys(mobile_specs).forEach(function(key) {
if(i==5)
{
break;
}
var mobile1=mobile_specs.[key];
var mobile2=mobile_specs.[key];
alert(mobile1 + " " +mobile2);
i++;
});
But the above code give me an error which is:
Illegal break statement
How can i break my loop when i==5 ?
Any help is appreciated.
There is no way to stop or break a forEach() loop other than by throwing an exception. I think forEach is not suited for your job, use a simple loop instead
This question already has answers here:
How to convert JSON string to array
(17 answers)
Closed 7 years ago.
I need to be able to grab some data from a string using PHP.
I got an API from the games website and need to break it down.
The string I need to break down is this:
http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513
I need to get the small icon image from that string which is the first url in the string, and the current price, which in the string is 887.
So, where it states this:
"current":{"trend":"neutral","price":887}
I need to grab the 887 and put it into a variable.
I'm using PHP,
thanks in advance if anyone can help :)
Use json_decode() for this purpose:
$item = json_decode($json)->item;
$price = $item->current->price;
$icon = $item->icon;
Download the JSON and convert to an object.
$item = json_decode(file_get_contents('http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513'))->item;
$name = $item->name;
// etc.
Here's a function you should keep handy. It is useful for printing formatted objects and arrays, which makes it much easier to examine their structures.
function debug($v) {
echo '<pre>';
print_r($v);
echo '</pre>';
}
This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 9 years ago.
Is there a built-in function in javascript to do this or this is only the option to go? Please look at the code below:
var arr=[1,3,4,'+','-', or whatever]
function value_check(user_click){
var operators=['+','-','/','*','.']
for (var i=0;i<operators.length;i++){
if (arr[arr.length-1]==operators[i]){var value1='operator found';}
if (user_click==operators[i]){
var value2= value1;alert("consecutive operators"); break;
}
}
}
I think this code achieves what I intend to do but is there a simple and shorter way of doing this. In words, I want to achieve something like this:
if (arr[arr.length-1] && user_click BOTH ARE IN operators array)
alert("consecutive operators)
Yes, there are some options:
JavaScript indexOf()
jQuery.inArray()
arrayName.indexOf() is what you are looking for.
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
im wondering if there is a function or something like php's <?php echo $_GET['height'];?> but in HTML or javascript.
I need it to get variables of the URL
Use location.search. It returns the entire query string, which you then can split, if you need to.
Html is a markup language, so you can't do such things.
Try using javascript.
here's an example:
//returns the height of the client
function height(){
var clientHeight = document.body.clientHeight;
alert(clientHeight);
}