$nesAry=array();
$nesAry["name"]="abc";
$nesAry["email"]="abc#email.com";
$nesAry1=array();
$nesAry1["name"]="abc1";
$nesAry1["email"]="abc1#email.com";
$nesAry2=array();
$nesAry2["name"]="abc2";
$nesAry2["email"]="abc2#email.com";
$responseAry = array();
$responseAry[0]=$nesAry;
$responseAry[1]=$nesAry1;
$responseAry[2]=$nesAry2;
echo json_encode($responseAry); // here output like this => [{"name":"abc","email":"abc#email.com"},{"name":"abc1","email":"abc1#email.com"},{"name":"abc2","email":"abc2#email.com"}]
unset($responseAry[1]);
echo "------------removed 1--------";
echo json_encode($responseAry); // but here output like this => {"0":{"name":"abc","email":"abc#email.com"},"2":{"name":"abc2","email":"abc2#email.com"}}
I want Out put Like this after removing an element \n [{"name":"abc","email":"abc#email.com"},{"name":"abc2","email":"abc2#email.com"}]
Please Help me
Try to regenerate your array after unset an item:
$nesAry=array();
$nesAry["name"]="abc";
$nesAry["email"]="abc#email.com";
$nesAry1=array();
$nesAry1["name"]="abc1";
$nesAry1["email"]="abc1#email.com";
$nesAry2=array();
$nesAry2["name"]="abc2";
$nesAry2["email"]="abc2#email.com";
$responseAry = array();
$responseAry[0]=$nesAry;
$responseAry[1]=$nesAry1;
$responseAry[2]=$nesAry2;
echo json_encode($responseAry); // __here output like this => [{"name":"abc","email":"abc2#email.com"},{"name":"abc1","email":"abc1#email.com"},{"name":"abc2"}]__
unset($responseAry[1]);
$responseAry = array_values($responseAry); //regenerate array(reindexing)
echo "------------removed 1--------";
echo json_encode($responseAry); //[{"name":"abc","email":"abc#email.com"},{"name":"abc2","email":"abc2#email.com"}]
EDIT:
As other option you can use array_splice method http://php.net/manual/en/function.array-splice.php
$nesAry=array();
$nesAry["name"]="abc";
$nesAry["email"]="abc#email.com";
$nesAry1=array();
$nesAry1["name"]="abc1";
$nesAry1["email"]="abc1#email.com";
$nesAry2=array();
$nesAry2["name"]="abc2";
$nesAry2["email"]="abc2#email.com";
$responseAry = array();
$responseAry[0]=$nesAry;
$responseAry[1]=$nesAry1;
$responseAry[2]=$nesAry2;
echo json_encode($responseAry); // __here output like this => [{"name":"abc","email":"abc2#email.com"},{"name":"abc1","email":"abc1#email.com"},{"name":"abc2"}]__
array_splice($responseAry,1,1);
echo "------------removed 1--------";
echo json_encode($responseAry);
Your array when first converted to json is a so called "associative" array, and json_encode then exports it to the object you see in the first echo.
After your unset, the array is changed to a "numeric" array and json_encode will export the array with array keys.
Php it self does not care about how the array is used, but json_encode does.
You can use
echo json_encode(array_values($responseAry));
Or not change the final array you want to export
Related
I need to create an array of keys and store it in a db table.
$myArr = array();
foreach($keys AS $key) {
$myArr[$r->id] = $r->key;
}
before storing it I serialize it
$db_arr = serialize($myjArr);
Later I need to get the stores array and loop through it to perform some action. However, when I unserialize stored array and do print_r my output looks like this:
Array ( [5981] => 7u7Dj [5982] => mVmx4 )
It appears that the array is malformed. What am I missing?
You should take a look at this, I think you may need to unserialize the data before trying to use it php unserialize
$array = unserialize($serialized_array);
Here is an example
$original = [
"who" => "you",
"me" => "yes"
];
echo "<pre>";
print_r($original);
echo "</pre>";
$ser = serialize($original);
echo "<pre>";
print_r($ser);
echo "</pre>";
$un = unserialize($ser);
echo "<pre>";
print_r($un);
echo "</pre>";
So I got this array:
$list = simplexml_load_file('http://feu01.ps4.update.playstation.net/update/ps4/list/eu/ps4-updatelist.xml');
if($list) {
$data = array('firmware' => $list->region->system_pup[0]['label']);
header('Content-Type: application/json');
echo json_encode($data);
}
And it outputs this:
{"firmware":{"0":"5.01"}}
But there's also a zero there. How do I remove it from the array so it's like this:
{"firmware":"5.01"}
This is a SimpleXMLElement result which you need to convert to a string if you want that data:
$data = array('firmware' => (string) $list->region->system_pup[0]['label']);
In JSON form it looks like an associative array, which it isn't, so the [0] trick which normally works to navigate to it won't.
I am very new in PHP and I was checking whether all my controlers are in so how can I echo this? what I tried resulting nothing
$controllers = array_intersect($json_api->get_controllers(), $active_controllers );
$return = array(
'json_api_version' => $version,
'controllers' => array_values($controllers)
);
echo $return['controllers']['controllers'];
use print_r function:
print_r($return['controllers']);
when you want read city you do:
$arr_controllers = $return['controllers'];
$key_2 = $arr_controllers[2];
where 2 is the key
If you want to print the values of an array with a
understandable format you should use print_r() instead of echo like so:
print_r($return['controllers']);
You can also use var_dump() to get some extra information about the fields, like the type and lenght.
If what you need is to asign a certain index of the array to a value just do something like this:
$variable = $return['controllers'][indexOfField]; // indexOfField=2 for city field
echo $variable;
For further information about print_r() check the official manual.
How would I setup an associative array to reference specific values at different sections of a page. My function:
<?php
function park_data($park_page_id) {
$data = array();
if($park_page_id){
$data = mysql_fetch_assoc(mysql_query("SELECT * FROM `park_profile` WHERE `park_id` = $park_page_id"));
return $data;
}
}
?>
My print_r:
<?php
print_r (park_data(1));
?>
Produces the following associative array:
Array ( [park_id] => 1 [park_name] => Kenai Fjords [park_address] => 1212 4th Avenue [park_city] => Seward [park_state] => Alaska [park_zip] => 99664)
How would I print just the [park_name] value from this array?
From the docs:
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.
// on PHP 5.4
print_r(park_data(1)['park_name']);
// earlier versions
$tmp = park_data(1);
print_r($tmp['park_name']);
$park=park_data(1);
echo $park['park_name'];
To output custom formatted text in general, and in this case to output only a single array's key value, use echo, because print_r() called on an array displays the whole array's structure and content, and that's not what you want:
<?php
// code
$park_data=park_data(1);
echo $park_data["park_name"];
// code
?>
I am really going crazy at the moment, i have the following serialized, however when i try to unserialized it returns false.
Serialized array:
Array
(
[0] => a:1:{i:0;s:9:"714443801";}
)
Current Code:
<?php
$votesArray = unserialize($Vzzz);
echo "<pre>";
print_r($votesArray);
echo "</pre>";
?>
You are not unserializing it:
try:
print_r(unserialize($votesArray[0]))
If you are storing the serialized string, then store like this:
$serialisedData = base64_encode(serialize($arr));
After Retrieving,
//to unserialize...
$arr = unserialize(base64_decode($serialisedData));