This line I am replacing:
reset($currencies->currencies);
while (list($key, $value, $title) = each($currencies->currencies)) {
I think this is correct? or am I wrong?
foreach($currencies->currencies as $key => $value, $title) {
The foreach construct has two forms: (foreach $foo as $bar), and (foreach $array as $key=>value). Therefore, a comma-separated "value" isn't going to work, or so it seems, because you apparently have a multi-dimensional data structure (more than one $value for each $key).
You also don't say whether ($currencies->currencies) is an array, or another object.
If $currencies->currencies is an object:
foreach ($currencies->currencies as $myObject) {
echo "The key is: " . $myObject->key;
echo "The value is: " . $myObject->value;
echo "The title is: " . $myObject->title;
}
If $currencies->currencies is an array:
foreach ($currencies->currencies as $myArray) {
echo "The key is: " . $myArray['key'];
echo "The value is: " . $myArray['value'];
echo "The title is: " . $myArrat['title'];
}
Related
this is my code, I want to echo all the data inside this $json.
$json = '{"year":"2018","month":"4","name":"Apr 2018 - Sept 2018","description":null,"code":"2018\/04","session_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"session_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_to_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"added_dropped_from_at":null,"added_dropped_to_at":null,"withdrew_from_at":null,"withdrew_to_at":null,"attended_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"attended_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_from_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"created_by":1,"updated_by":1,"id":2}';
$arr = json_decode($json,true);
foreach($arr as $key=>$value){
echo $key . "<br>";
echo $value . "<br>"; // PHP Notice: Array to string conversion in /workspace/Main.php on line 11
// not displaying the value
}
?>
the problem come went the data loop on session_from_at which have three data in array inside.
I think you are not using blade. Your file name is Main.php
If you are using blade, I advise you to use #foreach instead of foreach.
And after json_decode, you still have an array inside array.
Try this code:
$json = '{"year":"2018","month":"4","name":"Apr 2018 - Sept 2018","description":null,"code":"2018\/04","session_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"session_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_to_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"added_dropped_from_at":null,"added_dropped_to_at":null,"withdrew_from_at":null,"withdrew_to_at":null,"attended_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"attended_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_from_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"created_by":1,"updated_by":1,"id":2}';
$arr = json_decode($json, true);
function echoArray($arr) {
foreach($arr as $key=>$value){
echo $key . "<br>";
if (is_array($value)) {
echoArray($value);
} else {
echo $value . "<br>";
}
}
}
echoArr($arr);
You got an error because other $value is an array you cant echo it so try to check if its array or object like this :
foreach($arr as $key=>$value){
echo $key . "<br>";
if(is_object($value) || is_array($value)){
foreach($value as $key2=>$value2){
echo $key2 . "<br>";
echo $value2 . "<br>";
}
}else{
echo $value . "<br>";
}
}
try it:
$arr['year'];
$arr['month'];
you do not need to use foreach.
Currently working on a project in C but I have to generate a large struct which I figured would generate in PHP since I'm more familiar with PHP.
I have 2 arrays. The first one is quite simple (is way larger than this but I assume this is enough to replicate the issue):
$vehicles = [
'vehicleSuper' => [ "adder", "autarch", "banshee2", "bullet" ],
'vehicleSport' => [ "alpha", "banshee", "bestiagts", "blista2" ],
//...
];
The second array looks like this:
$textures = [
'candc_apartments' => [
"limo2_b", "limo2",
],
'candc_default' => [
"marshall_flag18_b", "mesa_b", "rentbus", "marshall_flag21_b", "crusader", "boxville4", "buzzard_b", "dukes2_b", "dukes",
],
'lgm_default' => [
"hotknife", "coquette", "voltic_tless", "vacca", "infernus", "cogcabri_b", "stinger_b", "banshee_b", "ztype", "ninef", "jb700", "superd", "monroe_b", "rapidgt2_b", "khamel", "comet2_b", "cheetah_b", "rapidgt_b", "stinger", "carbon_b", "surano_convertable_b", "rapidgt2", "infernus_b", "jb700_b", "ninef_b", "stingerg", "superd_b", "bullet_b", "ztype_b", "hotknife_b", "cogcabri", "surano_convertable", "rapidgt", "stingerg_b", "coquette_b", "bullet", "carbon", "ninef2", "carboniz", "cheetah", "adder_b", "entityxf", "adder", "feltzer",
],
];
Now I generate the list like this using the 2 arrays above:
echo '<pre>';
foreach($vehicles as $category => $val) {
echo "vehicleSpawner " . $category . "[] = {\n";
foreach($val as $item) {
echo " { \"" . $item . "\", \"" . array_search($item, array_column($textures, $item)) . "\", \"\" },\n";
}
echo '}';
echo '<hr>';
}
This outputs something similar to:
{ "adder", "", "" },
The last 2 values are empty. What I want to achieve: Fill these out with the values from $textures array. In this case, I'm trying to fill it up like this:
// arg 1: name from $vehicles
// arg 2: key from $textures
// arg 3: val from $textures
{ "adder", "lgm_default", "adder" },
Currently my array_search($item, array_column($textures, $item)) method doesn't seem to work. How would I go and get this working? Help is appreciated, thanks all!
array_column($textures, $item) will search in array $textures key named $item, but there is no key with that name so it returns false, what you need to do is loop on $textures array and search if value $item exists:
foreach($val as $item) {
foreach($textures as $k => $v) {
//this will return array index or false if not exists
$pos = array_search($item, $v);
if ( $pos !== false )
echo " { \"" . $item . "\", \"" . $k . "\", \"".$v[$pos]."\" },\n";
}
}
If you want to see multiple matches within a given pair of subarrays, array_intersect() will compare both subarrays once and iterate the results. This is more efficient than running fresh array_search() calls for each element of one of the subarrays.
Code: (Demo)
foreach($vehicles as $category => $v_vals) {
echo "vehicleSpawner " . $category . "[] = {\n";
foreach($textures as $k => $t_vals) {
foreach (array_intersect($v_vals, $t_vals) as $match) {
echo " { \"$match\", \"$k\", \"$match\" },\n";
}
}
echo "}\n";
}
Output:
vehicleSpawner vehicleSuper[] = {
{ "adder", "lgm_default", "adder" },
{ "bullet", "lgm_default", "bullet" },
}
vehicleSpawner vehicleSport[] = {
}
If this isn't the exact output format/structure that you desire, it can be tweaked to accommodate.
So I have this array that saves a json string.
Array
(
[0] => "2jDQoU9D2wu04wqkg0ImUI":{"date":"2016-08-02 14:08:49","type":"story","story_id":"2jDQoU9D2wu04wqkg0ImUI","series_id":"1RAv0uDbcIieYgYqywqYmk"}
)
I want to be able to access just the value "2jDQoU9D2wu04wqkg0ImUI" at the start of the json string and then also be able to do something like $value[0]['type'] to get the type from this json string object. I'm pretty new to PHP and struggling to get this working. I've tried JSON encoding/decoding and can't seem to get anything to work.
What's the proper way to go about this? Thanks in advance.
I hope this code will solve your problem.
$array[0] = '"2jDQoU9D2wu04wqkg0ImUI":{"date":"2016-08-02 14:08:49","type":"story","story_id":"2jDQoU9D2wu04wqkg0ImUI","series_id":"1RAv0uDbcIieYgYqywqYmk"}';
//print_r($arr);
$JsonString = '{' . $array[0] . '}';
$json = json_decode($JsonString);
foreach($json as $key => $value){
echo "Key : $key <br />";
echo "Type : ". $value->type."<br />";
echo "date : ". $value->date."<br />";
echo "story_id : ". $value->story_id."<br />";
echo "series_id : ". $value->series_id."<br />";
}
so you have array $jsonArray and you want to access just the key of it
if its single dimension array :
echo key($jsonArray); //prints 2jDQoU9D2wu04wqkg0ImUI
Otherwise if its multidimensional you can loop through it and do whatever you want with each key
foreach($jsonArray as $key => $value) {
echo $key; //prints 2jDQoU9D2wu04wqkg0ImUI
}
Try this:
$arrayOfObjects = [];
foreach($array as $key => $value) arrayOfObjects[] = json_decode($value);
Now you can loop through this new $arrayOfObjects
foreach($arrayOfObjects as $key => $object){
echo $object->date . '<br>';
echo $object->type . '<br>';
echo $object->story_id . '<br>';
echo $object->series_id . '<br> ==== >br> ';
}
I have an array $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);. How can i get (echo) all names and corresponding ages in a single instance (like foreach $value as $value)?? The array may have more data than shown here.
foreach ($ages as $name => $age) {
echo "$name = $age\n";
}
u have a lot options
like
print_r
print_r($array)
var_dump , foreach
print_r($array);
or
var_dump($array)
foreach ($ages as $key => $value) {
echo $key . "'s age is " . $value . "<br />";
}
I have a POST in PHP for which I won't always know the names of the variable fields I will be processing.
I have a function that will loop through the values (however I would also like to capture the variable name that goes with it.)
foreach ($_POST as $entry)
{
print $entry . "<br>";
}
Once I figure out how to grab the variable names, I also need to figure out how I can make the function smart enough to detect and loop through arrays for a variable if they are present (i.e. if I have some checkbox values.)
If you just want to print the entire $_POST array to verify your data is being sent correctly, use print_r:
print_r($_POST);
To recursively print the contents of an array:
printArray($_POST);
function printArray($array){
foreach ($array as $key => $value){
echo "$key => $value";
if(is_array($value)){ //If $value is an array, print it as well!
printArray($value);
}
}
}
Apply some padding to nested arrays:
printArray($_POST);
/*
* $pad='' gives $pad a default value, meaning we don't have
* to pass printArray a value for it if we don't want to if we're
* happy with the given default value (no padding)
*/
function printArray($array, $pad=''){
foreach ($array as $key => $value){
echo $pad . "$key => $value";
if(is_array($value)){
printArray($value, $pad.' ');
}
}
}
is_array returns true if the given variable is an array.
You can also use array_keys which will return all the string names.
You can have the foreach loop show the index along with the value:
foreach ($_POST as $key => $entry)
{
print $key . ": " . $entry . "<br>";
}
As to the array checking, use the is_array() function:
foreach ($_POST as $key => $entry)
{
if (is_array($entry)) {
foreach($entry as $value) {
print $key . ": " . $value . "<br>";
}
} else {
print $key . ": " . $entry . "<br>";
}
}
It's much better to use:
if (${'_'.$_SERVER['REQUEST_METHOD']}) {
$kv = array();
foreach (${'_'.$_SERVER['REQUEST_METHOD']} as $key => $value) {
$kv[] = "$key=$value";
}
}
If you want to detect array fields use a code like this:
foreach ($_POST as $key => $entry)
{
if (is_array($entry)){
print $key . ": " . implode(',',$entry) . "<br>";
}
else {
print $key . ": " . $entry . "<br>";
}
}