Get the first element of my array $_POST['tableFields'] - php

I'm trying to get the first element of my asociative array $_POST['tableFields'].
print_r($_POST['tableFields']); // I get: ["id","usuario","apellido1","apellido2","email","password"]
I tryied using reset() method but doesn't show anything.
$campo = reset($_POST['tableFields']);
print_r($campo); // This doesn't show anything.

It is JSON format so its a string value. You must do print_r(json_decode($_POST['tableFields'],1)[0]); or reset(json_decode($_POST['tableFields'],1));

Related

PHP JSON Arrays within an Array

I have a script that loops through and retrieves some specified values and adds them to a php array. I then have it return the value to this script:
//Returns the php array to loop through
$test_list= $db->DatabaseRequest($testing);
//Loops through the $test_list array and retrieves a row for each value
foreach ($test_list as $id => $test) {
$getList = $db->getTest($test['id']);
$id_export[] = $getList ;
}
print(json_encode($id_export));
This returns a JSON value of:
[[{"id":1,"amount":2,"type":"0"}], [{"id":2,"amount":25,"type":"0"}]]
This is causing problems when I try to parse the data onto my android App. The result needs to be something like this:
[{"id":1,"amount":2,"type":"0"}, {"id":2,"amount":25,"type":"0"}]
I realize that the loop is adding the array into another array. My question is how can I loop through a php array and put or keep all of those values into an array and output them in the JSON format above?
of course I think $getList contains an array you database's columns,
use
$id_export[] = $getList[0]
Maybe can do some checks to verify if your $getList array is effectively 1 size
$db->getTest() seems to be returning an array of a single object, maybe more, which you are then adding to a new array. Try one of the following:
If there will only ever be one row, just get the 0 index (the simplest):
$id_export[] = $db->getTest($test['id'])[0];
Or get the current array item:
$getList = $db->getTest($test['id']);
$id_export[] = current($getList); //optionally reset()
If there may be more than one row, merge them (probably a better and safer idea regardless):
$getList = $db->getTest($test['id']);
$id_export = array_merge((array)$id_export, $getList);

PHP adressing the first key in an array that changes

For example, lets say I have an array that looks liked
$stuff = array('random_string_1' => 'random_value_1','random_string_2' => 'random_value_2');
and then I call the asort() function and it changes the array around. How do I get the new first string without knowing what it actually is?
If you want to get the first value of the array you can use reset
reset($stuff);
If you want to also get the key use key
key($stuff);
If you need to get the first value, do it like this:
$stuff = array('random_string_1' => 'random_value_1','random_string_2' => 'random_value_2');
$values = array_values($stuff); // this is a consequential array with values in the order of the original array
var_dump($values[0]); // get first value..
var_dump($values[1]); // get second value..

PHP - JSON Parsing - add and remove object

I am getting [{"2016":"200"},{"2017":"200"}] This JSON string from server , I want to append another object ie. {"2018":"324"} at the end of the existing JSON array and want to delete object at the 0th index the existing JSON array so that the my desired result [{"2017":"200"}{"2018":"324"}] can be obtained.
Here's what i am doing
$str = json_decode($x1,TRUE); //x1 is my JSON
array_push($str, array("2018"=>"324")); //adding another object
unset($str[0]); //removing 0th index
$s = json_encode($str,TRUE); //making JSON again
echo $s;
Problem here is $s giving output in object form like {"1":{"2017":"200"},"2":{"2018":"35"}} While what is wanted is [{"2017":"200"}{"2018":"324"}]
I might suggest better approach, using array_shift():
$json = json_decode($x1, TRUE); // decoding
array_shift($json); // removing first element
$json[] = [ "2018" => "324" ]; // adding last element
$str = json_encode($json); // encoding
echo $str;
As others already pointed out, you getting object equivalent because your array not starting from 0 index, so it is converted in javascript object with numeric keys. I'm using array_shift() to remove first element. It will also reset indices in array. It also would be (insignificantly) faster, if you remove first key before adding other elements to array.

PHP: inline get element from associative array returned by a function

I have a PHP function returning an associative array and I would like to get an element from it. Normally, I first create array from function and then I get element
$associative_array = someFunction();
$element = $associative_array['key'];
Is it possible to get element directly inline? (I can't change function to return the element).
Something like this (which fails due to syntax error, but it's just to show the idea):
$element = someFunction()['key'];

Parse JSON data within array

I have the below JSON and trying to parse it with this foreach. I only get -104.21 returned. I am needing the output to be 37.72,-104.21
"start_latlng":[
37.72,
-104.21
],
PHP to loop through "start_latlng" array: When I echo $stLatLng I only get -104.21
foreach($stData['start_latlng'] as $latlng) {
$stLatLng = $latlng;
}
From the fact that you're getting -104.21 I presume you are already decoding the JSON into a PHP format.
The reason you'll only be getting that value is because it is the last value in the array, and you're assigning $stLatLng to each value of your array as you iterate over it, so the variable will get overwritten each time, see example:
foreach($stData['start_latlng'] as $latlng) {
$stLatLng = $latlng; // first time it's 37.72, second time it's -104.21
}
Here's a demo of what's happening in that loop.
To get both your values, you don't need to loop it. You can just use list() to get both values out (example):
list($lat, $lng) = $stData['start_latlng'];
// $lat = 37.72, $lng = -104.21

Categories