Array shows values but print_r(array_values) is empty - php

I'm getting a response from a service and print_r() shows an array but it seems to be different from usual arrays.
If I print array_values it's are empty.
PHP:
print_r($token);
//Result: Array ( [{"access_token":"123","token_type":"bearer"}] => )
print_r(array_values($token));
//Result: Array ( [0] => )
Why are the access_token and token_type values not listed in array_values?

The answer is not JSON, It's not because you have a JSON type array. It is because there is NO value in your array.
//Result: Array ( [{"access_token":"123","token_type":"bearer"}] => )
That array has one index with no value, hence array_values shows nothing. You have created that array incorrectlly :)

I can reproduce your outputs with this:
$token = array('{"access_token":"123","token_type":"bearer"}' => '');
That is because you have an array with the key
'{"access_token":"123","token_type":"bearer"}'
but no value.
To access the JSON string in the array key, you could do this:
$keys = array_keys($token);
print_r($keys[0]);
To access the JSON object, you can further do
print_r(json_decode($keys[0]));
Output:
(
[access_token] => 123
[token_type] => bearer
)
Demo: Fiddle

Related

get value within object in an array

I have an array, containing an object. I need the value of a property of the first object but somehow I get an empty value.
My array $params (from print_r) looks like this:
Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...
Cut off here, there are two more objects in this array.
Now if I do: echo $params[0]->name I get an empty result.
Also tried print_r($params[0], true);, empty result.
Also tried, empty result:
$status = $params[0];
echo $status->name;
What am I doing wrong here?
Thanks in advance
Well, as you said your array looks like this :
Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...
So there is no $param[0], you should do $param['newOrderStatus'] and then get what you want : $param['newOrderStatus']->name
You need to access object as following
$params['newOrderStatus'];
In above object you will have all child objects so you can access them by following
$params['newOrderStatus']->name;
$params['newOrderStatus']->template;
Your array $params has a key called newOrderStatus which has the object as a value you are looking for.
Looking at your example, there is value for index 0.
To get the value of the name property, you could use:
$params['newOrderStatus']->name
You can type cast it to an array like this:
$array = (array) $yourObject;

PHP insert keys in string

I have the following string:
{"1":"http://localhost:8888/classecar/uploads/dropzone/ferrari1.jpg","2":"http://localhost:8888/classecar/uploads/dropzone/ferrari2.jpg","3":"http://localhost:8888/classecar/uploads/dropzone/ferrari3.jpg","4":"http://localhost:8888/classecar/uploads/dropzone/ferrari4.jpg"}
How can I get rid of the numbers preceding "http..." and transform this same numbers in array keys?
Like this:
[0] => "http...",
[1] => "http...",
[2] => "http...",
That looks like a JSON string, so you could decode it.
You could try
$array = json_decode($string, true);
You may also need to reindex the array so it is 0 based; so something like
$array = array_values(json_decode($string, true));
you are having an array data in JSON formation. you have to use a PHP function json_decode to get an result.
$php_array = json_decode($your_array[0], true);
//to see your array
print_r($php_array);
You are missing something I think.
Look at this snippet:
$a=[
0 => '{
"1":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari1.jpg",
"2":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari2.jpg",
"3":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari3.jpg",
"4":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari4.jpg"
}'
];
echo "<pre>";
print_r(json_decode($a[0],TRUE));
it returns:
Array
(
[1] => http://localhost:8888/classecar/uploads/dropzone/ferrari1.jpg
[2] => http://localhost:8888/classecar/uploads/dropzone/ferrari2.jpg
[3] => http://localhost:8888/classecar/uploads/dropzone/ferrari3.jpg
[4] => http://localhost:8888/classecar/uploads/dropzone/ferrari4.jpg
)
This will work considering that the array value is a "string" containing a json object.
A var_dump on your array will give you an exact idea on what type the array value is.
Hope this helps:
<?php
//you might want to convert the JSON string to an array:
$json = '{"1":"http://localhost:8888/classecar/uploads/dropzone/ferrari1.jpg","2":"http://localhost:8888/classecar/uploads/dropzone/ferrari2.jpg","3":"http://localhost:8888/classecar/uploads/dropzone/ferrari3.jpg","4":"http://localhost:8888/classecar/uploads/dropzone/ferrari4.jpg"}';
$array = json_decode($json);
var_dump($array);
// here you already have the json converted to an php array
$arrayWithoutStrangeIndexes = [];
foreach($array as $index => $content){
$arrayWithoutStrangeIndexes[]= $content;
}
// here is just your array with plain data
var_dump($arrayWithoutStrangeIndexes);

print multidimensional array in json response

How can print multidimensional array of single array.
$data = array(
'ride_categry'=>$ridecategory,
'pool_ride_type'=> $pollride,
'ride_looking_for'=> $ridefor,
'seating_capacity'=>$seatingcapcity,
'ride_from'=> $rideform,
'ride_to'=> $rideto,
'daily_route'=> $daily,
//'ride_now'=> $ridenow,
'ride_date'=> $ridedate,
'ride_time'=> $ridetime,
);
print this in multidimensional array like
Array
(
[one] => Array
(
[two] => Array
(
[three] => Array
(
[four] =>
)
)
)
)
PHP arrays are not multi-dimensional, they are nested. The example you gave does not illustrate a nested array.
If you want to exchange compound data structures between PHP and Javascript then use the builtin JSON routines:
<?php
$data=array(...);
header("Content-Type: application/json");
print json_encode($data);
If you want to print all the nested data from $data use print_r function:
print_r($data);

Get value of nested key name in php array

I have been working with parsing some remote JSON with PHP. I have been able to download the JSON and assign it to a variable, and I have used the array functionality with json_decode:
$data = json_decode($remotejson, true);
I have then printed the complete array back to verify the contents of the array:
echo print_r($data);
The array prints back and I can see the keys and values:
[files] => Array
(
[/photogalleryupload.thumbs/1934307_000001.jpg] => Array
(
[source] => derivative
[format] => Thumbnail
[original] => moviefile_1934307.mp4
)
I am trying to get the value of the first nested key name which is "/photogalleryupload.thumbs/1934307_000001.jpg" and assign it to a variable.
For example, I would like the following code:
echo $data['files'][0];
To return this:
/photogalleryupload.thumbs/1934307_000001.jpg
This does not work.
The difficulty I am having is that this value I am trying to return is a 2nd level key name and I have been having trouble finding a way of assigning it to a variable.
$keys = array_keys($data['files'])
$key = $keys[0]

How do i get passed an empty key in a php array

how to i get to the arrays content if it doesnt have a key like this
$products[0]
this will get me partially there but how to i get past []
( [0] => Array
( [] => Array (
[0] => Array ( [product_name] => stuff i need to get to )
That is very strange. You could try
$products[0][''][0]['product_name']
Use var_dump or var_export to print your array and you will see its an empty string.
print_r will give you output like that for empty strings.
$products[0][""][0]["product_name"]

Categories